Advertisement
Guest User

Parrot DF3120 GPIO Keys Polling

a guest
Feb 17th, 2012
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <signal.h>
  5. #include <sys/mman.h>
  6. #include <fcntl.h>
  7. #include <SDL/SDL.h>
  8.  
  9. #define BASE_PORTS 0x56000000
  10. #define BASE_ADC 0x58000000
  11. #define PORT_F_CONFIGURACION 0x50
  12. #define PORT_G_CONFIGURACION 0x60
  13. #define PORT_F_DATOS PORT_F_CONFIGURACION + 4
  14. #define PORT_G_DATOS PORT_G_CONFIGURACION + 4
  15. #define ADC_0 0x0C
  16. #define BUTTON_L  3
  17. #define BUTTON_C  4
  18. #define BUTTON_R  2
  19. #define INCLINOMETRO_1  9
  20. #define INCLINOMETRO_2  10
  21. #define CONFIGURA_F 0xFC0F
  22. #define CONFIGURA_G 0xFFC3FFFF
  23. #define CONFIGURA_ADC 0x7FC2
  24.  
  25. typedef struct
  26. {
  27.  char button_left;
  28.  char button_central;
  29.  char button_right;
  30.  char inclinacion;
  31.  unsigned char luminosidad;
  32. }
  33. ENTRADAS;
  34.  
  35.  
  36. volatile char salir = 0;
  37. static volatile void *memoria_puertos;
  38. static volatile void *memoria_adc;
  39.  
  40. void salida(int sig)
  41. {
  42.     salir = 1;
  43. }
  44.  
  45. void inicializa_entradas()
  46. {
  47.     int fd = open("/dev/mem", O_RDWR);
  48.  
  49.         if (fd < 0)
  50.     {
  51.         perror("/dev/mem");
  52.         exit(1);
  53.     }
  54.     memoria_puertos = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, BASE_PORTS);
  55.     if (memoria_puertos == MAP_FAILED)
  56.     {
  57.         perror("mmap ports");
  58.         exit(1);
  59.     }
  60.  
  61.     memoria_adc = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, BASE_ADC);
  62.     if (memoria_adc == MAP_FAILED)
  63.     {
  64.         perror("mmap adc");
  65.         exit(1);
  66.     }
  67.  
  68.     uint32_t configuracion = *(uint32_t *) (memoria_puertos + PORT_F_CONFIGURACION);
  69.     configuracion &= CONFIGURA_F;
  70.     *(uint32_t *) (memoria_puertos + PORT_F_CONFIGURACION) = configuracion;
  71.  
  72.     configuracion = *(uint32_t *) (memoria_puertos + PORT_G_CONFIGURACION);
  73.     configuracion &= CONFIGURA_G;
  74.     *(uint32_t *) (memoria_puertos + PORT_G_CONFIGURACION) = configuracion;
  75.  
  76.     *(uint32_t *) (memoria_adc) = CONFIGURA_ADC;
  77.     configuracion = *(uint32_t *) (memoria_adc + ADC_0);
  78.  
  79. }
  80.  
  81. void lee_entradas(ENTRADAS *entradas)
  82. {
  83.     uint32_t datos = *(uint32_t *) (memoria_puertos + PORT_F_DATOS);
  84.     entradas->button_right = (datos & (int)pow(2, BUTTON_R)) >> BUTTON_R;
  85.     entradas->button_left = (datos & (int)pow(2, BUTTON_L)) >> BUTTON_L;
  86.     entradas->button_central = (datos & (int)pow(2, BUTTON_C)) >> BUTTON_C;
  87.     datos = *(uint32_t *) (memoria_puertos + PORT_G_DATOS);
  88.     entradas->inclinacion = ((datos & (int)pow(2, INCLINOMETRO_2)) + (datos & (int)pow(2, INCLINOMETRO_1))) >> INCLINOMETRO_1;
  89.     datos = *(uint32_t *) memoria_adc;
  90.     if(datos & 0x8000)
  91.     {
  92.         datos = *(uint32_t *) (memoria_adc + ADC_0);
  93.         entradas->luminosidad = (datos & 0x3FF) * 255 / 1023;
  94.     }
  95. }
  96.  
  97. int main(void)
  98. {
  99.     float radianes = 0;
  100.     float destino_radianes;
  101.     float suma_radianes;
  102.     ENTRADAS entradas;
  103.  
  104.     inicializa_entradas();
  105.  
  106.     signal(SIGINT, salida);
  107.  
  108.     while(!salir)
  109.     {
  110.         lee_entradas(&entradas);
  111.  
  112.         // dibujamos el button left
  113.         if(!entradas.button_left)
  114.         {
  115.             return 1;
  116.         }
  117.         else
  118.         {
  119.             // return 1;
  120.         }
  121.         // dibujamos el button central
  122.         // destino.x = pantalla->w / 2 - ok->w / 2;
  123.         if(!entradas.button_central)
  124.         {
  125.             return 2;
  126.         }
  127.         else
  128.         {
  129.         }
  130.         // dibujamos el button right
  131.             if(!entradas.button_right)
  132.         {
  133.             return 3;
  134.             // SDL_BlitSurface(derecha, NULL, pantalla, &destino);
  135.         }
  136.         else
  137.         {
  138.             // SDL_FillRect(pantalla, &destino, SDL_MapRGB(pantalla->format, 0, 0, 0));
  139.             // return 3;
  140.         }
  141.     }
  142.  
  143.  
  144.     printf("\nEnd of Program\n");
  145.  
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement