void updaterange() { leds[LED_0].b = 255; updateleds(); range[0] = FLEXIO_HCSR_Ping(0, GPIOA, 5); // 0 leds[LED_0].b = 0; leds[LED_0].r = 255 - (range[0] / 2); leds[LED_0].g = range[0] / 2; leds[LED_180].b = 255; updateleds(); range[4] = FLEXIO_HCSR_Ping(5, GPIOA, 14); // 180 leds[LED_180].b = 0; leds[LED_180].r = 255 - (range[4] / 2); leds[LED_180].g = range[4] / 2; leds[LED_45].b = 255; updateleds(); range[1] = FLEXIO_HCSR_Ping(2, GPIOA, 13); // 45 leds[LED_45].b = 0; leds[LED_45].r = 255 - (range[1] / 2); leds[LED_45].g = range[1] / 2; leds[LED_225].b = 255; updateleds(); range[5] = FLEXIO_HCSR_Ping(6, GPIOC, 7); // 225 leds[LED_225].b = 0; leds[LED_225].r = 255 - (range[5] / 2); leds[LED_225].g = range[5] / 2; leds[LED_90].b = 255; updateleds(); range[2] = FLEXIO_HCSR_Ping(3, GPIOA, 12); // 90 leds[LED_90].b = 0; leds[LED_90].r = 255 - (range[2] / 2); leds[LED_90].g = range[2] / 2; leds[LED_270].b = 255; updateleds(); range[6] = FLEXIO_HCSR_Ping(7, GPIOA, 16); // 270 leds[LED_270].b = 0; leds[LED_270].r = 255 - (range[6] / 2); leds[LED_270].g = range[6] / 2; leds[LED_135].b = 255; updateleds(); range[3] = FLEXIO_HCSR_Ping(4, GPIOA, 17); // 135 leds[LED_135].b = 0; leds[LED_135].r = 255 - (range[3] / 2); leds[LED_135].g = range[3] / 2; leds[LED_315].b = 255; updateleds(); range[7] = FLEXIO_HCSR_Ping(8, GPIOA, 15); // 315 leds[LED_315].b = 0; leds[LED_315].r = 255 - (range[7] / 2); leds[LED_315].g = range[7] / 2; updateleds(); }
void readcmd(usbdevice* kb, const char* line){ char word[strlen(line) + 1]; int wordlen; // See if the first word is a serial number. If so, switch devices and skip to the next word. usbsetting* set = (IS_ACTIVE(kb) ? &kb->setting : 0); usbprofile* profile = (set ? &set->profile : 0); usbmode* mode = (profile ? profile->currentmode : 0); cmd command = NONE; cmdhandler handler = 0; int rgbchange = 0; // Read words from the input while(sscanf(line, "%s%n", word, &wordlen) == 1){ line += wordlen; // Check for a command word if(!strcmp(word, "device")){ command = DEVICE; handler = 0; continue; } else if(!strcmp(word, "mode")){ command = MODE; handler = 0; continue; } else if(!strcmp(word, "switch")){ command = NONE; handler = 0; if(profile) profile->currentmode = mode; rgbchange = 1; continue; } else if(!strcmp(word, "hwload")){ command = NONE; handler = 0; if(profile) hwloadprofile(kb); rgbchange = 1; } else if(!strcmp(word, "hwsave")){ command = NONE; handler = 0; if(profile) hwsaveprofile(kb); } else if(!strcmp(word, "erase")){ command = NONE; handler = 0; if(mode) erasemode(mode); rgbchange = 1; continue; } else if(!strcmp(word, "eraseprofile")){ command = NONE; handler = 0; if(profile){ eraseprofile(profile); mode = profile->currentmode = getusbmode(0, profile); } rgbchange = 1; continue; } else if(!strcmp(word, "name")){ command = NAME; handler = 0; if(mode) updatemod(&mode->id); continue; } else if(!strcmp(word, "profilename")){ command = PROFILENAME; handler = 0; if(profile) updatemod(&profile->id); continue; } else if(!strcmp(word, "bind")){ command = BIND; handler = cmd_bind; continue; } else if(!strcmp(word, "unbind")){ command = UNBIND; handler = cmd_unbind; continue; } else if(!strcmp(word, "rebind")){ command = REBIND; handler = cmd_rebind; continue; } else if(!strcmp(word, "macro")){ command = MACRO; handler = 0; continue; } else if(!strcmp(word, "rgb")){ command = RGB; handler = cmd_ledrgb; rgbchange = 1; if(mode) updatemod(&mode->id); continue; } if(command == NONE) continue; else if(command == DEVICE){ if(strlen(word) == SERIAL_LEN - 1){ usbdevice* found = findusb(word); if(found){ kb = found; set = &kb->setting; } else { // If the device isn't plugged in, find (or add) it to storage kb = 0; set = addstore(word); } profile = (set ? &set->profile : 0); mode = (profile ? profile->currentmode : 0); } continue; } // Only the DEVICE command is valid without an existing mode if(!mode) continue; if(command == MODE){ int newmode; if(sscanf(word, "%u", &newmode) == 1 && newmode > 0 && newmode < MODE_MAX) mode = getusbmode(newmode - 1, profile); continue; } else if(command == NAME){ // Name just parses a whole word setmodename(mode, word); continue; } else if(command == PROFILENAME){ // Same for profile name setprofilename(profile, word); continue; } else if(command == RGB){ // RGB command has a special response for "on", "off", and a hex constant int r, g, b; if(!strcmp(word, "on")){ cmd_ledon(mode); continue; } else if(!strcmp(word, "off")){ cmd_ledoff(mode); continue; } else if(sscanf(word, "%02x%02x%02x", &r, &g, &b) == 3){ for(int i = 0; i < N_KEYS; i++) cmd_ledrgb(mode, i, word); continue; } } else if(command == MACRO && !strcmp(word, "clear")){ // Macro has a special clear command cmd_macroclear(mode); continue; } // Split the parameter at the colon int left = -1; sscanf(word, "%*[^:]%n", &left); if(left <= 0) continue; const char* right = word + left; if(right[0] == ':') right++; // Macros have a separate left-side handler if(command == MACRO){ word[left] = 0; cmd_macro(mode, word, right); continue; } // Scan the left side for key names and run the request command int position = 0, field = 0; char keyname[11]; while(position < left && sscanf(word + position, "%10[^:,]%n", keyname, &field) == 1){ int keycode; if(!strcmp(keyname, "all")){ // Set all keys for(int i = 0; i < N_KEYS; i++) handler(mode, i, right); } else if((sscanf(keyname, "#%d", &keycode) && keycode >= 0 && keycode < N_KEYS) || (sscanf(keyname, "#x%x", &keycode) && keycode >= 0 && keycode < N_KEYS)){ // Set a key numerically handler(mode, keycode, right); } else { // Find this key in the keymap for(unsigned i = 0; i < N_KEYS; i++){ if(keymap[i].name && !strcmp(keyname, keymap[i].name)){ handler(mode, i, right); break; } } } if(word[position += field] == ',') position++; } } if(mode && rgbchange) updateleds(kb); }