Advertisement
silver2row

Molloy LED program

Jul 24th, 2019
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. /** Simple On-board LED flashing program - written in C by Derek Molloy
  2. *    simple functional struture for the Exploring BeagleBone book
  3. *
  4. *    This program uses USR LED 3 and can be executed in three ways:
  5. *         makeLED on
  6. *         makeLED off
  7. *         makeLED flash  (flash at 100ms intervals - on 50ms/off 50ms)
  8. *         makeLED status (get the trigger status)
  9. *
  10. * Written by Derek Molloy for the book "Exploring BeagleBone: Tools and
  11. * Techniques for Building with Embedded Linux" by John Wiley & Sons, 2014
  12. * ISBN 9781118935125. Please see the file README.md in the repository root
  13. * directory for copyright and GNU GPLv3 license information.            */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19. #define LED3_PATH "/sys/class/gpio/gpio60/"
  20.  
  21. void writeLED(char filename[], char value[]);
  22. void removeTrigger();
  23.  
  24. int main(int argc, char* argv[]){
  25.    if(argc!=2){
  26.         printf("Usage is makeLEDC and one of:\n");
  27.         printf("   on, off, flash or status\n");
  28.         printf(" e.g. makeLED flash\n");
  29.         return 2;
  30.    }
  31.    printf("Starting the makeLED program\n");
  32.    printf("The current LED Path is: " LED3_PATH "\n");
  33.  
  34.    // select whether command is on, off, flash or status
  35.    if(strcmp(argv[1],"on")==0){
  36.         printf("Turning the LED on\n");
  37.         removeTrigger();
  38.         writeLED("/brightness", "1");
  39.    }
  40.    else if (strcmp(argv[1],"off")==0){
  41.         printf("Turning the LED off\n");
  42.         removeTrigger();
  43.         writeLED("/brightness", "0");
  44.    }
  45.    else if (strcmp(argv[1],"flash")==0){
  46.         printf("Flashing the LED\n");
  47.         writeLED("/trigger", "timer");
  48.         writeLED("/delay_on", "50");
  49.         writeLED("/delay_off", "50");
  50.    }
  51.    else if (strcmp(argv[1],"status")==0){
  52.       FILE* fp;   // see writeLED function below for description
  53.       char  fullFileName[100];
  54.       char line[80];
  55.       sprintf(fullFileName, LED3_PATH "/trigger");
  56.       fp = fopen(fullFileName, "rt"); //reading text this time
  57.       while (fgets(line, 80, fp) != NULL){
  58.          printf("%s", line);
  59.       }
  60.        else{
  61.         printf("Invalid command!\n");
  62.    }
  63.    printf("Finished the makeLED Program\n");
  64.    return 0;
  65. }
  66.  
  67. void writeLED(char filename[], char value[]){
  68.    FILE* fp;   // create a file pointer fp
  69.    char  fullFileName[100];  // to store the path and filename
  70.    sprintf(fullFileName, LED3_PATH "%s", filename); // write path and filename
  71.    fp = fopen(fullFileName, "w+"); // open file for writing
  72.    fprintf(fp, "%s", value);  // send the value to the file
  73.    fclose(fp);  // close the file using the file pointer
  74. }
  75.  
  76. void removeTrigger(){
  77.   writeLED("/trigger", "none");
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement