Advertisement
MegamiSyn

Untitled

Apr 1st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.36 KB | None | 0 0
  1. #include <wiringPi.h>
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <lirc/lirc_client.h>
  7. #include <time.h>
  8.  
  9. void flipLED (int led);
  10.  
  11. //The WiringPi pin numbers used by our LEDs
  12. #define LED1 4
  13. #define LED2 5
  14. #define LED3 6
  15.  
  16. #define ON 1
  17. #define OFF 0
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21.         struct lirc_config *config;
  22.  
  23.         //Timer for our buttons
  24.         int buttonTimer = millis();
  25.  
  26.         char *code;
  27.         char *c;
  28.  
  29.         //Initiate WiringPi and set WiringPi pins 4, 5 & 6 (GPIO 23, 24 & 25) to output. These are the pins the LEDs are connected to.
  30.         if (wiringPiSetup () == -1)
  31.             exit (1) ;
  32.  
  33.         pinMode (LED1, OUTPUT);
  34.         pinMode (LED2, OUTPUT);
  35.         pinMode (LED3, OUTPUT);
  36.  
  37.         //Initiate LIRC. Exit on failure
  38.         if(lirc_init("lirc",1)==-1)
  39.                 exit(EXIT_FAILURE);
  40.  
  41.         //Read the default LIRC config at /etc/lirc/lircd.conf  This is the config for your remote.
  42.         if(lirc_readconfig(NULL,&config,NULL)==0)
  43.         {
  44.                 //Do stuff while LIRC socket is open  0=open  -1=closed.
  45.                 while(lirc_nextcode(&code)==0)
  46.                 {
  47.                         //If code = NULL, meaning nothing was returned from LIRC socket,
  48.                         //then skip lines below and start while loop again.
  49.                         if(code==NULL) continue;{
  50.                                 //Make sure there is a 400ms gap before detecting button presses.
  51.                                 if (millis() - buttonTimer  > 400){
  52.                                         //Check to see if the string "KEY_1" appears anywhere within the string 'code'.
  53.                                         if(strstr (code,"KEY_1")){
  54.                                                 printf("MATCH on KEY_1\n");
  55.                                                 flipLED(LED1);
  56.                                                 buttonTimer = millis();
  57.                                         }
  58.                                         else if(strstr (code,"KEY_2")){
  59.                                                 printf("MATCH on KEY_2\n");
  60.                                                 flipLED(LED2);
  61.                                                 buttonTimer = millis();
  62.                                         }
  63.                                         else if(strstr (code,"KEY_3")){
  64.                                                 printf("MATCH on KEY_3\n");
  65.                                                 flipLED(LED3);
  66.                                                 buttonTimer = millis();
  67.                                         }
  68.                                 }
  69.                         }
  70.                         //Need to free up code before the next loop
  71.                         free(code);
  72.                 }
  73.                 //Frees the data structures associated with config.
  74.                 lirc_freeconfig(config);
  75.         }
  76.         //lirc_deinit() closes the connection to lircd and does some internal clean-up stuff.
  77.         lirc_deinit();
  78.         exit(EXIT_SUCCESS);
  79. }
  80.  
  81. void flipLED (int led)
  82. {
  83.         //If LED is on, turn it off. Otherwise it is off, so thefore we need to turn it on.
  84.         if(digitalRead(led)==ON)
  85.                 digitalWrite(led, OFF);
  86.         else
  87.                 digitalWrite(led, ON);
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement