Subversion Repositories Projects

Compare Revisions

Ignore whitespace Rev 129 → Rev 130

/Riddim/Makefile
1,7 → 1,7
CFLAGS+=-Wall -g -I/usr/include/libxml2
CFLAGS+=-Wall -g
LDFLAGS+=-g -lusb -lbluetooth -lconfuse
 
riddim: fc.o bluetooth_handler.o riddim.o lib/x52/x52.o
riddim: config.o statistics.o fc.o evdev_handler.o bluetooth_handler.o riddim.o lib/x52/x52.o
 
 
clean:
/Riddim/README
1,4 → 1,5
# RIDDIM
#
# -> Remote Interactive Digital Drone Interface Mashup
#
# Author: Marcus -ligi- Bueschleb
14,19 → 15,9
# eduard -- - at - -- hasenleithner.at for the linux x-52 lib
#
 
 
=Preface=
 
Project to Control and interact with a MikroKopter. The main Focus on startup is the usage of one X-52 Joystick in combination with one Router. The X-52 has the possibility to Display some something on the Joystick and has lots of Axis and Buttons - so i will prefer it for flying.
 
= Documentation =
 
http://www.rcmovie.de/view_video.php?viewkey=7ec0047de2d5d1b28a75
http://www.youtube.com/watch?v=3a0FSJTDQlM
http://forum.mikrokopter.de/topic-2554.html
= Documentation / Infos =
http://www.mikrokopter.de/ucwiki/en/Riddim
 
= Licence =
 
http://creativecommons.org/licenses/by-nc-sa/2.0/de/ (Creative Commons / Non Commercial / Share Alike
34,6 → 25,7
This explicitly includes that lethal Weapon owning "People" (e.g. Army & Police) are not allowed to use this Project
If you want to use this tool in any Commercial Context - please contact me first!
 
 
=Hardware Setup=
 
You need:
69,9 → 61,3
0.2 - first version with possibility to fly ( based on FC0.68b commands )
0.1 - initial release with basic functions and proove of concept Code ( Scanning for Blueooth Devices / read Joy / communicate to MK / ..)
 
= ToDo =
 
- Force Feedback from ACC Sensor Data
- Support more JoySticks -> generalize
- Support more Aircraft types -> generalize
/Riddim/bluetooth_handler.c
5,6 → 5,9
char names[MAX_BT_DEVICES][248];
char addrs[MAX_BT_DEVICES][19];
 
char BtHostRxBuffer[150];
 
 
int scan_bt()
{
inquiry_info *ii = NULL;
48,3 → 51,130
close( sock );
return 1;
}
 
struct timeval timeout;
fd_set readfds, writefds;
int s;
struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
char buf[1024] = { 0 };
int client, bytes_read, status;
unsigned int opt = sizeof(rem_addr);
 
int maxfd, sock_flags;
 
 
 
int bt_host_init()
{
// allocate socket
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
// bind socket to port 1 of the first available bluetooth adapter
loc_addr.rc_family = AF_BLUETOOTH;
loc_addr.rc_bdaddr = *BDADDR_ANY;
loc_addr.rc_channel = 1;
bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));
// put server socket into listening mode
listen(s, 1);
// put server socket into nonblocking mode
sock_flags = fcntl( s, F_GETFL, 0 );
fcntl( s, F_SETFL, sock_flags | O_NONBLOCK );
 
return 1;
}
 
 
int bt_host_connected=0;
 
int bt_host_send(char RxBuffer[150],int len)
{
if (bt_host_connected)
{
send(client,"\r",1,0);
send(client,RxBuffer,len,0);
send(client,"\r",1,0);
}
return 1;
}
 
int bt_host_tick(int mk_fd)
{
 
// try to accept connection if none yet
if (!bt_host_connected)
{
timeout.tv_sec = 0;
timeout.tv_usec = 100;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_SET(s, &readfds);
maxfd = s;
printf("waiting for connection\n");
status = select(maxfd + 1, &readfds, &writefds, 0, &timeout);
if( status > 0 && FD_ISSET( s, &readfds ) ) {
// incoming connection
client = accept( s, (struct sockaddr*)&rem_addr, &opt );
if( client >= 0 )
{
bt_host_connected=1;
 
// close the server socket, leaving only the client socket open
// close(s);
ba2str( &rem_addr.rc_bdaddr, buf );
fprintf(stderr, "accepted connection from %s!!!!!!!\n", buf);
memset(buf, 0, sizeof(buf));
// put client socket into nonblocking mode
sock_flags = fcntl( client, F_GETFL, 0 );
fcntl( client, F_SETFL, sock_flags | O_NONBLOCK );
}
}
 
}
else
{
printf("reading from bt_host");
char in_char='#';
int count=0;
int r=0;
 
timeout.tv_sec = 0;
timeout.tv_usec = 100;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_SET(client, &readfds);
maxfd = client;
printf("waiting for connection\n");
status = select(maxfd + 1, &readfds, 0, 0, &timeout);
if( status >0 )
{
send(mk_fd,"\r",1,0);
while(in_char!='\r')
{
count=read(client,&in_char,1);
if (count==-1)
{
bt_host_connected=0;
return 0;
}
printf("count: %d in %d chr %c\n",count,in_char,in_char);
send(mk_fd,&in_char,1,0);
BtHostRxBuffer[r]=in_char;
}
send(mk_fd,"\r",1,0);
}
else
return 0;
}
return 1;
}
/Riddim/bluetooth_handler.h
2,7 → 2,9
#if !defined(BLUETOOTH_HANDLER_H)
#define BLUETOOTH_HANDLER_H
 
 
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
 
10,6 → 12,20
 
#include <stdlib.h>
 
 
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
 
 
 
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>//for close() for socket
 
#include <string.h>
 
#define MAX_BT_DEVICES 3
 
extern int bt_device_count;
18,5 → 34,8
extern char addrs[MAX_BT_DEVICES][19];
 
int scan_bt();
int bt_host_tick(int mk_fd);
int bt_host_init();
int bt_host_send(char RxBuffer[150],int len);
 
#endif
/Riddim/config.c
0,0 → 1,65
#include "config.h"
 
char *input_evdev_name;
 
char *bluetooth_mac;
 
int mk_socket_port=0;
int loop_delay=0;
 
 
 
double nick_mul=0.3f;
double roll_mul=0.3f;
double gier_mul=0.3f;
double gas_mul=0.3f;
 
 
int rel_axis_nick=1;
int rel_axis_roll=0;
int rel_axis_gier=5;
int rel_axis_gas=2;
 
 
cfg_bool_t exit_after_init = cfg_false;
 
 
int parse_config(char* fname)
{
 
 
cfg_opt_t opts[] = {
CFG_SIMPLE_STR("bluetooth_mac", &bluetooth_mac),
 
CFG_SIMPLE_BOOL("exit_after_init", &exit_after_init),
CFG_SIMPLE_STR("input_evdev", &input_evdev_name),
CFG_SIMPLE_INT("loop_delay", &loop_delay),
CFG_SIMPLE_INT("mk_socket_port", &mk_socket_port),
CFG_SIMPLE_FLOAT("nick_mul", &nick_mul),
CFG_SIMPLE_FLOAT("roll_mul", &roll_mul),
CFG_SIMPLE_FLOAT("gier_mul", &gier_mul),
CFG_SIMPLE_FLOAT("gas_mul", &gas_mul),
CFG_SIMPLE_INT("rel_axis_nick", &rel_axis_nick),
CFG_SIMPLE_INT("rel_axis_roll", &rel_axis_roll),
CFG_SIMPLE_INT("rel_axis_gier", &rel_axis_gier),
CFG_SIMPLE_INT("rel_axis_gas", &rel_axis_gas),
CFG_END()
};
cfg_t *cfg;
 
 
printf("Parsing config file %s\n",fname);
cfg = cfg_init(opts, 0);
 
cfg_parse(cfg,fname);
 
 
return 0;
}
/Riddim/config.h
0,0 → 1,32
#if !defined(CONFIG_H)
#define CONFIG_H
 
#include <confuse.h>
 
// values from config
extern char *input_evdev_name;
extern char *bluetooth_mac;
extern int mk_socket_port;
extern int loop_delay;
 
 
 
extern double nick_mul;
extern double roll_mul;
extern double gier_mul;
extern double gas_mul;
 
 
extern int rel_axis_nick;
extern int rel_axis_roll;
extern int rel_axis_gier;
extern int rel_axis_gas;
 
 
extern cfg_bool_t exit_after_init ;
 
 
 
int parse_config(char* fname);
 
#endif
/Riddim/evdev_handler.c
0,0 → 1,177
#include "evdev_handler.h"
 
int evdev_fd;
int evdev_out_fd;
int retval;
size_t read_bytes; /* how many bytes were read */
struct input_event ev[64]; /* the events (up to 64 at once) */
struct input_event led_event;
 
 
 
int *evdev_rel_axis=NULL;
char *evdev_button=NULL;
 
int counter;
 
int connect_evdev(char* input_evdev_name)
{
evdev_rel_axis = (int *) calloc( 100, sizeof( int ) );
evdev_button = (char *)calloc( 100, sizeof( char ) );
 
struct input_devinfo {
uint16_t bustype;
uint16_t vendor;
uint16_t product;
uint16_t version;
};
struct input_devinfo device_info;
 
if ((evdev_out_fd = open(input_evdev_name, O_WRONLY)) < 0)
{
printf(" cant open %s for writing\n",input_evdev_name);
}
 
if ((evdev_fd = open(input_evdev_name, O_RDONLY)) < 0)
{
printf(" cant open %s for reading!",input_evdev_name);
return 0;
}
ioctl(evdev_fd,EVIOCGID,&device_info);
printf("vendor 0x%04hx product 0x%04hx version 0x%04hx \n",
device_info.vendor, device_info.product,
device_info.version);
 
 
char name[256]= "Unknown";
 
if(ioctl(evdev_fd, EVIOCGNAME(sizeof(name)), name) < 0) {
perror("evdev ioctl");
}
 
printf("EVDEV reports name: %s\n", name);
 
/* this macro is used to tell if "bit" is set in "array"
* it selects a byte from the array, and does a boolean AND
* operation with a byte that only has the relevant bit set.
* eg. to check for the 12th bit, we do (array[1] & 1<<4)
*/
 
 
uint8_t evtype_bitmask[EV_MAX/8 + 1];
 
if(ioctl(evdev_fd, EVIOCGBIT(0, sizeof(evtype_bitmask)), evtype_bitmask) < 0)
perror("evdev ioctl");
 
printf("Supported event types:\n");
 
int i;
for (i = 0; i < EV_MAX; i++) {
if (test_bit(i, evtype_bitmask)) {
/* this means that the bit is set in the event types list */
printf(" Event type 0x%02x ", i);
switch ( i)
{
case EV_KEY :
printf(" (Keys or Buttons)\n");
break;
case EV_ABS :
printf(" (Absolute Axes)\n");
break;
case EV_LED :
printf(" (LEDs)\n");
break;
case EV_REP :
printf(" (Repeat)\n");
break;
case EV_SYN :
printf(" (Sync?)\n");
break;
case EV_REL :
printf(" (Relative Axis)\n");
break;
case EV_MSC :
printf(" (Misc)\n");
break;
default:
printf(" (Unknown event type: 0x%04hx)\n", i);
}}}
 
return 1;
}
 
int init_evdevstatus_led()
{
led_event.type=EV_LED;
led_event.code = LED_MISC;
led_event.value = 1;
if (evdev_out_fd)
retval = write(evdev_out_fd, &led_event, sizeof(struct input_event));
return 1;
}
 
int blink_evdev_led()
{
 
if (evdev_out_fd)
{
if (led_event.value)
led_event.value = 0;
else
led_event.value = 1 ;
retval = write(evdev_out_fd, &led_event, sizeof(struct input_event));
}
return 1;
}
 
int poll_evdev()
{
 
struct timeval tv;
 
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(evdev_fd,&rfds);
 
tv.tv_sec = 0;
tv.tv_usec = 5;
retval = select(evdev_fd+1, &rfds, NULL, NULL, &tv);
if (retval==-1)
printf("error in select!!!!!!!!\n");
else if (retval)
{
read_bytes = read(evdev_fd, ev, sizeof(struct input_event) * 64);
if (read_bytes < (int) sizeof(struct input_event)) {
perror("evtest: short read");
exit (1);
}
for (counter = 0; counter < (int) (read_bytes / sizeof(struct input_event)); counter++)
{
printf("%d type:%d code:%d val:%d \n",counter,ev[counter].type,ev[counter].code,ev[counter].value);
if (ev[counter].type==EV_REL) evdev_rel_axis[ ev[counter].code]= ev[counter].value;
if (ev[counter].type==EV_KEY) evdev_button[ ev[counter].code-256]= ev[counter].value;
}
for (counter=0;counter<10;counter++)
printf("A%d %d -" , counter, evdev_rel_axis[counter] );
printf("\n");
for (counter=0;counter<10;counter++)
printf("B%d %d -" , counter, evdev_button[counter] );
// printf("input read done: nick:%d roll:%d gier:%d gas:%d\n",axis[3] , axis[4] , axis[5] , axis[2]);
}
else
printf("no data from evdev data from evdev: \n");
return 1;
}
/Riddim/evdev_handler.h
0,0 → 1,26
#if !defined(EVDEV_HANDLER_H)
#define EVDEV_HANDLER_H
 
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>//for close() for socket
#include <string.h>
#include <linux/joystick.h>
#include <netinet/in.h>
 
 
 
#define test_bit(bit, array) (array[bit/8] & (1<<(bit%8)))
 
int connect_evdev(char* input_evdev_name);
int blink_evdev_led();
int init_evdevstatus_led();
int poll_evdev();
 
extern int *evdev_rel_axis;
extern char *evdev_button;
 
 
#endif
/Riddim/fc.c
4,9 → 4,14
unsigned char TxBuffer[150];
unsigned char _TxBuffer[150];
 
char RxBuffer[150];
char PrintableRxBuffer[150];
 
 
 
int mk_socket;
 
int status;
 
void AddCRC(unsigned int wieviele)
{
55,28 → 60,66
// for (c=0;c<pt+2;c++)
// {
status = write(mk_socket,&TxBuffer , pt+3);
 
// printf("Send to MK %d \n" , TxBuffer[c] );
// }
/*while(TxBuffer[i] !='\r' && i<150)
/* int i=0;
while(TxBuffer[i] !='\r' && i<150)
{
// TxBuffer[i]='#';
status = send(s,&TxBuffer[i] , 1, 0);
status = send(mk_socket,TxBuffer[i] , 1, 0);
printf(" +%d%c ",i,TxBuffer[i]);
i++;
}
 
status = send(s,"\r" , 1, 0);
// status = send(s,"\r" , 1, 0);
*/
// status = send(s,"\r" , 1, 0);
status = send(mk_socket,"\r" , 1, 0);
status = send(mk_socket,"\n" , 1, 0);
printf("\n");
}
 
/*
 
int rx_last_length;
 
int read_from_mk()
{
char in_char='#';
int count=0;
int r=0;
printf("starting read\n");
while(in_char!='\n')
{
// printf("b read\n");
count=read(mk_socket,&in_char,1);
//printf("a read %d\n",count);
if (count!=-1)
{
// printf("%c\n",in_char);
RxBuffer[r]=in_char;
if (in_char!=0)
PrintableRxBuffer[r++]=in_char;
else
PrintableRxBuffer[r++]='0';
}
 
}
rx_last_length=r;
PrintableRxBuffer[r++]='\0'; // terminate
printf("done --->%s\n",PrintableRxBuffer);
 
if (RxBuffer[2]=='D')
debug_sets++;
 
return 1;
}
 
 
int connect_mk_bluetooth(char dest[18])
{
 
 
struct sockaddr_rc addr ;
 
91,13 → 134,14
// connect to server
status = connect(mk_socket, (struct sockaddr *)&addr, sizeof(addr));
 
printf("connection status %d\n",status);
return status;
}
 
*/
 
 
 
int connect_mk_localhost_socket(int port)
{
 
119,6 → 163,7
// status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
status = connect(mk_socket,(struct sockaddr*) &sa, sizeof(struct sockaddr_in));
 
printf("connection status %d\n",status);
return status;
 
}
/Riddim/fc.h
1,11 → 1,13
 
 
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include "bluetooth_handler.h"
#include "statistics.h"
 
 
struct ExternControl_s
{
unsigned char Digital[2]; // (noch unbenutzt)
26,6 → 28,11
 
//int connect_mk_bluetooth(char dest[18]);
int connect_mk_localhost_socket(int port);
int connect_mk_bluetooth(char dest[18]);
 
void SendOutData(unsigned char cmd,unsigned char modul, unsigned char *snd, unsigned char len);
int read_from_mk();
 
extern char RxBuffer[150];
extern char PrintableRxBuffer[150];
extern int rx_last_length;
/Riddim/joy_handler.c
0,0 → 1,34
 
int connect_joy()
{
axis = (int *) calloc( 100, sizeof( int ) );
button = (char *)calloc( 100, sizeof( char ) );
button_trigger = (char *) calloc( 100, sizeof( char ) );
 
// axis = (int *) calloc( num_of_axis, sizeof( int ) );
// button = (char *)calloc( num_of_buttons, sizeof( char ) );
// button_trigger = (char *) calloc( num_of_buttons, sizeof( char ) );
 
if( ( x52_input_fd = open( JOY_DEV, O_RDONLY ) ) < 0 )
{
printf( "Couldn't open joystick device %s\n", JOY_DEV );
printf( "try modprobe joydev\n" );
return 0;
}
 
ioctl( x52_input_fd, JSIOCGAXES, &num_of_axis );
ioctl( x52_input_fd, JSIOCGBUTTONS, &num_of_buttons );
ioctl( x52_input_fd, JSIOCGNAME(80), &name_of_joystick );
 
printf("Joystick detected: %s\n\t%d axis\n\t%d buttons\n\n"
, name_of_joystick
, num_of_axis
, num_of_buttons );
fcntl( x52_input_fd, F_SETFL, O_NONBLOCK ); /* use non-blocking mode */
 
return 1;
}
/Riddim/joy_handler.h
0,0 → 1,0
#include <linux/joystick.h>
/Riddim/riddim.c
7,199 → 7,43
* 2007-2008 Marcus -LiGi- Bueschleb
*
*
**************************************************/
**************************************************/
 
#include "riddim.h"
 
#define FALSE 0
#define TRUE 1
 
#define test_bit(bit, array) (array[bit/8] & (1<<(bit%8)))
 
int state=STATEID_SCANNING;
 
 
// from config
char *input_evdev_name;
int mk_socket_port=0;
int loop_delay=0;
 
 
 
double nick_mul=0.3f;
double roll_mul=0.3f;
double gier_mul=0.3f;
double gas_mul=0.3f;
 
 
int rel_axis_nick=1;
int rel_axis_roll=0;
int rel_axis_gier=5;
int rel_axis_gas=2;
 
 
cfg_bool_t exit_after_init = cfg_false;
int x52_input_fd, num_of_axis=0, num_of_buttons=0;
char *button_trigger=NULL, name_of_joystick[80];
 
// time struct for measuring
struct timeval time_struct1;
struct timeval time_struct2;
 
 
char RxBuffer[150];
 
 
int status;
 
 
 
 
int x52_input_fd, *axis=NULL, num_of_axis=0, num_of_buttons=0, x;
char *button=NULL,*button_trigger=NULL, name_of_joystick[80];
 
struct js_event x52_event_struct;
 
int engines_on=0;
int old_engines_on=0;
 
char in_char;
int *axis;
char *button;
 
 
 
struct x52 *x52_output;
 
int selected_bt_device=0;
 
int yalv; /* loop counter */
size_t read_bytes; /* how many bytes were read */
struct input_event ev[64]; /* the events (up to 64 at once) */
//int evdev_fd;
//int evdev_out_fd;
 
int evdev_fd;
int evdev_out_fd;
 
int evdev_rw=TRUE;
 
int connect_evdev()
{
 
struct input_devinfo {
uint16_t bustype;
uint16_t vendor;
uint16_t product;
uint16_t version;
};
struct input_devinfo device_info;
 
if ((evdev_out_fd = open(input_evdev_name, O_WRONLY)) < 0)
{
printf(" cant open evdev read/write - trying readonly\n");
evdev_rw=FALSE;
}
if ((evdev_fd = open(input_evdev_name, O_RDONLY)) < 0)
{
printf(" cant open evdev !");
return 0;
}
ioctl(evdev_fd,EVIOCGID,&device_info);
printf("vendor 0x%04hx product 0x%04hx version 0x%04hx \n",
device_info.vendor, device_info.product,
device_info.version);
 
 
char name[256]= "Unknown";
 
if(ioctl(evdev_fd, EVIOCGNAME(sizeof(name)), name) < 0) {
perror("evdev ioctl");
}
 
printf("EVDEV reports name: %s\n", name);
 
/* this macro is used to tell if "bit" is set in "array"
* it selects a byte from the array, and does a boolean AND
* operation with a byte that only has the relevant bit set.
* eg. to check for the 12th bit, we do (array[1] & 1<<4)
*/
 
 
uint8_t evtype_bitmask[EV_MAX/8 + 1];
 
if(ioctl(evdev_fd, EVIOCGBIT(0, sizeof(evtype_bitmask)), evtype_bitmask) < 0)
perror("evdev ioctl");
 
printf("Supported event types:\n");
for (yalv = 0; yalv < EV_MAX; yalv++) {
if (test_bit(yalv, evtype_bitmask)) {
/* this means that the bit is set in the event types list */
printf(" Event type 0x%02x ", yalv);
switch ( yalv)
{
case EV_KEY :
printf(" (Keys or Buttons)\n");
break;
case EV_ABS :
printf(" (Absolute Axes)\n");
break;
case EV_LED :
printf(" (LEDs)\n");
break;
case EV_REP :
printf(" (Repeat)\n");
break;
case EV_SYN :
printf(" (Sync?)\n");
break;
case EV_REL :
printf(" (Relative Axis)\n");
break;
case EV_MSC :
printf(" (Misc)\n");
break;
default:
printf(" (Unknown event type: 0x%04hx)\n", yalv);
}}}
 
return 1;
}
 
int connect_joy()
{
axis = (int *) calloc( 100, sizeof( int ) );
button = (char *)calloc( 100, sizeof( char ) );
button_trigger = (char *) calloc( 100, sizeof( char ) );
 
// axis = (int *) calloc( num_of_axis, sizeof( int ) );
// button = (char *)calloc( num_of_buttons, sizeof( char ) );
// button_trigger = (char *) calloc( num_of_buttons, sizeof( char ) );
 
if( ( x52_input_fd = open( JOY_DEV, O_RDONLY ) ) < 0 )
{
printf( "Couldn't open joystick device %s\n", JOY_DEV );
printf( "try modprobe joydev\n" );
return 0;
}
 
ioctl( x52_input_fd, JSIOCGAXES, &num_of_axis );
ioctl( x52_input_fd, JSIOCGBUTTONS, &num_of_buttons );
ioctl( x52_input_fd, JSIOCGNAME(80), &name_of_joystick );
 
printf("Joystick detected: %s\n\t%d axis\n\t%d buttons\n\n"
, name_of_joystick
, num_of_axis
, num_of_buttons );
fcntl( x52_input_fd, F_SETFL, O_NONBLOCK ); /* use non-blocking mode */
 
return 1;
}
 
 
 
 
 
 
 
void write_display(int line,char* text)
{
if (x52_output) x52_settext(x52_output, line , text, strlen(text));
240,7 → 84,7
}
 
 
int r=0;
 
int count=0;
int connected=0;
 
255,82 → 99,67
printf("\tRemote Interactive Digital Drone Interface Mashup\n");
printf("\nusage:\n");
printf("\t riddim [config_file]\n\n");
 
cfg_opt_t opts[] = {
// bt_host_init();
 
CFG_SIMPLE_BOOL("exit_after_init", &exit_after_init),
CFG_SIMPLE_STR("input_evdev", &input_evdev_name),
CFG_SIMPLE_INT("loop_delay", &loop_delay),
CFG_SIMPLE_INT("mk_socket_port", &mk_socket_port),
 
CFG_SIMPLE_FLOAT("nick_mul", &nick_mul),
CFG_SIMPLE_FLOAT("roll_mul", &roll_mul),
CFG_SIMPLE_FLOAT("gier_mul", &gier_mul),
CFG_SIMPLE_FLOAT("gas_mul", &gas_mul),
CFG_SIMPLE_INT("rel_axis_nick", &rel_axis_nick),
CFG_SIMPLE_INT("rel_axis_roll", &rel_axis_roll),
CFG_SIMPLE_INT("rel_axis_gier", &rel_axis_gier),
CFG_SIMPLE_INT("rel_axis_gas", &rel_axis_gas),
CFG_END()
};
cfg_t *cfg;
if (argv[1])
parse_config(argv[1]);
else
parse_config("/etc/riddim.conf");
 
// input_evdev_name=strdup("/dev/event0");
printf("Parsing config file ");
cfg = cfg_init(opts, 0);
 
if (argv[1])
 
printf("input %s:\n",input_evdev_name);
/*
if (bluetooth_mac)
{
printf("%s\n ",argv[1]);
cfg_parse(cfg, argv[1]);
 
printf("Connecting via Bluetooth to %s\n",bluetooth_mac);
if (connect_mk_bluetooth(bluetooth_mac));;
connected=TRUE;
}
else
{
printf("/etc/riddim.conf\n");
cfg_parse(cfg, "/etc/riddim.conf");
}
printf("input %s:\n",input_evdev_name);
 
// 1st argument -> Bluetooth adrees to bypass scanning ( takes to long for short testing roundtrips )
 
*/
if (mk_socket_port)
{
printf("connecting to local port: %i\n",mk_socket_port);
printf("connecting to local port: %i\n",mk_socket_port);
if (connect_mk_localhost_socket(mk_socket_port)==-1)
printf("cant connect !!");
else
{
printf("connected to QC at adress: %s\n",argv[1]);
printf("connected !-)");
connected=TRUE;
}
}
 
int i;
// todo reenable bluetooth connection
 
/*
printf("\nInitializing X-52 input ..\n");
if (connect_joy())
{
printf(".. done");//
input=INPUT_JOYDEV;
printf(".. done");//
input=INPUT_JOYDEV;
}
else
printf(".. ERROR ");//
*/
 
printf("\nInitializing evdev input (%s) ..\n",input_evdev_name);
if (connect_evdev())
if (input_evdev_name)
{
printf(".. done");//
input=INPUT_EVDEV;
printf("\nInitializing evdev input (%s) ..\n",input_evdev_name);
if (connect_evdev(input_evdev_name))
{
printf(".. done");//
input=INPUT_EVDEV;
}
else
printf(".. ERROR ");//
}
else
printf(".. ERROR ");//
 
printf("\nInitializing X-52 output ..");
 
x52_output = x52_init();
346,49 → 175,42
printf(" not found \n");
 
/*
if (!connected)
if (!connected)
{
printf("Scanning for Bluetooth Devices ..\n");
write_display(1,"Bluetooth Scan");
scan_bt();
printf(" done \n");
printf(" %d Devices found \n",bt_device_count);
print_device_list() ;
printf("Scanning for Bluetooth Devices ..\n");
write_display(1,"Bluetooth Scan");
scan_bt();
printf(" done \n");
printf(" %d Devices found \n",bt_device_count);
print_device_list() ;
}
*/
 
int v_old;
// int v_old;
int polls=0;
 
int retval;
 
if (exit_after_init)
exit(0);
printf("starting loop ..\n");
 
struct input_event led_event;
led_event.type=EV_LED;
 
 
int complete_misses=0;
if (evdev_out_fd)
{
led_event.code = LED_MISC;
led_event.value = 1;
retval = write(evdev_out_fd, &led_event, sizeof(struct input_event));
}
int complete_matches=0;
 
 
int confirm_misses;
while( 1 )
 
 
 
init_evdevstatus_led();
 
while( TRUE )
{
 
if (evdev_out_fd)
{
if (led_event.value)
led_event.value = 0;
else
led_event.value = 1 ;
retval = write(evdev_out_fd, &led_event, sizeof(struct input_event));
}
 
blink_evdev_led();
// bt_host_tick(mk_socket);
usleep(loop_delay);
switch (input)
{
399,52 → 221,13
break;
 
case INPUT_EVDEV:
printf("input evdev\n");
printf("input evdev\n");
 
struct timeval tv;
int retval;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(evdev_fd,&rfds);
poll_evdev();
break;
 
tv.tv_sec = 0;
tv.tv_usec = 5;
 
retval = select(evdev_fd+1, &rfds, NULL, NULL, &tv);
 
if (retval==-1)
printf("error in select!!!!!!!!\n");
else if (retval)
{
read_bytes = read(evdev_fd, ev, sizeof(struct input_event) * 64);
 
if (read_bytes < (int) sizeof(struct input_event)) {
perror("evtest: short read");
exit (1);
}
for (yalv = 0; yalv < (int) (read_bytes / sizeof(struct input_event)); yalv++)
{
 
printf("%d type:%d code:%d val:%d \n",yalv,ev[yalv].type,ev[yalv].code,ev[yalv].value);
if (ev[yalv].type==EV_REL) axis[ ev[yalv].code]= ev[yalv].value;
if (ev[yalv].type==EV_KEY) button[ ev[yalv].code-256]= ev[yalv].value;
}
 
 
for (yalv=0;yalv<10;yalv++)
printf("A%d %d -" , yalv, axis[yalv] );
printf("\n");
 
for (yalv=0;yalv<10;yalv++)
printf("B%d %d -" , yalv, button[yalv] );
// printf("input read done: nick:%d roll:%d gier:%d gas:%d\n",axis[3] , axis[4] , axis[5] , axis[2]);
}
else
printf("no data from evdev data from evdev: \n");
 
break;
 
case INPUT_JOYDEV:
printf("input joydev\n");
// poll values from input device
465,7 → 248,7
break;
}
}
int x;
for( x=0 ; x<num_of_buttons ; ++x )
if( button[x]==0)
button_trigger[x]=0;
483,9 → 266,9
 
case STATEID_SCANNING:
 
state=STATEID_CONNECTING;
 
/*
ExternControl.Digital[0]=0;
ExternControl.Digital[1]=0;
ExternControl.RemoteTasten=0;
526,6 → 309,7
 
}
*/
break;
case STATEID_CONNECTING:
533,42 → 317,37
 
confirm_misses=0;
printf("connected:%d",connected);
if (connected) while (RxBuffer[1]!='t')
RxBuffer[1]=0;
if (connected)
 
 
while (RxBuffer[1]!='t')
{
 
RxBuffer[1]=0;
read_from_mk();
// bt_host_send(RxBuffer,rx_last_length);
printf("sending to host: %s",PrintableRxBuffer);
 
 
// ftime(&time_struct);
printf("waiting for confirm frame ( misses:%d )\n",confirm_misses);
 
printf("waiting for confirm frame ( confirmed:%d misses:%d )\n",complete_matches,complete_misses);
RxBuffer[2]=0;
 
 
 
r=0;
in_char='#';
// r=0;
 
while(in_char!='\n')
{
count=read(mk_socket,&in_char,1);
if (in_char!=0)
{
RxBuffer[r++]=in_char;
}
else
{
RxBuffer[r++]='0';
}
// printf("\ncount:%d r:%d %d %c \n",count , r, in_char, in_char);
}
RxBuffer[r++]='\0';
printf("%d--->%s\n",complete_misses,RxBuffer);
// new
/*
if (button_trigger[12]>1)
{
SendOutData('s', 0, (unsigned char *)&ExternControl, sizeof(ExternControl));
button_trigger[12]=0;
}
*/
ExternControl.Frame='t';
if (++confirm_misses>4)
{
complete_misses++;
578,57 → 357,58
}
else
printf("not connected to mk\n");
 
gettimeofday(&time_struct2,NULL);
 
printf("last trip: %d",(int)(time_struct1.tv_usec-time_struct2.tv_usec));
act_mode=button[24] | (button[25]<<1);
// act_mode=button[24] | (button[25]<<1);
 
 
 
// Step converting axis data to nick/roll/gier/gas/..
 
act_nick=axis[rel_axis_nick]*nick_mul;
act_roll=axis[rel_axis_roll]*roll_mul;
act_gier=axis[rel_axis_gier]*gier_mul;
act_gas=axis[rel_axis_gas]*gas_mul;
act_nick=evdev_rel_axis[rel_axis_nick]*nick_mul;
act_roll=evdev_rel_axis[rel_axis_roll]*roll_mul;
act_gier=evdev_rel_axis[rel_axis_gier]*gier_mul;
act_gas=evdev_rel_axis[rel_axis_gas]*gas_mul;
act_gas=255;
 
 
/*
switch (act_mode)
switch (act_mode)
{
case 0:
 
 
act_nick=(axis[AXIS_NICK])*(INVERT_NICK);
act_roll=(axis[AXIS_ROLL])*(INVERT_ROLL);
act_gier=(axis[AXIS_GIER])*(INVERT_GIER);
act_gas=((axis[AXIS_GAS])-128)*(-1);
act_nick=(axis[AXIS_NICK])*(INVERT_NICK);
act_roll=(axis[AXIS_ROLL])*(INVERT_ROLL);
act_gier=(axis[AXIS_GIER])*(INVERT_GIER);
act_gas=((axis[AXIS_GAS])-128)*(-1);
// clip gas
if (act_gas<0) act_gas=0;
// clip gas
if (act_gas<0) act_gas=0;
 
if (act_gas>250) act_gas=250;
if (act_gas>250) act_gas=250;
 
//////// act_gas=0;
break;
//////// act_gas=0;
break;
 
case 1:
act_nick=(axis[AXIS_NICK]>>8)*(INVERT_NICK)/2;
act_roll=(axis[AXIS_ROLL]>>8)*(INVERT_ROLL)/2;
act_gier=(axis[AXIS_GIER]>>8)*(INVERT_GIER)/2;
act_gas=(axis[AXIS_GAS]>>8)*(INVERT_GAS);
act_nick=(axis[AXIS_NICK]>>8)*(INVERT_NICK)/2;
act_roll=(axis[AXIS_ROLL]>>8)*(INVERT_ROLL)/2;
act_gier=(axis[AXIS_GIER]>>8)*(INVERT_GIER)/2;
act_gas=(axis[AXIS_GAS]>>8)*(INVERT_GAS);
 
break;
break;
case 2:
act_nick=(axis[AXIS_NICK]>>8)*(INVERT_NICK)/3;
act_roll=(axis[AXIS_ROLL]>>8)*(INVERT_ROLL)/3;
act_gier=(axis[AXIS_GIER]>>8)*(INVERT_GIER)/3;
act_gas=(axis[AXIS_GAS]>>8)*(INVERT_GAS);
act_nick=(axis[AXIS_NICK]>>8)*(INVERT_NICK)/3;
act_roll=(axis[AXIS_ROLL]>>8)*(INVERT_ROLL)/3;
act_gier=(axis[AXIS_GIER]>>8)*(INVERT_GIER)/3;
act_gas=(axis[AXIS_GAS]>>8)*(INVERT_GAS);
 
 
break;
break;
 
}
*/
635,23 → 415,21
ExternControl.Digital[0]=0;
ExternControl.Digital[1]=0;
ExternControl.RemoteTasten=0;
ExternControl.Higt=0;
ExternControl.free=0;
ExternControl.Frame='t';
ExternControl.Config=1;
 
 
ExternControl.Nick=act_nick; //(axis[1]>>8)*(-1)/2;
// printf("nick%d\n",ExternControl.Nick);
ExternControl.Roll=act_roll*(-1); //(axis[0]>>8)*(-1)/2;
ExternControl.Gier=(axis[5]>>8);
ExternControl.Gier=act_gier; // ************
ExternControl.Gas=(axis[2]>>8)*(-1)+127;
ExternControl.Gas=act_gas; // ************
// ExternControl.Gas=0; // ************
ExternControl.Higt=0;
ExternControl.free=0;
ExternControl.Frame='t';
ExternControl.Config=1;
complete_matches++;
 
printf("---+++act_mode %d , act_nick %d , act_roll %d , act_gier %d , act_gas %d",act_mode , act_nick , act_roll , act_gier , act_gas);
 
 
 
if (connected)
{
printf("sending data\n");
661,30 → 439,32
printf("sent data\n");
}
// printf("sleeping\n");
// for (polls=0;polls<100;polls++) // FIXME - better Polling
// printf("end_sleep\n");
// printf("sleeping\n");
// for (polls=0;polls<100;polls++) // FIXME - better Polling
// printf("end_sleep\n");
int v=axis[6]/655+50;
if (v!=v_old)if (x52_output) x52_setbri(x52_output, 0,v );
v_old=v;
// int v=axis[6]/655+50;
// if (v!=v_old)if (x52_output) x52_setbri(x52_output, 0,v );
// v_old=v;
printf("v: %d \n",v);
// printf("v: %d \n",v);
for (i=0;i<num_of_axis;i++)
/*
for (i=0;i<num_of_axis;i++)
printf("A%d: %d ", i,axis[i]>>8 );
for( x=0 ; x<num_of_buttons ; ++x )
for( x=0 ; x<num_of_buttons ; ++x )
printf("B%d: %d ", x, button[x] );
*/
 
break;
}
printf("\n");
fflush(stdout);
printf("loop fin ( misses:%d)\n",complete_misses);
printf("loop fin ( confirmed:%d misses:%d | debug_sets:%d )\n",complete_matches,complete_misses,debug_sets);
}
 
/Riddim/riddim.conf
2,12 → 2,19
 
input_evdev = "/dev/input/event10"
mk_socket_port = 54321
loop_delay=20000
#loop_delay=20000
 
loop_delay=90000
 
 
#loop_delay=50000
#loop_delay=500000
 
# to check how the system was inited
exit_after_init=false
#exit_after_init=true
 
#bluetooth_mac="00:0B:CE:01:2E:00"
gas_mul=0.3
rel_axis_gas=2
nick_mul=-0.3
/Riddim/riddim.h
13,9 → 13,15
#include <confuse.h>
#include <string.h>
 
 
// for configuration file parsing
#include "config.h"
// for measuring
#include "statistics.h"
// for understanding the FC
#include "fc.h"
#include "bluetooth_handler.h"
#include "evdev_handler.h"
 
#include <stdio.h>
#include <stdlib.h>
28,15 → 34,13
 
 
#include <sys/time.h>
#include <bluetooth/rfcomm.h>
 
 
#include <linux/joystick.h>
 
 
 
 
#include <errno.h>
 
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>//for close() for socket
48,6 → 52,11
int act_gas=0;
int act_mode=0;
 
 
 
#define FALSE 0
#define TRUE 1
 
#define JOY_DEV "/dev/input/js0"
 
 
56,6 → 65,7
#define STATEID_SCANNING 0
#define STATEID_CONNECTING 1
 
 
#define BUTTON_SELECT 26
#define BUTTON_DOWN 28
#define BUTTON_UP 27
/Riddim/statistics.c
0,0 → 1,10
#include "statistics.h"
 
struct timeval time_struct1;
struct timeval time_struct2;
 
 
int debug_sets=0;
int confirm_sets=0;
int version_sets=0;
 
/Riddim/statistics.h
0,0 → 1,12
#include <sys/time.h>
 
// time struct for measuring
extern struct timeval time_struct1;
extern struct timeval time_struct2;
 
 
extern int debug_sets;
extern int confirm_sets;
extern int version_sets;