Guest User

Untitled

a guest
Mar 26th, 2013
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <unistd.h>
  8. #include <syslog.h>
  9. #include <string.h>
  10. #include <wiringPi.h>
  11.  
  12. #define sync_pin    7  // => 3
  13. #define data_pin    9  // => 5
  14. #define clock_pin   8  // => 7
  15.  
  16. unsigned char led_cmd[10] =  {0,0,1,0,0,0,0,1,0,0}; //Activates/initialises the LEDs, leaving the center LED lit.
  17. unsigned char anim_cmd[10] = {0,0,1,0,0,0,0,1,0,1}; //Makes the startup animation on the ring of light.
  18. unsigned char sync_cmd[10] = {0,0,0,0,0,0,0,1,0,0}; //Initiates the sync process.
  19.  
  20.  
  21. void wait_for(int pin, int v) {
  22.     int pin_value;
  23.     do {
  24.         pin_value = digitalRead(pin);
  25.     } while( pin_value != v );
  26. }
  27.  
  28. void send_cmd(unsigned char * cmd) {
  29.     int i = 0;
  30.  
  31.     /* Send start bit (wakeup the clock) */
  32.     digitalWrite(data_pin, LOW);
  33.    
  34.     for(i = 0; i < 10; i++){
  35.         wait_for(clock_pin, 0);
  36.  
  37.         digitalWrite(data_pin, cmd[i]);
  38.  
  39.         wait_for(clock_pin, 1);
  40.     }
  41.     wait_for(clock_pin, 0);
  42.    
  43.     /* Send end bit (stop the clock) */
  44.     digitalWrite(data_pin, HIGH);
  45.     wait_for(clock_pin, 1);
  46. }
  47.  
  48. int main (void)
  49. {
  50.     pid_t pid, sid;
  51.     printf ("Rasberry Pi 360 Board Daemon\n") ;
  52.  
  53.     /* Init Wiring Pi */
  54.     if (wiringPiSetup () == -1)
  55.         return 1 ;
  56.        
  57.     /* Fork off the parent process */
  58.     pid = fork();
  59.     if (pid < 0) {
  60.         exit(EXIT_FAILURE);
  61.     }
  62.     /* If we got a good PID, then
  63.        we can exit the parent process. */
  64.     if (pid > 0) {
  65.         exit(EXIT_SUCCESS);
  66.     }
  67.    
  68.     /* Create a new SID for the child process */
  69.     sid = setsid();
  70.     if (sid < 0) {
  71.             /* Log the failure */
  72.             exit(EXIT_FAILURE);
  73.     }
  74.  
  75.     /* Setup gpio */
  76.     pinMode(sync_pin, INPUT);
  77.     pinMode(data_pin, OUTPUT);
  78.     pinMode(clock_pin, OUTPUT);
  79.     digitalWrite(clock_pin, HIGH);
  80.     pinMode(clock_pin, INPUT);
  81.  
  82.     /* Stop RF Board Clock */
  83.     digitalWrite(data_pin, HIGH);
  84.  
  85.     delay(2000);
  86.  
  87.     send_cmd(led_cmd);
  88.     send_cmd(anim_cmd);
  89.    
  90.     while(1) {
  91.         // Wait For Event in sync btn
  92.         wait_for(sync_pin, 0);
  93.         send_cmd(sync_cmd);
  94.     }
  95.     return 0 ;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment