|
| 1 | +/*-----------------------------------------*\ |
| 2 | +| DygmaRaiseController.cpp | |
| 3 | +| | |
| 4 | +| Driver for Dygma Raise keyboard | |
| 5 | +| | |
| 6 | +| Timo Schlegel (@eispalast) 12/12/2021 | |
| 7 | +\*-----------------------------------------*/ |
| 8 | + |
| 9 | +#include "DygmaRaiseController.h" |
| 10 | + |
| 11 | +using namespace std::chrono_literals; |
| 12 | + |
| 13 | +static int val_char_len(int number) |
| 14 | +{ |
| 15 | + if(number < 10) |
| 16 | + { |
| 17 | + return 1; |
| 18 | + } |
| 19 | + else if |
| 20 | + (number < 100) |
| 21 | + { |
| 22 | + return 2; |
| 23 | + } |
| 24 | + else |
| 25 | + { |
| 26 | + return 3; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +DygmaRaiseController::DygmaRaiseController() |
| 31 | +{ |
| 32 | + |
| 33 | +} |
| 34 | + |
| 35 | +DygmaRaiseController::~DygmaRaiseController() |
| 36 | +{ |
| 37 | + serialport->serial_close(); |
| 38 | + delete serialport; |
| 39 | +} |
| 40 | + |
| 41 | +void DygmaRaiseController::Initialize(char* port) |
| 42 | +{ |
| 43 | + port_name = port; |
| 44 | + |
| 45 | + serialport = new serial_port(port_name.c_str(), DYGMA_RAISE_BAUD); |
| 46 | +} |
| 47 | + |
| 48 | +std::string DygmaRaiseController::GetDeviceLocation() |
| 49 | +{ |
| 50 | + return("COM: " + port_name); |
| 51 | +} |
| 52 | + |
| 53 | +void DygmaRaiseController::SendDirect(std::vector<RGBColor>colors, size_t led_num) |
| 54 | +{ |
| 55 | + char serial_buf[MAX_LEN]; |
| 56 | + |
| 57 | + /*-----------------------------------------------------*\ |
| 58 | + | Zero out buffer | |
| 59 | + \*-----------------------------------------------------*/ |
| 60 | + memset(serial_buf,0x00,sizeof(serial_buf)); |
| 61 | + |
| 62 | + /*-----------------------------------------------------*\ |
| 63 | + | Set up led theme packet | |
| 64 | + \*-----------------------------------------------------*/ |
| 65 | + sprintf(serial_buf,"led.theme"); |
| 66 | + int actual_length=9; |
| 67 | + |
| 68 | + /*-----------------------------------------------------*\ |
| 69 | + | Fill packet with color values | |
| 70 | + \*-----------------------------------------------------*/ |
| 71 | + for(std::size_t led_idx = 0; led_idx < led_num; led_idx++) |
| 72 | + { |
| 73 | + int r = RGBGetRValue(colors[led_idx]); |
| 74 | + int g = RGBGetGValue(colors[led_idx]); |
| 75 | + int b = RGBGetBValue(colors[led_idx]); |
| 76 | + |
| 77 | + sprintf(serial_buf+actual_length," %d",r); |
| 78 | + actual_length += val_char_len(r) + 1; |
| 79 | + |
| 80 | + sprintf(serial_buf+actual_length," %d",g); |
| 81 | + actual_length += val_char_len(g) + 1; |
| 82 | + |
| 83 | + sprintf(serial_buf+actual_length," %d",b); |
| 84 | + actual_length += val_char_len(b) + 1; |
| 85 | + } |
| 86 | + |
| 87 | + /*-----------------------------------------------------*\ |
| 88 | + | Add the final newline | |
| 89 | + \*-----------------------------------------------------*/ |
| 90 | + sprintf(serial_buf+actual_length,"\n"); |
| 91 | + actual_length++; |
| 92 | + |
| 93 | + /*-----------------------------------------------------*\ |
| 94 | + | Send packet | |
| 95 | + \*-----------------------------------------------------*/ |
| 96 | + serialport->serial_write(serial_buf, actual_length); |
| 97 | +} |
0 commit comments