1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
| #include <stdint.h> #include <stdbool.h> #include <string.h> #include "hal.h" #include "config.h" #include "usb_comm.h" #include "lcd_display.h"
#define MAX_BUTTONS 10 typedef struct { uint8_t button_id; uint8_t function_type; uint8_t function_param[6]; } ButtonMapping;
ButtonMapping button_map[MAX_BUTTONS]; uint8_t button_count = 0;
#define MAX_ENCODERS 2 typedef struct { uint8_t encoder_id; uint8_t function_type; uint8_t function_param[6]; } EncoderMapping;
EncoderMapping encoder_map[MAX_ENCODERS]; uint8_t encoder_count = 0;
typedef enum { FUNC_TYPE_NONE, FUNC_TYPE_KEYBOARD, FUNC_TYPE_MOUSE, FUNC_TYPE_MEDIA, FUNC_TYPE_MACRO, FUNC_TYPE_CUSTOM } FunctionType;
typedef enum { ENCODER_TYPE_NONE, ENCODER_TYPE_VOLUME, ENCODER_TYPE_SCROLL, ENCODER_TYPE_CUSTOM } EncoderType;
void system_init(void); void handle_button_event(uint8_t button_id, bool is_pressed); void handle_encoder_event(uint8_t encoder_id, int16_t rotation); void update_lcd_display(void); void process_usb_data(uint8_t *data, uint16_t len); void execute_function(uint8_t function_type, uint8_t *params); void load_config(void); void save_config(void);
int main(void) { system_init(); load_config();
while (1) { for (uint8_t i = 0; i < button_count; i++) { if (hal_button_is_pressed(i)) { handle_button_event(i, true); } else { handle_button_event(i, false); } }
for(uint8_t i=0; i<encoder_count; i++){ int16_t rotation = hal_encoder_get_rotation(i); if(rotation != 0) handle_encoder_event(i, rotation); }
update_lcd_display();
uint8_t usb_data[64]; uint16_t usb_len = usb_comm_receive(usb_data, sizeof(usb_data)); if (usb_len > 0) { process_usb_data(usb_data, usb_len); } hal_delay_ms(10); } return 0; }
void system_init(void) { hal_init(); lcd_init(); usb_comm_init(); button_count = 0; memset(button_map, 0, sizeof(button_map));
encoder_count = 0; memset(encoder_map, 0, sizeof(encoder_map)); lcd_clear(); lcd_display_string("Ready"); }
void handle_button_event(uint8_t button_id, bool is_pressed) { static uint32_t last_time[MAX_BUTTONS] = {0}; if (is_pressed) { if (hal_get_tick_ms() - last_time[button_id] > 50) { last_time[button_id] = hal_get_tick_ms(); for(int i =0; i<button_count; i++){ if(button_map[i].button_id == button_id){ execute_function(button_map[i].function_type, button_map[i].function_param); break; } } } } }
void handle_encoder_event(uint8_t encoder_id, int16_t rotation) { for(int i =0; i<encoder_count; i++){ if(encoder_map[i].encoder_id == encoder_id){ if(rotation >0) { for(uint16_t j=0; j< rotation; j++){ execute_function(encoder_map[i].function_type, encoder_map[i].function_param); } } else{ for(uint16_t j=0; j< -rotation; j++){ execute_function(encoder_map[i].function_type, encoder_map[i].function_param); } } break; } } }
void update_lcd_display(void) { }
void process_usb_data(uint8_t *data, uint16_t len) {
if (len < 2) return; uint8_t cmd = data[0]; uint8_t data_len = data[1];
if(len < 2 + data_len) return;
uint8_t* cmd_data = data+2;
switch (cmd) { case CMD_BUTTON_CONFIG: { if(data_len < 3) return; if (button_count >= MAX_BUTTONS) { break; } uint8_t id = cmd_data[0]; uint8_t type = cmd_data[1]; uint8_t* param = cmd_data+2;
button_map[button_count].button_id = id; button_map[button_count].function_type = type; memcpy(button_map[button_count].function_param, param, data_len-2); button_count++; save_config(); break; } case CMD_ENCODER_CONFIG:{ if(data_len < 3) return; if (encoder_count >= MAX_ENCODERS) { break; } uint8_t id = cmd_data[0]; uint8_t type = cmd_data[1]; uint8_t* param = cmd_data+2; encoder_map[encoder_count].encoder_id = id; encoder_map[encoder_count].function_type = type; memcpy(encoder_map[encoder_count].function_param, param, data_len-2); encoder_count++; save_config(); break; } case CMD_IMAGE_DATA: { lcd_display_image(cmd_data, data_len); break; } case CMD_REQUEST_CONFIG:{ uint8_t rsp_data[256]; uint16_t rsp_len = 0; rsp_data[rsp_len++] = CMD_RESPONSE_CONFIG; rsp_data[rsp_len++] = button_count; memcpy(rsp_data+rsp_len, button_map, sizeof(ButtonMapping)*button_count); rsp_len += sizeof(ButtonMapping)*button_count; rsp_data[rsp_len++] = encoder_count; memcpy(rsp_data+rsp_len, encoder_map, sizeof(EncoderMapping)*encoder_count); rsp_len += sizeof(EncoderMapping)*encoder_count;
usb_comm_send(rsp_data, rsp_len); break; } case CMD_RESET_CONFIG:{ button_count = 0; memset(button_map, 0, sizeof(button_map));
encoder_count = 0; memset(encoder_map, 0, sizeof(encoder_map)); save_config(); break; }
default: break; } }
void execute_function(uint8_t function_type, uint8_t *params) { switch (function_type) { case FUNC_TYPE_KEYBOARD: { if(params[0] == 0){ hal_keybord_release(); } else{ hal_keyboard_press(params[0]); } break; } case FUNC_TYPE_MOUSE:{ hal_mouse_event(params); break; } case FUNC_TYPE_MEDIA:{ hal_media_control(params); break; } case FUNC_TYPE_MACRO:{ break; } case FUNC_TYPE_CUSTOM:{ break; } case ENCODER_TYPE_VOLUME:{ hal_audio_control(params); break; } case ENCODER_TYPE_SCROLL:{ hal_mouse_scroll(params); break; } default: break; } }
#include "eeprom.h"
#define CONFIG_ADDR 0x0000
void load_config(void) { uint8_t* p = (uint8_t*)CONFIG_ADDR; uint16_t size = 0; eeprom_read(p, &button_count, sizeof(uint8_t)); p += sizeof(uint8_t); if(button_count > 0 && button_count <=MAX_BUTTONS){ eeprom_read(p, button_map, sizeof(ButtonMapping)*button_count); } p += sizeof(ButtonMapping)*MAX_BUTTONS; eeprom_read(p, &encoder_count, sizeof(uint8_t)); p += sizeof(uint8_t); if(encoder_count > 0 && encoder_count <=MAX_ENCODERS){ eeprom_read(p, encoder_map, sizeof(EncoderMapping)*encoder_count); } }
void save_config(void) { uint8_t* p = (uint8_t*)CONFIG_ADDR; eeprom_write(p, &button_count, sizeof(uint8_t)); p+= sizeof(uint8_t); if(button_count > 0){ eeprom_write(p, button_map, sizeof(ButtonMapping)*button_count); } p += sizeof(ButtonMapping)*MAX_BUTTONS; eeprom_write(p, &encoder_count, sizeof(uint8_t)); p+= sizeof(uint8_t); if(encoder_count > 0){ eeprom_write(p, encoder_map, sizeof(EncoderMapping)*encoder_count); } }
|