diff --git a/src/monocoque/devices/serialdevice.c b/src/monocoque/devices/serialdevice.c index d607396..318b333 100644 --- a/src/monocoque/devices/serialdevice.c +++ b/src/monocoque/devices/serialdevice.c @@ -70,6 +70,91 @@ int arduino_shiftlights_update(SimDevice* this, SimData* simdata) return result; } +int arduino_simled_update(SimDevice* this, SimData* simdata) +{ + SerialDevice* serialdevice = (void *) this->derived; + int result = 1; + + int total_leds = serialdevice->numleds; + size_t bufsize = (total_leds * 3) + 14; + char bytes[bufsize]; + int endled = serialdevice->endled; + int startled = serialdevice->startled; + if(endled == 0) + { + endled = total_leds; + } + int num_avail_leds = endled - startled + 1; + int rpm = simdata->rpms; + int maxrpm = simdata->maxrpm; + int litleds = 0; + if(rpm > 0 && maxrpm > 0) + { + int rpmmargin = ceil(.05*maxrpm); + int rpminterval = (maxrpm-rpmmargin) / (num_avail_leds); + + + for (int l = 1; l <= (num_avail_leds); l++) + { + if(rpm >= (rpminterval * l)) + { + litleds = l; + } + } + + + + for(int j = 0; j < bufsize; j++) + { + bytes[j] = 0x00; + } + bytes[0] = 0xff; + bytes[1] = 0xff; + bytes[2] = 0xff; + bytes[3] = 0xff; + bytes[4] = 0xff; + bytes[5] = 0xff; + bytes[6] = 0x73; + bytes[7] = 0x6c; + bytes[8] = 0x65; + bytes[9] = 0x64; + bytes[10] = 0x73; + bytes[bufsize-1] = 0xfd; + bytes[bufsize-2] = 0xfe; + bytes[bufsize-3] = 0xff; + + for(int i = 0; i < litleds; i++) + { + if(i < ((num_avail_leds) / 2)) + { + //green + bytes[11 + ((i + startled - 1) * 3) + 1] = 0xff; + } + else + { + if(i < num_avail_leds - 1) + { + //yellow + bytes[11 + ((i + startled - 1) * 3) + 0] = 0xff; + bytes[11 + ((i + startled - 1) * 3) + 1] = 0xff; + } + } + if(i == num_avail_leds - 1) + { + //red + bytes[11 + ((i + startled - 1) * 3) + 0] = 0xff; + } + } + } + + slogt("Updating arduino device lights to %i", litleds); + // we can add configs to set all the colors + size_t size = sizeof(bytes); + + arduino_update(serialdevice, &bytes, size); + return result; +} + int arduino_simwind_update(SimDevice* this, SimData* simdata) { SerialDevice* serialdevice = (void *) this->derived; @@ -201,6 +286,7 @@ int serialdev_init(SerialDevice* serialdevice, DeviceSettings* ds) static const vtable serial_simdevice_vtable = { &serialdev_update, &serialdev_free }; static const vtable arduino_shiftlights_vtable = { &arduino_shiftlights_update, &serialdev_free }; +static const vtable arduino_simled_vtable = { &arduino_simled_update, &serialdev_free }; static const vtable arduino_simwind_vtable = { &arduino_simwind_update, &serialdev_free }; static const vtable arduino_simhaptic_vtable = { &arduino_simhaptic_update, &serialdev_free }; static const vtable serialwheel_vtable = { &serial_wheel_update, &serial_wheel_free }; @@ -223,6 +309,14 @@ SerialDevice* new_serial_device(DeviceSettings* ds, MonocoqueSettings* ms) { this->m.vtable = &arduino_shiftlights_vtable; slogi("Initializing arduino device for shiftlights."); break; + case (SIMDEVTYPE_SIMLED): + this->devicetype = ARDUINODEV__SIMLED; + this->numleds = ds->serialdevsettings.numleds; + this->startled = ds->serialdevsettings.startled; + this->endled = ds->serialdevsettings.endled; + this->m.vtable = &arduino_simled_vtable; + slogi("Initializing arduino device for simled."); + break; case (SIMDEVTYPE_SIMWIND): this->devicetype = ARDUINODEV__SIMWIND; this->m.vtable = &arduino_simwind_vtable; diff --git a/src/monocoque/devices/serialdevice.h b/src/monocoque/devices/serialdevice.h index af7cbe1..7137bfd 100644 --- a/src/monocoque/devices/serialdevice.h +++ b/src/monocoque/devices/serialdevice.h @@ -10,6 +10,7 @@ typedef enum ARDUINODEV__SIMWIND = 1, ARDUINODEV__HAPTIC = 2, SERIALDEV__MOZAR5 = 3, + ARDUINODEV__SIMLED = 4 } SerialDeviceType; diff --git a/src/monocoque/devices/simdevice.h b/src/monocoque/devices/simdevice.h index 017a60b..6019f94 100644 --- a/src/monocoque/devices/simdevice.h +++ b/src/monocoque/devices/simdevice.h @@ -53,6 +53,9 @@ typedef struct // move these two they only apply to the haptic device int motorsposition; int numlights; + int numleds; + int startled; + int endled; int baudrate; double ampfactor; double state; diff --git a/src/monocoque/helper/confighelper.c b/src/monocoque/helper/confighelper.c index d0424cb..7d37df8 100644 --- a/src/monocoque/helper/confighelper.c +++ b/src/monocoque/helper/confighelper.c @@ -145,6 +145,11 @@ int strtodevsubtype(const char* device_subtype, DeviceSettings* ds, int simdev) ds->dev_subtype = SIMDEVTYPE_SHIFTLIGHTS; break; } + if (strcicmp(device_subtype, "Simleds") == 0) + { + ds->dev_subtype = SIMDEVTYPE_SIMLED; + break; + } if (strcicmp(device_subtype, "SimWind") == 0) { ds->dev_subtype = SIMDEVTYPE_SIMWIND; @@ -748,6 +753,18 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co config_setting_lookup_int(device_settings, "numlights", &numlights); ds->serialdevsettings.numlights = numlights; + int numleds = 6; + config_setting_lookup_int(device_settings, "numleds", &numleds); + ds->serialdevsettings.numleds = numleds; + + int startled = 1; + config_setting_lookup_int(device_settings, "startled", &startled); + ds->serialdevsettings.startled = startled; + + int endled = 1; + config_setting_lookup_int(device_settings, "endled", &endled); + ds->serialdevsettings.endled = endled; + int baud = 9600; config_setting_lookup_int(device_settings, "baud", &baud); ds->serialdevsettings.baud = baud; @@ -827,7 +844,7 @@ int uiloadconfig(const char* config_file_str, int confignum, int configureddevic int settingsfree(DeviceSettings ds) { - if (ds.dev_subtype == SIMDEVTYPE_SIMWIND || ds.dev_subtype == SIMDEVTYPE_SHIFTLIGHTS) + if (ds.dev_subtype == SIMDEVTYPE_SIMWIND || ds.dev_subtype == SIMDEVTYPE_SHIFTLIGHTS || ds.dev_subtype == SIMDEVTYPE_SIMLED) { if (ds.serialdevsettings.portdev != NULL) { diff --git a/src/monocoque/helper/confighelper.h b/src/monocoque/helper/confighelper.h index 22cf3f2..214d8bc 100644 --- a/src/monocoque/helper/confighelper.h +++ b/src/monocoque/helper/confighelper.h @@ -29,7 +29,8 @@ typedef enum SIMDEVTYPE_SIMWIND = 4, SIMDEVTYPE_SERIALHAPTIC = 5, SIMDEVTYPE_USBWHEEL = 6, - SIMDEVTYPE_SERIALWHEEL = 7 + SIMDEVTYPE_SERIALWHEEL = 7, + SIMDEVTYPE_SIMLED = 8 } DeviceSubType; @@ -155,6 +156,9 @@ typedef struct char* portdev; MotorPosition motorsposition; int numlights; + int numleds; + int startled; + int endled; float ampfactor; int baud; } diff --git a/tests/simlighttest.c b/tests/simlighttest.c index 7532a46..7d09b3b 100644 --- a/tests/simlighttest.c +++ b/tests/simlighttest.c @@ -10,7 +10,7 @@ int check(enum sp_return result); int main() { - char* port_name = "/dev/ttyACM0"; + char* port_name = "/dev/simdev0"; /* The ports we will use. */ struct sp_port* port; @@ -31,25 +31,79 @@ int main() ShiftLightsData sd; - int numlights = 7; - size_t bufsize = (numlights * 3) + 9; - char bytes[bufsize]; + + size_t bufsize1 = 11; + size_t recv_bufsize1 = 5; + char recv_buf1[recv_bufsize1]; + char bytes1[bufsize1]; + + for(int j = 0; j < bufsize1; j++) + { + bytes1[j] = 0x00; + } + bytes1[0] = 0xff; + bytes1[1] = 0xff; + bytes1[2] = 0xff; + bytes1[3] = 0xff; + bytes1[4] = 0xff; + bytes1[5] = 0xff; + bytes1[6] = 0x6c; + bytes1[7] = 0x65; + bytes1[8] = 0x64; + bytes1[9] = 0x73; + bytes1[10] = 0x63; + + int result = 0; + unsigned int timeout = 2000; + result = check(sp_blocking_write(port, &bytes1, bufsize1, timeout)); + result = check(sp_blocking_read(port, &recv_buf1, recv_bufsize1, timeout)); + + char numstr[4]; + for(int j = 0; j < 4; j++) + { + numstr[j] = '\0'; + } + for(int j = 0; j < recv_bufsize1; j++) + { + if(recv_buf1[j] != 0 && recv_buf1[j] != 0x0d && recv_buf1[j] != 0x0a) + { + numstr[j] = recv_buf1[j]; + } + } + int numlights = atoi(numstr); + printf("numlights is %i\n", numlights); + //printf("%x\n", recv_buf[0]); + //printf("%x\n", recv_buf[1]); + //printf("%x\n", recv_buf[2]); + //printf("%x\n", recv_buf[3]); + //printf("%x\n", recv_buf[4]); + + size_t bufsize = (numlights * 3) + 14; + char bytes[bufsize]; + for(int j = 0; j < bufsize; j++) + { + bytes[j] = 0x00; + } bytes[0] = 0xff; bytes[1] = 0xff; bytes[2] = 0xff; bytes[3] = 0xff; bytes[4] = 0xff; bytes[5] = 0xff; + bytes[6] = 0x73; + bytes[7] = 0x6c; + bytes[8] = 0x65; + bytes[9] = 0x64; + bytes[10] = 0x73; bytes[bufsize-1] = 0xfd; bytes[bufsize-2] = 0xfe; bytes[bufsize-3] = 0xff; - int result; - unsigned int timeout = 2000; - for( int i = 0; i < 7; i++) + + for( int i = 0; i < numlights; i++) { - bytes[(i * 3) + 6 + 1] = 0xff; + bytes[(i * 3) + 11 + 1] = 0xff; /* Send data. */ result = check(sp_blocking_write(port, &bytes, bufsize, timeout)); @@ -67,7 +121,10 @@ int main() sleep(2); } - sd.litleds = 0; + for( int i = 0; i < numlights; i++) + { + bytes[(i * 3) + 11 + 1] = 0x00; + } result = check(sp_blocking_write(port, &bytes, bufsize, timeout)); check(sp_close(port));