Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
iamedu committed Jun 7, 2011
0 parents commit a3da96b
Show file tree
Hide file tree
Showing 446 changed files with 29,497 additions and 0 deletions.
19 changes: 19 additions & 0 deletions app/libtt/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CFLAGS = -Iinclude -lpthread
#CC=arm-unknown-linux-gnu-gcc
CC=gcc

demo: list.o controller.o main.c lcd.o
$(CC) -o demo main.c list.o lcd.o controller.o $(CFLAGS)

list.o: list.c
$(CC) -c list.c $(CFLAGS)

controller.o: controller.c
$(CC) -c controller.c $(CFLAGS)

lcd.o: lcd.c
$(CC) -c lcd.c $(CFLAGS)

clean:
rm -r *.o demo

Binary file added app/libtt/android.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/libtt/campo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
173 changes: 173 additions & 0 deletions app/libtt/controller.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#include <controller.h>
#include <list.h>

#include <pthread.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#include <linux/input.h>
#include <linux/joystick.h>

#define NAME_LENGTH 128

#define UP 4
#define DOWN 6
#define RIGHT 5
#define LEFT 7

ListNode *root = NULL;

typedef struct {
int fd;
unsigned char axes;
unsigned char buttons;
int version;
char name[NAME_LENGTH];
uint16_t btnmap[KEY_MAX - BTN_MISC + 1];
uint8_t axmap[ABS_MAX + 1];
char *button;
int *axis;
pthread_t thread;
pthread_mutex_t mutex;
} Controller;

void *buttons_reload(void *arg) {
Controller *self = (Controller *)arg;
struct js_event js;
int ret = 0;
while(1) {
if(read(self->fd, &js, sizeof(struct js_event)) != sizeof(struct js_event)) {
perror("\ncontroller: error reading");
ret = 1;
pthread_exit(&ret);
}
pthread_mutex_lock(&self->mutex);
switch(js.type & ~JS_EVENT_INIT) {
case JS_EVENT_BUTTON:
self->button[js.number] = js.value;
break;
case JS_EVENT_AXIS:
self->axis[js.number] = js.value;
break;
}
pthread_mutex_unlock(&self->mutex);
}
pthread_exit(&ret);
}

int initControl(char *filename) {
Controller *controller;
pthread_t thread;

controller = (Controller *)malloc(sizeof(Controller));

//Initialize
controller->fd = open(filename, O_RDONLY);
controller->axes = 2;
controller->buttons = 2;
controller->version = 0x000800;

ioctl(controller->fd, JSIOCGVERSION, &controller->version);
ioctl(controller->fd, JSIOCGAXES, &controller->axes);
ioctl(controller->fd, JSIOCGBUTTONS, &controller->buttons);
ioctl(controller->fd, JSIOCGNAME(NAME_LENGTH), controller->name);
ioctl(controller->fd, JSIOCGAXMAP, controller->axmap);
ioctl(controller->fd, JSIOCGBTNMAP, controller->btnmap);

controller->button = calloc(controller->buttons, sizeof(char));
controller->axis = calloc(controller->axes, sizeof(int));

root = add_node(root, controller);

pthread_create(&controller->thread, NULL, buttons_reload, controller);
pthread_mutex_init(&controller->mutex, NULL);

return controller->fd;
}

int compare(ListNode *this, ListNode *other) {
Controller *cthis;
Controller *cother;

cthis = (Controller *)this->object;
cother = (Controller *)other->object;

return cthis->fd == cother->fd;
}

int readControl(int fd) {
int result = 8;
int *dir;
int i;
ListNode *tmpNode;
Controller *tmpController;
ListNode *node = (ListNode *)malloc(sizeof(ListNode));
Controller *ctemp = (Controller *)malloc(sizeof(Controller));
ctemp->fd = fd;
node->object = ctemp;
tmpNode = get_node(root, node, &compare);
if(tmpNode == NULL) {
free(ctemp);
free(node);
return -1;
}
free(ctemp);
free(node);
ctemp = tmpNode->object;
pthread_mutex_lock(&ctemp->mutex);

dir = malloc(sizeof(int) * 4);

for(i = 0; i < 4; i++) {
dir[i] = 0;
}

if(ctemp->button[4] ||(ctemp->axis[1] >= -32767 && ctemp->axis[1] < 0)){
dir[0] = 1;
}
if(ctemp->button[6] ||(ctemp->axis[1] > 0 && ctemp->axis[1] <= 32767)){
dir[2] = 1;
}
if(ctemp->button[5] ||(ctemp->axis[0] > 0 && ctemp->axis[0] <= 32767)){
dir[1] = 1;
}
if(ctemp->button[7] ||(ctemp->axis[0] >= -32767 && ctemp->axis[0] < 0)){
dir[3] = 1;
}

//printf("%d %d\n", ctemp->button[UP], ctemp->button[RIGHT]);

if(dir[0] && dir[1]) {
result = 1;
} else if(dir[1] && dir[2]) {
result = 3;
} else if(dir[2] && dir[3]) {
result = 5;
} else if(dir[3] && dir[0]) {
result = 7;
} else if(dir[0]) {
result = 0;
} else if(dir[1]) {
result = 2;
} else if(dir[2]) {
result = 4;
} else if(dir[3]) {
result = 6;
} else {
result = 8;
}

free(dir);
pthread_mutex_unlock(&ctemp->mutex);
return result;
}

Binary file added app/libtt/gameover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions app/libtt/images.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[images]
1=jugador1.png
2=jugador2.png
3=pelota.png
4=campo.png
5=gameover.png
8 changes: 8 additions & 0 deletions app/libtt/include/controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef _CONTROLLER_H
#define _CONTROLLER_H

int initControl(char *filename);
int readControl(int fd);

#endif

23 changes: 23 additions & 0 deletions app/libtt/include/lcd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef _LCD_H
#define _LCD_H

typedef struct {
int x;
int y;
} Point;

typedef struct {
int width;
int height;
} Dimension;

int initLCD();
int createObject(int lcd, int id, int w, int h);
int move(int lcd, int id, int mid);
int setPosition(int lcd, int id, int x, int y);
int stopLCD(int lcd);
Point *getPosition(int lcd, int id);
Dimension *getDimensions(int lcd, int id);

#endif

9 changes: 9 additions & 0 deletions app/libtt/include/list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
typedef struct {
void *object;
void *next;
} ListNode;

ListNode *add_node(ListNode *root, void *value);
ListNode *get_node(ListNode *root, ListNode* expected,
int (*comparator)(void *, void *));

Binary file added app/libtt/jugador1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/libtt/jugador2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
140 changes: 140 additions & 0 deletions app/libtt/lcd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#include <lcd.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>

#define buflen 512
#define HOSTNAME "127.0.0.1"
unsigned int portno = 1234;

typedef struct {
char operation;
char op1;
char op2;
char op3;
} LCDMessage;

typedef struct {
int res1;
int res2;
} LCDResult;

int initLCD() {
int sd = socket(AF_INET, SOCK_STREAM, 0);

struct sockaddr_in sin;
struct hostent *host = gethostbyname(HOSTNAME);
char buf[buflen];
int len;

memcpy(&sin.sin_addr.s_addr, host->h_addr, host->h_length);
sin.sin_family = AF_INET;
sin.sin_port = htons(portno);

if (connect(sd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
perror("Connecting");
exit(1);
}

return sd;
}

int stopLCD(int lcd) {
return close(lcd);
}

int createObject(int lcd, int id, int x, int y) {
LCDMessage *message = malloc(sizeof(LCDMessage));
LCDResult *result = malloc(sizeof(LCDResult));
int res;

message->operation = 1;
message->op1 = x;
message->op2 = y;
message->op3 = id;

send(lcd, message, sizeof(LCDMessage), 0);
recv(lcd, result, sizeof(LCDResult), 0);

res = result->res1;

free(result);
free(message);
return res;
}

int move(int lcd, int id, int mid) {
LCDMessage *message = malloc(sizeof(LCDMessage));

message->operation = 2;
message->op1 = id;
message->op2 = mid;
message->op3 = 0;

send(lcd, message, sizeof(LCDMessage), 0);

free(message);

usleep(1000);

return 1;
}

int setPosition(int lcd, int id, int x, int y) {
LCDMessage *message = malloc(sizeof(LCDMessage));

message->operation = 4;
message->op1 = id;
message->op2 = x;
message->op3 = y;

send(lcd, message, sizeof(LCDMessage), 0);

free(message);

usleep(1000);

return 1;
}

Point *getPosition(int lcd, int id) {
LCDMessage *message = malloc(sizeof(LCDMessage));
LCDResult *result = malloc(sizeof(LCDResult));
Point *point = malloc(sizeof(Point));
message->operation = 3;
message->op1 = id;

send(lcd, message, sizeof(LCDMessage), 0);
recv(lcd, result, sizeof(LCDResult), 0);

point->x = result->res1;
point->y = result->res2;

free(message);
free(result);

return point;
}

Dimension *getDimensions(int lcd, int id) {
LCDMessage *message = malloc(sizeof(LCDMessage));
LCDResult *result = malloc(sizeof(LCDResult));
Dimension *dimensions = malloc(sizeof(Dimension));
message->operation = 5;
message->op1 = id;

send(lcd, message, sizeof(LCDMessage), 0);
recv(lcd, result, sizeof(LCDResult), 0);

dimensions->width = result->res1;
dimensions->height = result->res2;

free(message);
free(result);

return dimensions;

}
Loading

0 comments on commit a3da96b

Please sign in to comment.