Skip to content

Commit 84cc3cf

Browse files
committed
more fixes + improvements
1 parent d196d99 commit 84cc3cf

9 files changed

Lines changed: 130 additions & 79 deletions

File tree

Keypad.Firmware/Keypad.Firmware.ino

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
//app include
1010
#include "src/debug_mode.h"
1111
#if !CONFIGURATION_DEBUG_MODE
12+
#if NEO_COUNT > 0
1213
#include "src/neo/neo.h"
14+
#endif
1315
#include "src/userUsbHidKeyboardMouse/USBHIDKeyboardMouse.h"
1416
#include "src/buttons.h"
1517
#include "src/encoder.h"
@@ -27,20 +29,24 @@ void setup()
2729
#if CONFIGURATION_DEBUG_MODE
2830
debug_mode_setup();
2931
#else
32+
#if NEO_COUNT > 0
3033
// Initialize neopixels
3134
NEO_init();
3235
delay(10);
3336
NEO_clearAll();
37+
#endif
3438

3539
// Go in bootloader mode if the configured boot button is held during power-on
3640
if (configuration_bootloader_requested())
3741
{
42+
#if NEO_COUNT > 0
3843
const uint8_t boot_hues[3] = {NEO_CYAN, NEO_BLUE, NEO_MAG};
3944
for (uint8_t i = 0; i < NEO_COUNT; ++i)
4045
{
4146
NEO_writeHue(i, boot_hues[i % 3], NEO_BRIGHT_KEYS);
4247
}
4348
NEO_update(); // update pixels
49+
#endif
4450
BOOT_now(); // jump to bootloader
4551
}
4652

Keypad.Firmware/src/led.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
#include <Arduino.h>
22
#include "../configuration.h"
3-
#include "neo/neo.h"
43
#include "led.h"
54

65
#if !CONFIGURATION_DEBUG_MODE
76

8-
// ===================================================================================
9-
// Color section
10-
// ============================================================================
11-
12-
#if NEO_COUNT <= 0
13-
#error "NEO_COUNT must be greater than 0"
14-
#endif
7+
#if NEO_COUNT > 0
8+
#include "neo/neo.h"
159

1610
static enum led_keyboard_mode_t led_mode_s = LED_LOOP;
1711
static int color_hue_s[NEO_COUNT] = {0}; // hue value: 0..191 color map
@@ -90,4 +84,29 @@ void led_update()
9084
NEO_update();
9185
}
9286

87+
#else
88+
89+
void led_set_color_hue(uint8_t led0, uint8_t led1, uint8_t led2)
90+
{
91+
(void)led0;
92+
(void)led1;
93+
(void)led2;
94+
}
95+
96+
void led_set_mode(enum led_keyboard_mode_t mode)
97+
{
98+
(void)mode;
99+
}
100+
101+
void led_presskey(int key)
102+
{
103+
(void)key;
104+
}
105+
106+
void led_update()
107+
{
108+
}
109+
110+
#endif
111+
93112
#endif

Keypad.Firmware/src/neo/neo.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
// Libraries, Variables and Constants
1919
// ===================================================================================
2020
#include "../../configuration.h"
21+
22+
#if NEO_COUNT > 0
23+
2124
#include "neo.h"
2225

2326
#define NEOPIN PIN_asm(PIN_NEO) // convert PIN_NEO for inline assembly
@@ -157,3 +160,5 @@ void NEO_writeHue(uint8_t pixel, uint8_t hue, uint8_t bright) {
157160
void NEO_clearPixel(uint8_t pixel) {
158161
NEO_writeColor(pixel, 0, 0, 0);
159162
}
163+
164+
#endif

Keypad.Flasher.Server.Tests/ConfigurationGeneratorTests.cs

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ public void GenerateHeader_WithMultipleLedIndices_ComputesNeoCountFromBindings()
6161
public void GenerateHeader_WithCustomNeoPixelPin_EmitsPin()
6262
{
6363
var configuration = new ConfigurationDefinition(
64-
Array.Empty<ButtonBinding>(),
64+
new List<ButtonBinding>
65+
{
66+
new ButtonBinding(1, true, 0, false, false, new HidSequenceBinding("a", 0))
67+
},
6568
Array.Empty<EncoderBinding>(),
6669
DebugMode: false,
6770
NeoPixelPin: 31);
@@ -71,6 +74,25 @@ public void GenerateHeader_WithCustomNeoPixelPin_EmitsPin()
7174
Assert.That(result, Does.Contain("#define PIN_NEO P31"));
7275
}
7376

77+
[Test]
78+
public void GenerateHeader_WithNoAssignedLedIndices_SetsNeoCountToZero()
79+
{
80+
var configuration = new ConfigurationDefinition(
81+
new List<ButtonBinding>
82+
{
83+
new ButtonBinding(1, true, -1, false, false, new HidSequenceBinding("a", 0))
84+
},
85+
Array.Empty<EncoderBinding>(),
86+
DebugMode: false,
87+
NeoPixelPin: 34);
88+
89+
var result = Generator.GenerateHeader(configuration);
90+
91+
Assert.That(result, Does.Contain("#define NEO_COUNT 0"));
92+
Assert.That(result, Does.Not.Contain("#define PIN_NEO"));
93+
Assert.That(result, Does.Not.Contain("#define NEO_GRB"));
94+
}
95+
7496
[Test]
7597
public void GenerateHeader_WithDebugMode_EmitsFlag()
7698
{
@@ -121,33 +143,33 @@ public void GenerateSource_WithFourButtons_WritesExpectedConfiguration()
121143
var buttons = new List<ButtonBinding>
122144
{
123145
new ButtonBinding(
124-
Pin: 10,
146+
Pin: 15,
125147
ActiveLow: true,
126148
LedIndex: 0,
127-
BootloaderOnBoot: false,
128-
BootloaderChordMember: false,
129-
Function: new HidSequenceBinding("x", 0)),
149+
BootloaderOnBoot: true,
150+
BootloaderChordMember: true,
151+
Function: new HidSequenceBinding("1", 0)),
130152
new ButtonBinding(
131-
Pin: 12,
153+
Pin: 16,
132154
ActiveLow: true,
133155
LedIndex: 1,
134-
BootloaderOnBoot: false,
156+
BootloaderOnBoot: true,
135157
BootloaderChordMember: true,
136-
Function: new HidSequenceBinding("y", 0)),
158+
Function: new HidSequenceBinding("2", 0)),
137159
new ButtonBinding(
138-
Pin: 14,
139-
ActiveLow: false,
160+
Pin: 17,
161+
ActiveLow: true,
140162
LedIndex: 2,
141-
BootloaderOnBoot: false,
142-
BootloaderChordMember: false,
143-
Function: new HidSequenceBinding("Enter", 2)),
163+
BootloaderOnBoot: true,
164+
BootloaderChordMember: true,
165+
Function: new HidSequenceBinding("3", 0)),
144166
new ButtonBinding(
145-
Pin: 15,
146-
ActiveLow: false,
147-
LedIndex: -1,
167+
Pin: 11,
168+
ActiveLow: true,
169+
LedIndex: 3,
148170
BootloaderOnBoot: true,
149171
BootloaderChordMember: true,
150-
Function: new HidFunctionBinding("hid_consumer_volume_down"))
172+
Function: new HidSequenceBinding("4", 0))
151173
};
152174

153175
var configuration = new ConfigurationDefinition(buttons, Array.Empty<EncoderBinding>(), DebugMode: false, NeoPixelPin: 34);
@@ -164,11 +186,11 @@ public void GenerateSource_WithTwoButtonModule_WritesExpectedConfiguration()
164186
{
165187
var buttons = new List<ButtonBinding>
166188
{
167-
new ButtonBinding(32, true, 0, false, false, new HidSequenceBinding("1", 0)),
168-
new ButtonBinding(14, true, 1, false, false, new HidSequenceBinding("2", 0))
189+
new ButtonBinding(32, true, -1, true, true, new HidSequenceBinding("1", 0)),
190+
new ButtonBinding(14, true, -1, true, true, new HidSequenceBinding("2", 0))
169191
};
170192

171-
var configuration = new ConfigurationDefinition(buttons, Array.Empty<EncoderBinding>(), DebugMode: false, NeoPixelPin: 34);
193+
var configuration = new ConfigurationDefinition(buttons, Array.Empty<EncoderBinding>(), DebugMode: false, NeoPixelPin: -1);
172194

173195
var expected = ReadExpected("generate_source_2_button_module.c");
174196

Keypad.Flasher.Server.Tests/ExpectedOutputs/ConfigurationGenerator/generate_source_2_button_module.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ const button_binding_t button_bindings[] = {
55
{
66
.pin = 32,
77
.active_low = true,
8-
.led_index = 0,
9-
.bootloader_on_boot = false,
10-
.bootloader_chord_member = false,
8+
.led_index = -1,
9+
.bootloader_on_boot = true,
10+
.bootloader_chord_member = true,
1111
.function = {
1212
.type = HID_BINDING_SEQUENCE,
1313
.function.sequence = {
@@ -20,9 +20,9 @@ const button_binding_t button_bindings[] = {
2020
{
2121
.pin = 14,
2222
.active_low = true,
23-
.led_index = 1,
24-
.bootloader_on_boot = false,
25-
.bootloader_chord_member = false,
23+
.led_index = -1,
24+
.bootloader_on_boot = true,
25+
.bootloader_chord_member = true,
2626
.function = {
2727
.type = HID_BINDING_SEQUENCE,
2828
.function.sequence = {

Keypad.Flasher.Server.Tests/ExpectedOutputs/ConfigurationGenerator/generate_source_4_buttons.c

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,63 @@
33

44
const button_binding_t button_bindings[] = {
55
{
6-
.pin = 10,
6+
.pin = 15,
77
.active_low = true,
88
.led_index = 0,
9-
.bootloader_on_boot = false,
10-
.bootloader_chord_member = false,
9+
.bootloader_on_boot = true,
10+
.bootloader_chord_member = true,
1111
.function = {
1212
.type = HID_BINDING_SEQUENCE,
1313
.function.sequence = {
14-
.sequence = {'x'},
14+
.sequence = {'1'},
1515
.length = 1,
1616
.delay = 0
1717
}
1818
}
1919
},
2020
{
21-
.pin = 12,
21+
.pin = 16,
2222
.active_low = true,
2323
.led_index = 1,
24-
.bootloader_on_boot = false,
24+
.bootloader_on_boot = true,
2525
.bootloader_chord_member = true,
2626
.function = {
2727
.type = HID_BINDING_SEQUENCE,
2828
.function.sequence = {
29-
.sequence = {'y'},
29+
.sequence = {'2'},
3030
.length = 1,
3131
.delay = 0
3232
}
3333
}
3434
},
3535
{
36-
.pin = 14,
37-
.active_low = false,
36+
.pin = 17,
37+
.active_low = true,
3838
.led_index = 2,
39-
.bootloader_on_boot = false,
40-
.bootloader_chord_member = false,
39+
.bootloader_on_boot = true,
40+
.bootloader_chord_member = true,
4141
.function = {
4242
.type = HID_BINDING_SEQUENCE,
4343
.function.sequence = {
44-
.sequence = {'E', 'n', 't', 'e', 'r'},
45-
.length = 5,
46-
.delay = 2
44+
.sequence = {'3'},
45+
.length = 1,
46+
.delay = 0
4747
}
4848
}
4949
},
5050
{
51-
.pin = 15,
52-
.active_low = false,
53-
.led_index = -1,
51+
.pin = 11,
52+
.active_low = true,
53+
.led_index = 3,
5454
.bootloader_on_boot = true,
5555
.bootloader_chord_member = true,
5656
.function = {
57-
.type = HID_BINDING_FUNCTION,
58-
.function.functionPointer = hid_consumer_volume_down
57+
.type = HID_BINDING_SEQUENCE,
58+
.function.sequence = {
59+
.sequence = {'4'},
60+
.length = 1,
61+
.delay = 0
62+
}
5963
}
6064
}
6165
};

Keypad.Flasher.Server/Configuration/ConfigurationGenerator.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ public class ConfigurationGenerator
77
public string GenerateHeader(ConfigurationDefinition configuration)
88
{
99
var neoPixelCount = CalculateNeoPixelCount(configuration.Buttons);
10-
var neoPixelPin = configuration.NeoPixelPin;
11-
if (neoPixelPin < 0)
12-
{
13-
throw new ArgumentOutOfRangeException(nameof(configuration), "NeoPixel pin must be non-negative.");
14-
}
15-
var neoPixelPinMacro = $"P{neoPixelPin}";
16-
1710
var sb = new StringBuilder();
1811
sb.AppendLine("#pragma once");
1912
sb.AppendLine();
@@ -23,9 +16,22 @@ public string GenerateHeader(ConfigurationDefinition configuration)
2316
sb.AppendLine($"#define CONFIGURATION_ENCODER_CAPACITY {configuration.Encoders.Count}");
2417
sb.AppendLine($"#define CONFIGURATION_DEBUG_MODE {ToCInteger(configuration.DebugMode)}");
2518
sb.AppendLine();
26-
sb.AppendLine($"#define PIN_NEO {neoPixelPinMacro}");
27-
sb.AppendLine($"#define NEO_COUNT {neoPixelCount}");
28-
sb.AppendLine("#define NEO_GRB");
19+
if (neoPixelCount > 0)
20+
{
21+
if (configuration.NeoPixelPin < 0)
22+
{
23+
throw new ArgumentOutOfRangeException(nameof(configuration), "NeoPixel pin must be non-negative when LEDs are configured.");
24+
}
25+
26+
var neoPixelPinMacro = $"P{configuration.NeoPixelPin}";
27+
sb.AppendLine($"#define PIN_NEO {neoPixelPinMacro}");
28+
sb.AppendLine($"#define NEO_COUNT {neoPixelCount}");
29+
sb.AppendLine("#define NEO_GRB");
30+
}
31+
else
32+
{
33+
sb.AppendLine("#define NEO_COUNT 0");
34+
}
2935
return sb.ToString();
3036
}
3137

@@ -115,7 +121,7 @@ private static int CalculateNeoPixelCount(IReadOnlyCollection<ButtonBinding> but
115121
}
116122

117123
var count = maxLedIndex + 1;
118-
return count < 1 ? 1 : count;
124+
return count < 0 ? 0 : count;
119125
}
120126

121127
private static void AppendButton(StringBuilder sb, ButtonBinding button, bool isLast, bool debugMode)

Keypad.Flasher.Server/Controllers/FlasherController.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,10 @@ public record Firmware(byte[] FileBytes);
131131

132132
internal static ConfigurationDefinition CreateDefaultConfiguration()
133133
{
134-
var buttons = new List<ButtonBinding>
134+
var buttons = new List<ButtonBinding>
135135
{
136-
new ButtonBinding(
137-
Pin: 32,
138-
ActiveLow: true,
139-
LedIndex: 0,
140-
BootloaderOnBoot: false,
141-
BootloaderChordMember: false,
142-
Function: new HidSequenceBinding("1", 0)),
143-
new ButtonBinding(
144-
Pin: 14,
145-
ActiveLow: true,
146-
LedIndex: 1,
147-
BootloaderOnBoot: false,
148-
BootloaderChordMember: false,
149-
Function: new HidSequenceBinding("2", 0))
136+
new ButtonBinding(32, true, 0, true, true, new HidSequenceBinding("1", 0)),
137+
new ButtonBinding(14, true, 1, true, true, new HidSequenceBinding("2", 0))
150138
};
151139

152140
return new ConfigurationDefinition(buttons, Array.Empty<EncoderBinding>(), DebugMode: false, NeoPixelPin: 34);

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ If you find and successfully test a new compatible device not in this list, plea
7979
They all have USB-C connectors unless otherwise noted:
8080

8181
- [2 Keys](https://www.aliexpress.com/item/1005004970126333.html?spm=a2g0o.order_detail.order_detail_item.3.3c7af19cNrdJJB)
82+
- Note: Uses non-standard LEDs that are not compatible with this project's NeoPixel support so LED functionality will not work
8283
- [3 Keys 1 Knob](https://www.aliexpress.com/item/1005006627901462.html?spm=a2g0o.order_detail.order_detail_item.3.295bf19c3IDC8m)
8384
- [4 Keys](https://www.aliexpress.com/item/1005008020501723.html?spm=a2g0o.order_detail.order_detail_item.3.7d51f19cZTzoOY)
8485
- [6 Keys 1 Knob](https://www.aliexpress.com/item/1005009812219099.html?spm=a2g0o.order_detail.order_detail_item.3.6afff19cXlayc4)

0 commit comments

Comments
 (0)