Advertisement
silver2row

Trying to understand Configurations in C/C++

Aug 7th, 2024
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.70 KB | None | 0 0
  1. // Differences from Sean J. Miller on element14
  2. // Since its conception, the links have changed and have since been lost!
  3.  
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. // #include <gpiod.h>
  7. #include <stdlib.h>
  8. #include <fcntl.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <errno.h>
  12.  
  13. void GPIO_One(int Gpio) {
  14.     FILE *bright;
  15.     bright = fopen("/sys/class/leds/m1_high/brightness", "w");
  16.     fseek(bright, 0, SEEK_SET);
  17.     fprintf(bright, "%d", 1 * Gpio);
  18.     fclose(bright);
  19. }
  20.  
  21. void setupPWM(int enable) {
  22.     FILE *pwm;
  23.     pwm = fopen("/dev/beagle/pwm/P9_16/enable", "w");
  24.     fseek(pwm, 0, SEEK_SET);
  25.     fprintf(pwm, "%d", 1 * enable);
  26.     fclose(pwm);
  27. }
  28.  
  29. void setupPeriod(int period_one) {
  30.     FILE *period;
  31.     period = fopen("/dev/beagle/pwm/P9_16/period", "w");
  32.     fseek(period, 0, SEEK_SET);
  33.     fprintf(period, "%d", 10 * period_one);
  34.     fclose(period);
  35. }
  36.  
  37. void pwm_duty(int the_duty_multiplier) {
  38.     FILE *duty;
  39.     duty = fopen("/dev/beagle/pwm/P9_16/duty_cycle", "w");
  40.     fseek(duty, 0, SEEK_SET);
  41.     fprintf(duty, "%d", 100 * the_duty_multiplier);
  42.     fclose(duty);
  43. }
  44.  
  45. int testInteger = 0;
  46.  
  47. int main() {
  48.     printf("Setting up\n");
  49.     setupPWM(0);
  50.  
  51.     while(1) {
  52.         printf("Enter an integer, Please: ");
  53.         scanf("%d", &testInteger);
  54.         if (testInteger >= 10) {
  55.             setupPWM(1);
  56.             setupPeriod(140);
  57.             pwm_duty(10);
  58.             GPIO_One(1);
  59.         }
  60.         usleep(2500);
  61.  
  62.         if (testInteger < 2) {
  63.             setupPWM(1);
  64.             setupPeriod(250);
  65.             pwm_duty(0);
  66.             GPIO_One(1);
  67.         }
  68.  
  69.         usleep(2500);
  70.     }
  71.     pwm_duty(0);
  72.     setupPWM(0);
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement