1+ /*
2+ * mighty.c
3+ * mighty
4+ *
5+ * Created by Michel DEPEIGE on 03/03/2025.
6+ * Copyright (c) 2025 Michel DEPEIGE.
7+ *
8+ * This program is free software; you can redistribute it and/or modify
9+ * it under the terms of the Apache License, Version 2.0 (the "License");
10+ * You may obtain a copy of the License at:
11+ *
12+ * http://www.apache.org/licenses/LICENSE-2.0
13+ *
14+ * Unless required by applicable law or agreed to in writing, software
15+ * distributed under the License is distributed on an "AS IS" BASIS,
16+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+ * See the License for the specific language governing permissions and
18+ * limitations under the License.
19+ *
20+ */
21+
22+ #include <stdio.h>
23+ #include <string.h>
24+ #include "driver/gpio.h"
25+ #include "sdkconfig.h"
26+ #include "freertos/FreeRTOS.h"
27+ #include "freertos/queue.h"
28+ #include "freertos/semphr.h"
29+ #include "freertos/task.h"
30+ #include "esp_log.h"
31+ #include "esp_system.h"
32+ #include "esp_chip_info.h"
33+ #if !CONFIG_BT_NIMBLE_ENABLED
34+ #include "esp_bt_defs.h"
35+ #include "esp_bt_main.h"
36+ #include "esp_bt_device.h"
37+ #include "esp_gap_bt_api.h"
38+ #endif
39+ #include "esp_hid_common.h"
40+ #include "esp_hidh.h"
41+ #include "esp_hidh_api.h"
42+
43+ #include "blue.h"
44+ #include "led.h"
45+ #include "mighty.h"
46+
47+ /* functions */
48+
49+ /*
50+ * this function will decode a mighty frame and convert it for quadrature tasks
51+ *
52+ * Byte | Bits | Description
53+ * -----+------+---------------------------------------------------------------
54+ * 0 | 0 | Button 1 (left)
55+ * 0 | 1 | Button 2 (right)
56+ * 0 | 2 | Button 3 (middle)
57+ * 0 | 3-7 | unused
58+ * 1 | 0-7 | X Displacement
59+ * 2 | 0-7 | Y Displacement
60+ * 3 | 0-7 | Ball X Axis
61+ * 4 | 0-7 | Ball Y Axis
62+ * 5 | 0-7 | pressure sensor. Follow roughly (0)
63+ *
64+ * Original HID dump
65+ * +-------------------------------------------------+
66+ * | 05 01 09 02 a1 01 85 02 05 09 19 01 29 04 15 00 |
67+ * | 09 01 a1 00 15 81 25 7f 09 30 09 31 75 08 95 02 |
68+ * | 81 06 05 0c 0a 38 02 75 08 95 01 81 06 05 01 09 |
69+ * | 38 75 08 95 01 81 06 c0 05 ff 09 c0 75 08 95 01 |
70+ * | 81 02 05 06 09 20 85 47 15 00 25 64 75 08 95 01 |
71+ * | b1 a2 c0 |
72+ * +-------------------------------------------------+
73+ */
74+
75+ void mighty_input (esp_hidh_dev_t * dev , uint16_t id , uint8_t * data , uint16_t length ) {
76+ /*
77+ * Mighty Mouse reports are exactly 6 bytes. Check for size in case of random error
78+ * The button checks and handling follow the boot protocol
79+ *
80+ * Since the first 3 bytes are the same as boot, let's handle some errors here
81+ * and call the boot decoder with a truncated report (ignore pressure and ball)
82+ */
83+
84+ if (length != 6 ) {
85+ xTaskNotify (t_red , LED_ONCE , eSetValueWithOverwrite );
86+ return ;
87+ }
88+ if (data [3 ] || data [4 ]) {
89+ xTaskNotify (t_red , LED_ONCE , eSetValueWithOverwrite );
90+ }
91+
92+ blue_h_boot (data , 3 );
93+ }
94+
95+ /*
96+ * this will tell us if the device is a supported Mighty Mouse
97+ * PID + VID provided by the HID stack
98+ */
99+ bool mighty_is_mm (uint16_t vid , uint16_t pid ) {
100+ if (vid != MIGHTY_VID )
101+ return false;
102+ if (pid != MIGHTY_PID )
103+ return false;
104+
105+ return true;
106+ }
0 commit comments