#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <signal.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <SDL/SDL.h>
#define BASE_PORTS 0x56000000
#define BASE_ADC 0x58000000
#define PORT_F_CONFIGURACION 0x50
#define PORT_G_CONFIGURACION 0x60
#define PORT_F_DATOS PORT_F_CONFIGURACION + 4
#define PORT_G_DATOS PORT_G_CONFIGURACION + 4
#define ADC_0 0x0C
#define BUTTON_L 3
#define BUTTON_C 4
#define BUTTON_R 2
#define INCLINOMETRO_1 9
#define INCLINOMETRO_2 10
#define CONFIGURA_F 0xFC0F
#define CONFIGURA_G 0xFFC3FFFF
#define CONFIGURA_ADC 0x7FC2
typedef struct
{
char button_left;
char button_central;
char button_right;
char inclinacion;
unsigned char luminosidad;
}
ENTRADAS;
volatile char salir = 0;
static volatile void *memoria_puertos;
static volatile void *memoria_adc;
void salida(int sig)
{
salir = 1;
}
void inicializa_entradas()
{
int fd = open("/dev/mem", O_RDWR);
if (fd < 0)
{
perror("/dev/mem");
exit(1);
}
memoria_puertos = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, BASE_PORTS);
if (memoria_puertos == MAP_FAILED)
{
perror("mmap ports");
exit(1);
}
memoria_adc = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, BASE_ADC);
if (memoria_adc == MAP_FAILED)
{
perror("mmap adc");
exit(1);
}
uint32_t configuracion = *(uint32_t *) (memoria_puertos + PORT_F_CONFIGURACION);
configuracion &= CONFIGURA_F;
*(uint32_t *) (memoria_puertos + PORT_F_CONFIGURACION) = configuracion;
configuracion = *(uint32_t *) (memoria_puertos + PORT_G_CONFIGURACION);
configuracion &= CONFIGURA_G;
*(uint32_t *) (memoria_puertos + PORT_G_CONFIGURACION) = configuracion;
*(uint32_t *) (memoria_adc) = CONFIGURA_ADC;
configuracion = *(uint32_t *) (memoria_adc + ADC_0);
}
void lee_entradas(ENTRADAS *entradas)
{
uint32_t datos = *(uint32_t *) (memoria_puertos + PORT_F_DATOS);
entradas->button_right = (datos & (int)pow(2, BUTTON_R)) >> BUTTON_R;
entradas->button_left = (datos & (int)pow(2, BUTTON_L)) >> BUTTON_L;
entradas->button_central = (datos & (int)pow(2, BUTTON_C)) >> BUTTON_C;
datos = *(uint32_t *) (memoria_puertos + PORT_G_DATOS);
entradas->inclinacion = ((datos & (int)pow(2, INCLINOMETRO_2)) + (datos & (int)pow(2, INCLINOMETRO_1))) >> INCLINOMETRO_1;
datos = *(uint32_t *) memoria_adc;
if(datos & 0x8000)
{
datos = *(uint32_t *) (memoria_adc + ADC_0);
entradas->luminosidad = (datos & 0x3FF) * 255 / 1023;
}
}
int main(void)
{
float radianes = 0;
float destino_radianes;
float suma_radianes;
ENTRADAS entradas;
inicializa_entradas();
signal(SIGINT, salida);
while(!salir)
{
lee_entradas(&entradas);
// dibujamos el button left
if(!entradas.button_left)
{
return 1;
}
else
{
// return 1;
}
// dibujamos el button central
// destino.x = pantalla->w / 2 - ok->w / 2;
if(!entradas.button_central)
{
return 2;
}
else
{
}
// dibujamos el button right
if(!entradas.button_right)
{
return 3;
// SDL_BlitSurface(derecha, NULL, pantalla, &destino);
}
else
{
// SDL_FillRect(pantalla, &destino, SDL_MapRGB(pantalla->format, 0, 0, 0));
// return 3;
}
}
printf("\nEnd of Program\n");
return 0;
}