summaryrefslogtreecommitdiff
path: root/tests/test2/test2.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test2/test2.c')
-rw-r--r--tests/test2/test2.c151
1 files changed, 151 insertions, 0 deletions
diff --git a/tests/test2/test2.c b/tests/test2/test2.c
new file mode 100644
index 0000000..3b20afd
--- /dev/null
+++ b/tests/test2/test2.c
@@ -0,0 +1,151 @@
+#include <ftdi.h>
+#include <time.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+// FTDI Vendor ID
+#define VID 0x0403
+// FTDI Product ID
+#define PID 0x6001
+
+#define DMX_CHANNELS 512
+// One "official" DMX frame can take (1s/44Hz) = 22727272ns
+#define DMX_FRAMETIME 22727272
+#define DMX_BREAK 110000
+#define DMX_MAB 16000
+
+#define MAX_STR 64
+
+inline void ts_diffadd(struct timespec *res, struct timespec *a, struct timespec *b, struct timespec *c) {
+ time_t rem;
+
+ res->tv_nsec=a->tv_nsec-b->tv_nsec+c->tv_nsec;
+ rem=res->tv_nsec/1000000000;
+ res->tv_nsec=res->tv_nsec%1000000000;
+ res->tv_sec=a->tv_sec-b->tv_sec+c->tv_sec+rem;
+
+ if (res->tv_nsec<0 && res->tv_sec>0) res->tv_sec--; res->tv_nsec+=1000000000;
+ if (res->tv_nsec>0 && res->tv_sec<0) res->tv_nsec-=1000000000; res->tv_sec++;
+}
+
+void e(int res, const char *funcname, struct ftdi_context *ftdi, int exit_val) {
+ char *reason;
+
+ if ( res < 0 ) {
+ reason=ftdi_get_error_string(ftdi);
+ fprintf(stderr, "Error in %s : %s\n", funcname, reason);
+ exit(exit_val);
+ }
+
+}
+
+/*
+struct ftdi_device_list
+{
+ // pointer to next entry
+ struct ftdi_device_list *next;
+ // pointer to libusb's usb_device
+ struct usb_device *dev;
+}
+*/
+
+int main() {
+ int res;
+
+ char *manufacturer, *description, *serial;
+ char universe[DMX_CHANNELS+1];
+
+ struct ftdi_context ftdi;
+ struct ftdi_device_list *devlist=NULL;
+ struct usb_device *dev=NULL;
+
+ struct timespec ts_frame_time, ts_dmx_break, ts_dmx_mab, ts_to_sleep, ts_trame_begin, ts_now;
+
+ ts_frame_time.tv_sec=0;
+ ts_frame_time.tv_nsec=DMX_FRAMETIME;
+ ts_dmx_break.tv_sec=0;
+ ts_dmx_break.tv_nsec=DMX_BREAK;
+ ts_dmx_mab.tv_sec=0;
+ ts_dmx_mab.tv_nsec=DMX_MAB;
+
+ (void) memset(universe, 0, DMX_CHANNELS);
+ (void) memset(&ftdi, 0, sizeof(ftdi));
+ universe[DMX_CHANNELS]=0;
+
+ manufacturer=malloc(MAX_STR);
+ description=malloc(MAX_STR);
+ serial=malloc(MAX_STR);
+
+ e(ftdi_init(&ftdi),"ftdi_init",&ftdi,10);
+ e(ftdi_usb_find_all(&ftdi, &devlist, VID, PID),"ftdi_usb_find_all",&ftdi,11);
+
+ //Always take the first device
+ if (devlist==NULL || devlist->dev==NULL) {
+ fprintf(stderr, "No usb device detected (looking for 0x%04X:0x%04X)\n", VID, PID);
+ exit(2);
+ }
+ dev=devlist->dev;
+
+// fprintf(stderr, "DEBUG : next==%p\n", devlist->next);
+
+ e(ftdi_usb_get_strings(&ftdi, dev, manufacturer, MAX_STR, description, MAX_STR, serial, MAX_STR),"ftdi_usb_get_strings",&ftdi,12);
+ fprintf(stderr, "Connecting to USB device ('%s','%s','%s')\n", manufacturer, description, serial);
+ e(ftdi_usb_open_dev(&ftdi,dev),"ftdi_usb_open_dev",&ftdi,13);
+ fprintf(stderr, "Connected\n");
+
+ //Init the device
+ e(ftdi_usb_reset(&ftdi),"ftdi_usb_reset",&ftdi,15);
+ e(ftdi_set_baudrate(&ftdi, 250000),"ftdi_set_baudrate",&ftdi,16);
+ e(ftdi_set_line_property(&ftdi, BITS_8, STOP_BIT_2, NONE),"ftdi_set_line_property",&ftdi,17);
+ e(ftdi_setflowctrl(&ftdi, SIO_DISABLE_FLOW_CTRL),"ftdi_setflowctrl",&ftdi,18);
+ e(ftdi_usb_purge_buffers(&ftdi),"ftdi_usb_purge_buffers",&ftdi,19);
+ e(ftdi_setrts(&ftdi, 0),"ftdi_setrts",&ftdi,20);
+
+ // 1ms pause for initialization completion
+ ts_to_sleep.tv_sec=0;
+ ts_to_sleep.tv_nsec=1000000;
+ nanosleep(&ts_to_sleep,NULL);
+
+ ts_trame_begin.tv_sec=0;
+ ts_trame_begin.tv_nsec=0;
+
+ // Wait the end of the previous timeslot
+ clock_gettime(CLOCK_MONOTONIC, &ts_now);
+ ts_diffadd(&ts_to_sleep,&ts_frame_time,&ts_now,&ts_trame_begin);
+ nanosleep(&ts_to_sleep,NULL);
+
+ clock_gettime(CLOCK_MONOTONIC, &ts_trame_begin);
+
+ e(ftdi_set_line_property2(&ftdi, BITS_8, STOP_BIT_2, NONE, BREAK_ON),"ftdi_set_line_property2",&ftdi,30);
+ nanosleep(&ts_dmx_break,NULL);
+ e(ftdi_set_line_property2(&ftdi, BITS_8, STOP_BIT_2, NONE, BREAK_OFF),"ftdi_set_line_property2",&ftdi,31);
+ nanosleep(&ts_dmx_mab,NULL);
+
+
+
+ // Tests
+ universe[2]=120;
+ universe[3]=120;
+ universe[4]=120;
+
+
+ //e(ftdi_write_data(&ftdi, universe, DMX_CHANNELS),"ftdi_write_data",&ftdi,32);
+ res=ftdi_write_data(&ftdi, universe, DMX_CHANNELS);
+
+ //fprintf(stderr, "DEBUG : to_sleep=={%li,%li}\n", ts_to_sleep.tv_sec, ts_to_sleep.tv_nsec);
+ fprintf(stderr, "DEBUG : res==%i\n", res);
+// fprintf(stderr, "DEBUG : universe==%s\n", universe);
+
+ e(ftdi_usb_close(&ftdi),"ftdi_usb_close",&ftdi,50);
+ ftdi_list_free(&devlist);
+ ftdi_deinit(&ftdi);
+
+
+ free(manufacturer);
+ free(description);
+ free(serial);
+
+ exit(0);
+}
+