brand_new_model_f: fixing typo in product descriptor string
[jackhill/qmk/firmware.git] / keyboards / xwhatsit / util_comm.c
index f324a25..cf30479 100644 (file)
@@ -19,6 +19,7 @@
 #include "util_comm.h"
 #include "matrix_manipulate.h"
 #include <string.h>
+#include <tmk_core/common/eeprom.h>
 
 #if defined(KEYBOARD_SHARED_EP) && defined(RAW_ENABLE)
 #error "Enabling the KEYBOARD_SHARED_EP will make the util be unable to communicate with the firmware, because due to hidapi limiations, the util can't figure out which interface to talk to, so it hardcodes interface zero."
@@ -28,6 +29,8 @@
 #define RAW_EPSIZE 32
 #endif
 
+#define min(x, y) (((x) < (y))?(x):(y))
+
 extern const char *KEYBOARD_FILENAME; // This must be defined in keyboard_name.c to equal the filename. This is sent back to the PC-side software for it to determine which keyboard we are using.
 
 static const uint8_t magic[] = UTIL_COMM_MAGIC;
@@ -68,15 +71,18 @@ void raw_hid_receive(uint8_t *data, uint8_t length) {
             break;
         case UTIL_COMM_GET_KEYSTATE:
             response[2] = UTIL_COMM_RESPONSE_OK;
-            #if 1
-                matrix_scan_raw((matrix_row_t *)&response[3]); // AVR doesn't require not-byte values to be aligned, so this is correct, and it's an optimization.
-            #else
+            {
+                matrix_row_t current_matrix[MATRIX_ROWS];
+                matrix_scan_raw(current_matrix);
+                char *current_matrix_ptr = (char *)current_matrix;
+                int offset = 0;
+                if (sizeof(current_matrix) > 32-3)
                 {
-                    matrix_row_t current_matrix[MATRIX_ROWS];
-                    matrix_scan_raw(current_matrix);
-                    memcpy(&response[3], current_matrix, sizeof(current_matrix));
+                    offset = data[3];
+                    current_matrix_ptr += offset;
                 }
-            #endif
+                memcpy(&response[3], current_matrix_ptr, min(32-3, sizeof(current_matrix)-offset));
+            }
             break;
         case UTIL_COMM_GET_THRESHOLDS:
             response[2] = UTIL_COMM_RESPONSE_OK;
@@ -86,7 +92,14 @@ void raw_hid_receive(uint8_t *data, uint8_t length) {
                 const uint8_t cal_bin = data[3];
                 response[4] = cal_thresholds[cal_bin] & 0xff;
                 response[5] = (cal_thresholds[cal_bin] >> 8) & 0xff;
-                memcpy(&response[6], assigned_to_threshold[cal_bin], sizeof(assigned_to_threshold[cal_bin]));
+                char *assigned_to_threshold_ptr = (char *)assigned_to_threshold[cal_bin];
+                int offset = 0;
+                if (sizeof(assigned_to_threshold[cal_bin]) > 32-6)
+                {
+                    offset = data[4];
+                    assigned_to_threshold_ptr += offset;
+                }
+                memcpy(&response[6], assigned_to_threshold_ptr, min(32-6, sizeof(assigned_to_threshold[cal_bin]) - offset));
             }
             #else
             response[3] = 0;
@@ -108,9 +121,89 @@ void raw_hid_receive(uint8_t *data, uint8_t length) {
                 }
                 break;
             }
+        case UTIL_COMM_ERASE_EEPROM:
+            {
+                response[2] = UTIL_COMM_RESPONSE_OK;
+                uint16_t addr;
+                for (addr=0; addr<E2END; addr += 1)
+                {
+                    eeprom_update_byte((uint8_t*)addr, 0xff);
+                }
+                break;
+            }
+        case UTIL_COMM_GET_SIGNAL_VALUE:
+            {
+                response[2] = UTIL_COMM_RESPONSE_OK;
+                uint8_t col = data[3];
+                uint8_t row = data[4];
+                uint8_t count = data[5];
+                int i;
+                for (i=0;i<count;i++)
+                {
+                    uint16_t value = measure_middle_keymap_coords(col, row, CAPSENSE_HARDCODED_SAMPLE_TIME, 8);
+                    response[3+i*2] = value & 0xff;
+                    response[3+i*2+1] = (value >> 8) & 0xff;
+                    col += 1;
+                    if (col >= MATRIX_COLS) {
+                        col -= MATRIX_COLS;
+                        row += 1;
+                    }
+                    if (row >= MATRIX_ROWS)
+                    {
+                        break;
+                    }
+                }
+                break;
+            }
+        case UTIL_COMM_GET_KEYBOARD_DETAILS:
+            {
+                response[2] = UTIL_COMM_RESPONSE_OK;
+                response[3] = MATRIX_COLS;
+                response[4] = MATRIX_ROWS;
+                #if defined(CONTROLLER_IS_XWHATSIT_BEAMSPRING_REV_4)
+                response[5] = 1;
+                #elif defined(CONTROLLER_IS_XWHATSIT_MODEL_F_OR_WCASS_MODEL_F)
+                response[5] = 2;
+                #elif defined(CONTROLLER_IS_THROUGH_HOLE_BEAMSPRING)
+                response[5] = 3;
+                #elif defined(CONTROLLER_IS_THROUGH_HOLE_MODEL_F)
+                response[5] = 4;
+                #else
+                response[5] = 0;
+                #endif
+                response[6] = CAPSENSE_KEYBOARD_SETTLE_TIME_US;
+                response[7] = CAPSENSE_DAC_SETTLE_TIME_US;
+                response[8] = CAPSENSE_HARDCODED_SAMPLE_TIME;
+                response[9] = CAPSENSE_CAL_ENABLED;
+                response[10] = CAPSENSE_DAC_MAX & 0xFF;
+                response[11] = (CAPSENSE_DAC_MAX >> 8) & 0xFF;
+                break;
+            }
+        case UTIL_COMM_SHIFT_DATA:
+            {
+                response[2] = UTIL_COMM_RESPONSE_OK;
+                uint32_t shdata = (((uint32_t)(data[3])) << 0) |
+                                (((uint32_t)(data[4])) << 8) |
+                                (((uint32_t)(data[5])) << 16) |
+                                (((uint32_t)(data[6])) << 24);
+                shift_data(shdata);
+                break;
+            }
+        case UTIL_COMM_SET_DAC_VALUE:
+            {
+                response[2] = UTIL_COMM_RESPONSE_OK;
+                uint16_t value = data[3] | (((uint16_t)data[4]) << 8);
+                dac_write_threshold(value);
+                break;
+            }
+        case UTIL_COMM_GET_ROW_STATE:
+            {
+                response[2] = UTIL_COMM_RESPONSE_OK;
+                response[3] = test_single(255, 0, NULL);
+                break;
+            }
         default:
             break;
     }
     raw_hid_send(response, sizeof(response));
 }
-