Added Macro functionality, updated readme

This commit is contained in:
2026-03-29 22:19:27 +02:00
parent b49984b9c0
commit 59b3cb4dd1
9 changed files with 354 additions and 33 deletions
+1
View File
@@ -7,6 +7,7 @@ enum class ActionType : uint8_t
HID_KEY, // Standard-Keyboard-Keycode (direkt in Firmware gesendet)
HID_CONSUMER, // Consumer-Control-Keycode (Volume, Media, …)
HOST_COMMAND, // Command-ID → Windows-App führt aus (URL, Programm, …)
MACRO, // Makro-Slot (data = Slot-Index 031) → bis zu 4 HID-Keys sequenziell
};
struct __attribute__((packed)) SAction
+66
View File
@@ -0,0 +1,66 @@
// macro_config.cpp
// NVM-Zugriff für die Makro-Tabelle (Row 1, 0x1FF00).
// Nutzt dieselben NVMCTRL-Hilfsfunktionen wie nvm_config.cpp (dupliziert,
// da static kein gemeinsamer Header für interne NVM-Helfer).
#include "macro_config.h"
#include <Arduino.h>
#include <string.h>
static const uint32_t k_macro_addr = 0x1FF00UL; // Row 1 (256B nach Row 0)
static void nvm_wait() { while (!NVMCTRL->INTFLAG.bit.READY) {} }
static void nvm_exec(uint16_t cmd)
{
NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | cmd;
nvm_wait();
}
static void nvm_erase_row(uint32_t addr)
{
nvm_wait();
NVMCTRL->ADDR.reg = addr / 2;
nvm_exec(NVMCTRL_CTRLA_CMD_ER);
}
static void nvm_write_page(uint32_t addr, const uint8_t* data)
{
nvm_exec(NVMCTRL_CTRLA_CMD_PBC);
volatile uint32_t* dst = reinterpret_cast<volatile uint32_t*>(addr);
const uint32_t* src = reinterpret_cast<const uint32_t*>(data);
for (uint8_t i = 0; i < 64 / 4; i++) dst[i] = src[i];
NVMCTRL->ADDR.reg = addr / 2;
nvm_exec(NVMCTRL_CTRLA_CMD_WP);
}
bool macro_config_load(SMacroTable& tbl)
{
memcpy(&tbl, reinterpret_cast<const void*>(k_macro_addr), sizeof(tbl));
// Prüfen ob Row 1 noch gelöscht ist (alle 0xFF = nie beschrieben)
const uint8_t* raw = reinterpret_cast<const uint8_t*>(&tbl);
bool all_ff = true;
for (uint16_t i = 0; i < sizeof(tbl); i++) {
if (raw[i] != 0xFF) { all_ff = false; break; }
}
if (all_ff) {
memset(&tbl, 0, sizeof(tbl)); // Leere Tabelle als Default
return false;
}
return true;
}
void macro_config_save(const SMacroTable& tbl)
{
// Auf 4-Byte-ausgerichteten Puffer kopieren bevor nvm_write_page ihn als uint32_t* liest.
// SMacroTable ist __attribute__((packed)) und könnte unaligned liegen →
// direkter uint32_t*-Cast würde auf Cortex-M0+ einen HardFault auslösen.
uint8_t aligned_buf[256] __attribute__((aligned(4)));
memcpy(aligned_buf, &tbl, sizeof(tbl));
NVMCTRL->CTRLB.bit.MANW = 1;
nvm_erase_row(k_macro_addr);
for (uint8_t p = 0; p < 4; p++) {
nvm_write_page(k_macro_addr + p * 64, aligned_buf + p * 64);
}
}
+36
View File
@@ -0,0 +1,36 @@
#pragma once
// macro_config.h
// Makro-Tabelle: bis zu 32 Slots, je 4 HID-Key-Steps.
// Gespeichert in NVM Row 1 (0x1FF00, 256 Bytes).
//
// Slot-Zuweisung (vom Windows-App vergeben, Board speichert blind):
// Slot 019 : MX-Buttons (mx_idx)
// Slot 2031 : Encoder-Aktionen (enc*3 + act_idx, 0=SW/1=CW/2=CCW)
//
// Ein Step mit keycode=0 gilt als leer → Ausführung stoppt dort.
// Delay zwischen Steps: 20 ms (hardcoded).
#include <stdint.h>
#define MACRO_SLOTS 32
#define MACRO_MAX_STEPS 4
// Ein einzelner HID-Key-Step im Makro
struct __attribute__((packed)) SMacroStep
{
uint8_t keycode; // HID Keyboard Usage (0x00 = leer → Step überspringen)
uint8_t modifier; // HID Modifier-Byte (Ctrl=0x01, Shift=0x02, Alt=0x04, GUI=0x08)
};
// Komplette Makro-Tabelle (32 × 4 × 2 = 256 Bytes = eine NVM-Row)
struct __attribute__((packed)) SMacroTable
{
SMacroStep steps[MACRO_SLOTS][MACRO_MAX_STEPS];
};
// Makro-Tabelle aus NVM lesen (Row 1: 0x1FF00).
// Gibt false zurück wenn der Flash-Bereich noch gelöscht (0xFF) war → leere Tabelle geladen.
bool macro_config_load(SMacroTable& tbl);
// Makro-Tabelle in NVM schreiben (löscht Row 1, schreibt 4 Pages).
void macro_config_save(const SMacroTable& tbl);
+6
View File
@@ -82,6 +82,12 @@ void nvm_config_defaults(SDeviceConfig& cfg)
cfg.led_b[i] = 0;
}
// LED-Animationen: Regenbogen (COLOR_CYCLE=5) mit 4s Periode als Standard
for (uint8_t i = 0; i < 20; i++) {
cfg.led_anim[i] = 5; // LEDAnim::COLOR_CYCLE
cfg.led_period_ms[i] = 4000;
}
cfg.crc = nvm_config_crc(cfg);
}
+10 -4
View File
@@ -7,19 +7,21 @@
// Offset Size Inhalt
// 0 4 Magic (0x56503202 = 'VP2\x02')
// 4 1 Version
// 5 2 CRC16 über Bytes 7162
// 5 2 CRC16 über Bytes 7222
// 7 60 mx_actions[20] 20 × 3B (SAction packed)
// 67 36 enc_actions[4][3] 12 × 3B
// 103 20 led_r[20]
// 123 20 led_g[20]
// 143 20 led_b[20]
// 163 93 Padding bis 256 Bytes (erste Row voll)
// 163 20 led_anim[20] LEDAnim-Typ pro Button (uint8_t)
// 183 40 led_period_ms[20] Animationsperiode in ms (uint16_t, little-endian)
// 223 33 Padding bis 256 Bytes (erste Row voll)
// 256 256 Reserviert für zukünftige Erweiterungen (zweite Row)
//
// Gesamt genutzt: 163 Bytes (sizeof SDeviceConfig mit packed SAction)
// Gesamt genutzt: 223 Bytes (sizeof SDeviceConfig mit packed SAction)
#define NVM_CONFIG_MAGIC 0x56503202UL
#define NVM_CONFIG_VERSION 1
#define NVM_CONFIG_VERSION 2 // Version 2: led_anim + led_period_ms hinzugefügt
// Encoder-Aktions-Indizes (in SDeviceConfig.enc_actions[])
// Reihenfolge: [enc][0]=SW, [enc][1]=CW, [enc][2]=CCW
@@ -41,6 +43,10 @@ struct __attribute__((packed)) SDeviceConfig
uint8_t led_r[20];
uint8_t led_g[20];
uint8_t led_b[20];
// LED-Animationen pro MX-Button
uint8_t led_anim[20]; // LEDAnim-Typ (0=STATIC, 1=BLINK, 2=PULSE, 5=COLOR_CYCLE)
uint16_t led_period_ms[20]; // Animationsperiode in ms (0 = Firmware-Default verwenden)
};
// Standardwerte wenn keine gültige Config im NVM