Advertisement
Guest User

Untitled

a guest
Jan 16th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. /*
  2. ,* Copyright © 2013, Malcolm Sparks <malcolm@congreve.com>. All Rights Reserved.
  3. ,*
  4. ,* A program to convert USB firing events from the Dream Cheeky 'Big Red Button' to MQTT events.
  5. ,*/
  6.  
  7. #include <fcntl.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11.  
  12. #define LID_CLOSED 21
  13. #define BUTTON_PRESSED 22
  14. #define LID_OPEN 23
  15.  
  16. int main(int argc, char **argv)
  17. {
  18.   int fd;
  19.   int i, res, desc_size = 0;
  20.   char buf[256];
  21.  
  22.   /* Use a udev rule to make this device */
  23.   fd = open("/dev/hidraw3", O_RDWR|O_NONBLOCK);
  24.  
  25.   if (fd < 0) {
  26.     perror("Unable to open device");
  27.     return 1;
  28.   }
  29.  
  30.   int prior = LID_CLOSED;
  31.  
  32.   while (1) {
  33.     memset(buf, 0x0, sizeof(buf));
  34.     buf[0] = 0x08;
  35.     buf[7] = 0x02;
  36.  
  37.     res = write(fd, buf, 8);
  38.     if (res < 0) {
  39.       perror("write");
  40.       exit(1);
  41.     }
  42.  
  43.     memset(buf, 0x0, sizeof(buf));
  44.     res = read(fd, buf, 8);
  45.  
  46.     if (res >= 0) {
  47.       if (prior == LID_CLOSED && buf[0] == LID_OPEN) {
  48.          printf("Lid Aberto?\n");
  49.          fflush(stdout);
  50.       } else if (prior != BUTTON_PRESSED && buf[0] == BUTTON_PRESSED) {
  51.          printf("Fire!!\n");
  52.      system("cvlc --play-and-exit /home/atcasanova/Dropbox/atila.mp3");
  53.          fflush(stdout);
  54.       } else if (prior != LID_CLOSED && buf[0] == LID_CLOSED) {
  55.          printf("Lid Closed!");
  56.          fflush(stdout);
  57.       }
  58.       prior = buf[0];
  59.     }
  60.     usleep(20000); /* Sleep for 20ms*/
  61.   }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement