Skip to content
Snippets Groups Projects
Commit 5aaa1d77 authored by minute's avatar minute
Browse files

Merge branch 'kbd-refactor' into 'kbd-bitmaps'

Refactoring that splits Keyboard.c into a bunch of modules

See merge request !22
parents 17ff54bd 06239c02
No related branches found
No related tags found
No related merge requests found
Showing
with 1162 additions and 152 deletions
/*
LUFA Library
Copyright (C) Dean Camera, 2018.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/** \file
*
* Header file for Keyboard.c.
*/
#ifndef _KEYBOARD_H_
#define _KEYBOARD_H_
/* Includes: */
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/power.h>
#include <avr/interrupt.h>
#include <stdbool.h>
#include <string.h>
#include "Descriptors.h"
#include <LUFA/Drivers/Board/Joystick.h>
#include <LUFA/Drivers/Board/LEDs.h>
#include <LUFA/Drivers/Board/Buttons.h>
#include <LUFA/Drivers/USB/USB.h>
#include <LUFA/Platform/Platform.h>
/* Macros: */
/** LED mask for the library LED driver, to indicate that the USB interface is not ready. */
#define LEDMASK_USB_NOTREADY LEDS_LED1
/** LED mask for the library LED driver, to indicate that the USB interface is enumerating. */
#define LEDMASK_USB_ENUMERATING (LEDS_LED2 | LEDS_LED3)
/** LED mask for the library LED driver, to indicate that the USB interface is ready. */
#define LEDMASK_USB_READY (LEDS_LED2 | LEDS_LED4)
/** LED mask for the library LED driver, to indicate that an error has occurred in the USB interface. */
#define LEDMASK_USB_ERROR (LEDS_LED1 | LEDS_LED3)
/* Function Prototypes: */
void SetupHardware(void);
void EnterPowerOff(void);
void reset_keyboard_state(void);
void EVENT_USB_Device_Connect(void);
void EVENT_USB_Device_Disconnect(void);
void EVENT_USB_Device_ConfigurationChanged(void);
void EVENT_USB_Device_ControlRequest(void);
void EVENT_USB_Device_StartOfFrame(void);
bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
uint8_t* const ReportID,
const uint8_t ReportType,
void* ReportData,
uint16_t* const ReportSize);
void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
const uint8_t ReportID,
const uint8_t ReportType,
const void* ReportData,
const uint16_t ReportSize);
#endif
...@@ -17,8 +17,8 @@ BOARD = USBKEY ...@@ -17,8 +17,8 @@ BOARD = USBKEY
F_CPU = 16000000 F_CPU = 16000000
F_USB = $(F_CPU) F_USB = $(F_CPU)
OPTIMIZATION = s OPTIMIZATION = s
TARGET = Keyboard TARGET = keyboard
SRC = $(TARGET).c Descriptors.c i2c.c ssd1306.c $(LUFA_SRC_USB) $(LUFA_SRC_USBCLASS) SRC = $(TARGET).c descriptors.c i2c.c oled.c remote.c powersave.c backlight.c menu.c hid_report.c $(LUFA_SRC_USB) $(LUFA_SRC_USBCLASS)
LUFA_PATH = ./lufa-master/LUFA LUFA_PATH = ./lufa-master/LUFA
CC_FLAGS = -DUSE_LUFA_CONFIG_HEADER $(REFORM_KBD_OPTIONS) -IConfig/ CC_FLAGS = -DUSE_LUFA_CONFIG_HEADER $(REFORM_KBD_OPTIONS) -IConfig/
LD_FLAGS = -Wl,-u,vfprintf -lprintf_flt LD_FLAGS = -Wl,-u,vfprintf -lprintf_flt
......
# MNT Reform 2.0 Keyboard Firmware
## Code Structure
- `constants.h`: Define which keyboard variant you want to build for
- `keyboard.c`: Main entrypoint that includes the chosen matrix file (keyboard layout)
- `matrix.c`: Keyboard layout definition (default)
- `matrix_*.c`: Alternative layouts
- `backlight.c`: Keyboard backlight control
- `menu.c`: OLED Menu handling
- `oled.c`: OLED graphics control
- `remote.c`: Communication with MNT Reform motherboard LPC
- `hid_report.c`: USB HID raw report handler (commands sent by OS)
- `powersave.c`: Low power/sleep mode
- `descriptors.c`: USB HID descriptors
- `i2c.c`: Soft I2C master implementation (for OLED)
- `serial.c`: Soft UART implementation
- `font.c`: Bitmap data for OLED font and icons
## Dependencies
### Debian/Ubuntu
`apt install gcc-avr avr-libc dfu-programmer`
### Mac
*TODO: is this correct?*
```
brew tap osx-cross/avr
brew install avr-gcc
brew install dfu-programmer
```
## Hacking
To change the keyboard layout, adjust the `matrix` arrays in `keyboard.c`.
## Building
*Important*: Adjust the variant settings in `constants.h` do match your keyboard or laptop model.
I.e., if you are targeting the Standalone Keyboard, uncomment the `#define KBD_VARIANT_STANDALONE`.
To build, type:
`make`
To flash, put your keyboard into [flashing mode](https://mntre.com/reform2/handbook/parts.html#keyboard-firmware) and run:
`sudo ./flash.sh`
/*
MNT Reform 2.0 Keyboard Firmware
See keyboard.c for Copyright
SPDX-License-Identifier: MIT
*/
#include <avr/io.h>
#include <stdint.h>
#include "backlight.h"
int16_t pwmval = 8;
void kbd_brightness_init(void) {
// initial brightness
OCR0A = pwmval;
// clear/set, WGM1:0 set (Phase correct PWM)
TCCR0A = (1 << 7) | (0 << 6) | (0<<1) | 1;
// 3=WGM02, (cs02 2:0 -> clock/256 = 100)
TCCR0B = /*(1 << 3) |*/ (1 << 0) | (0 << 1) | 1;
}
void kbd_brightness_inc(void) {
pwmval+=2;
if (pwmval>=10) pwmval = 10;
OCR0A = pwmval;
}
void kbd_brightness_dec(void) {
pwmval-=2;
if (pwmval<0) pwmval = 0;
OCR0A = pwmval;
}
void kbd_brightness_set(int brite) {
pwmval = brite;
if (pwmval<0) pwmval = 0;
if (pwmval>=10) pwmval = 10;
OCR0A = pwmval;
}
/*
MNT Reform 2.0 Keyboard Firmware
See keyboard.c for Copyright
SPDX-License-Identifier: MIT
*/
#ifndef _BACKLIGHT_H_
#define _BACKLIGHT_H_
void kbd_brightness_init(void);
void kbd_brightness_inc(void);
void kbd_brightness_dec(void);
void kbd_brightness_set(int brite);
#endif
/*
MNT Reform 2.0 Keyboard Firmware
See keyboard.c for Copyright
SPDX-License-Identifier: MIT
*/
#ifndef _CONSTANTS_H_
#define _CONSTANTS_H_
#define KBD_FW_REV "R1 20210927"
//#define KBD_VARIANT_STANDALONE
#define KBD_VARIANT_QWERTY_US
//#define KBD_VARIANT_NEO2
//#define KBD_VARIANT_ELLEN
#define KBD_COLS 14
#define KBD_ROWS 6
#define KBD_MATRIX_SZ KBD_COLS * KBD_ROWS + 4
#define KBD_EDITOR_MARKER 0xfe,0xed,0xca,0xfe
#endif
/* /*
LUFA Library MNT Reform 2.0 Keyboard Firmware
Copyright (C) Dean Camera, 2018. See keyboard.c for Copyright
SPDX-License-Identifier: MIT
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/ */
/** \file /** \file
...@@ -36,8 +12,7 @@ ...@@ -36,8 +12,7 @@
*/ */
#include "Config/LUFAConfig.h" #include "Config/LUFAConfig.h"
#include "descriptors.h"
#include "Descriptors.h"
/** HID class report descriptor. This is a special descriptor constructed with values from the /** HID class report descriptor. This is a special descriptor constructed with values from the
......
/* /*
LUFA Library MNT Reform 2.0 Keyboard Firmware
Copyright (C) Dean Camera, 2018. See keyboard.c for Copyright
SPDX-License-Identifier: MIT
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/ */
/** \file /** \file
......
...@@ -2,6 +2,6 @@ ...@@ -2,6 +2,6 @@
dfu-programmer atmega32u4 erase --suppress-bootloader-mem dfu-programmer atmega32u4 erase --suppress-bootloader-mem
dfu-programmer atmega32u4 flash ./Keyboard.hex --suppress-bootloader-mem dfu-programmer atmega32u4 flash ./keyboard.hex --suppress-bootloader-mem
dfu-programmer atmega32u4 start dfu-programmer atmega32u4 start
/*
MNT Reform 2.0 Keyboard Firmware
See keyboard.c for Copyright
SPDX-License-Identifier: MIT
*/
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
const unsigned char font[] PROGMEM = { const unsigned char font[] PROGMEM = {
......
/*
MNT Reform 2.0 Keyboard Firmware
See keyboard.c for Copyright
SPDX-License-Identifier: MIT
*/
#include <stdint.h>
#include "backlight.h"
#include "hid_report.h"
#include "keyboard.h"
#include "menu.h"
#include "oled.h"
#include "powersave.h"
#include "remote.h"
// hid commands are all 4-letter, so they fit in a 32 bit integer
#define cmd(_s) (*(uint32_t *)(_s))
#define CMD_TEXT_FRAME cmd("OLED") // fill the screen with a single wall of text
#define CMD_ROW_INVERT cmd("OINV") // invert a line of text
#define CMD_REPORT_POWER cmd("RPRT") // ask for power stats report over UART
#define CMD_OLED_CLEAR cmd("WCLR") // clear the oled display
#define CMD_OLED_BITMAP cmd("WBIT") // (u16 offset, u8 bytes...) write raw bytes into the oled framebuffer
#define CMD_POWER_OFF cmd("PWR0") // turn off power rails
#define CMD_BACKLIGHT cmd("LITE") // keyboard backlight level
#define CMD_UART_ON cmd("UAR1") // uart reporting on
#define CMD_UART_OFF cmd("UAR0") // uart reporting off
void hid_report_cmd(uint8_t* data) {
const uint32_t command = *(uint32_t *)data;
if (command == CMD_TEXT_FRAME) {
gfx_on();
for (int y=0; y<4; y++) {
for (int x=0; x<21; x++) {
gfx_poke(x,y,data[4+y*21+x]);
}
}
gfx_flush();
}
else if (command == CMD_ROW_INVERT) {
gfx_clear_invert();
gfx_invert_row(data[4]-'0');
}
else if (command == CMD_BACKLIGHT) {
char brite = data[4]-'0';
brite++;
if (brite<=1) brite=0;
if (brite>9) brite=9;
kbd_brightness_set(brite);
}
else if (command == CMD_POWER_OFF) {
anim_goodbye();
remote_turn_off_som();
keyboard_power_off();
reset_keyboard_state();
}
else if (command == CMD_UART_ON) {
remote_enable_som_uart();
}
else if (command == CMD_UART_OFF) {
remote_disable_som_uart();
}
else if (command == CMD_REPORT_POWER) {
remote_report_voltages();
}
else if (command == CMD_OLED_BITMAP) {
matrix_render_direct(data+4);
}
else if (command == CMD_OLED_BITMAP) {
gfx_clear();
gfx_flush();
}
}
/*
MNT Reform 2.0 Keyboard Firmware
See keyboard.c for Copyright
SPDX-License-Identifier: MIT
*/
#ifndef _HID_REPORT_H_
#define _HID_REPORT_H_
#include <stdint.h>
void hid_report_cmd(uint8_t* data);
#endif
/*
MNT Reform 2.0 Keyboard Firmware
See keyboard.c for Copyright
SPDX-License-Identifier: MIT
*/
#include <util/twi.h> #include <util/twi.h>
#include <avr/io.h> #include <avr/io.h>
#include <stdlib.h> #include <stdlib.h>
......
#pragma once /*
MNT Reform 2.0 Keyboard Firmware
See keyboard.c for Copyright
SPDX-License-Identifier: MIT
*/
#ifndef _I2C_H_
#define _I2C_H_
#include <stdint.h> #include <stdint.h>
...@@ -36,7 +43,6 @@ static inline unsigned char i2c_start_write(unsigned char addr) { ...@@ -36,7 +43,6 @@ static inline unsigned char i2c_start_write(unsigned char addr) {
return i2c_master_start((addr << 1) | I2C_WRITE); return i2c_master_start((addr << 1) | I2C_WRITE);
} }
// from SSD1306 scrips
extern unsigned char i2c_rep_start(unsigned char addr); extern unsigned char i2c_rep_start(unsigned char addr);
extern void i2c_start_wait(unsigned char addr); extern void i2c_start_wait(unsigned char addr);
extern unsigned char i2c_readAck(void); extern unsigned char i2c_readAck(void);
...@@ -44,3 +50,5 @@ extern unsigned char i2c_readNak(void); ...@@ -44,3 +50,5 @@ extern unsigned char i2c_readNak(void);
extern unsigned char i2c_read(unsigned char ack); extern unsigned char i2c_read(unsigned char ack);
#define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak(); #define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak();
#endif
/*
MNT Reform 2.0 Keyboard Firmware
See keyboard.c for Copyright
SPDX-License-Identifier: MIT
*/
#ifndef _KEYBOARD_H_
#define _KEYBOARD_H_
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/power.h>
#include <avr/interrupt.h>
#include <stdbool.h>
#include <string.h>
#include "Config/LUFAConfig.h"
#include "descriptors.h"
#include <LUFA/Drivers/USB/USB.h>
#include <LUFA/Platform/Platform.h>
// some GPIO macros
#define output_low(port,pin) port &= ~(1<<pin)
#define output_high(port,pin) port |= (1<<pin)
#define set_input(portdir,pin) portdir &= ~(1<<pin)
#define set_output(portdir,pin) portdir |= (1<<pin)
// Top row, left to right
#define MATRIX_DEFAULT_ROW_1 \
KEY_ESCAPE,\
KEY_F1,\
KEY_F2,\
KEY_F3,\
KEY_F4,\
KEY_F5,\
KEY_F6,\
KEY_F7,\
KEY_F8,\
KEY_F9,\
KEY_F10,\
KEY_F11,\
KEY_F12,\
KEY_CIRCLE
// Second row
#define MATRIX_DEFAULT_ROW_2 \
KEY_GRAVE_ACCENT_AND_TILDE,\
KEY_1,\
KEY_2,\
KEY_3,\
KEY_4,\
KEY_5,\
KEY_6,\
KEY_7,\
KEY_8,\
KEY_9,\
KEY_0,\
KEY_MINUS_AND_UNDERSCORE,\
KEY_EQUAL_AND_PLUS,\
KEY_BACKSPACE
// Third row
#define MATRIX_DEFAULT_ROW_3 \
KEY_TAB,\
KEY_Q,\
KEY_W,\
KEY_E,\
KEY_R,\
KEY_T,\
KEY_Y,\
KEY_U,\
KEY_I,\
KEY_O,\
KEY_P,\
KEY_OPENING_BRACKET_AND_OPENING_BRACE,\
KEY_CLOSING_BRACKET_AND_CLOSING_BRACE,\
KEY_BACKSLASH_AND_PIPE
// Fourth row
#define MATRIX_DEFAULT_ROW_4 \
HID_KEYBOARD_SC_LEFT_CONTROL,\
HID_KEYBOARD_SC_APPLICATION,\
KEY_A,\
KEY_S,\
KEY_D,\
KEY_F,\
KEY_G,\
KEY_H,\
KEY_J,\
KEY_K,\
KEY_L,\
KEY_SEMICOLON_AND_COLON,\
KEY_APOSTROPHE_AND_QUOTE,\
KEY_ENTER
// Fifth row
#define MATRIX_DEFAULT_ROW_5 \
HID_KEYBOARD_SC_LEFT_SHIFT,\
HID_KEYBOARD_SC_NON_US_BACKSLASH_AND_PIPE,\
KEY_Z,\
KEY_X,\
KEY_C,\
KEY_V,\
KEY_B,\
KEY_N,\
KEY_M,\
HID_KEYBOARD_SC_COMMA_AND_LESS_THAN_SIGN,\
HID_KEYBOARD_SC_DOT_AND_GREATER_THAN_SIGN,\
KEY_SLASH_AND_QUESTION_MARK,\
HID_KEYBOARD_SC_UP_ARROW,\
HID_KEYBOARD_SC_RIGHT_SHIFT
// Sixth row
#define MATRIX_DEFAULT_ROW_6 \
HID_KEYBOARD_SC_EXECUTE,\
HID_KEYBOARD_SC_LEFT_GUI,\
HID_KEYBOARD_SC_RIGHT_CONTROL,\
KEY_SPACE,\
HID_KEYBOARD_SC_LEFT_ALT,\
HID_KEYBOARD_SC_RIGHT_ALT,\
KEY_SPACE,\
HID_KEYBOARD_SC_PAGE_UP,\
HID_KEYBOARD_SC_PAGE_DOWN,\
HID_KEYBOARD_SC_LEFT_ARROW,\
HID_KEYBOARD_SC_DOWN_ARROW,\
HID_KEYBOARD_SC_RIGHT_ARROW
void setup_hardware(void);
void reset_keyboard_state(void);
// LUFA USB handlers
void EVENT_USB_Device_Connect(void);
void EVENT_USB_Device_Disconnect(void);
void EVENT_USB_Device_ConfigurationChanged(void);
void EVENT_USB_Device_ControlRequest(void);
void EVENT_USB_Device_StartOfFrame(void);
bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
uint8_t* const ReportID,
const uint8_t ReportType,
void* ReportData,
uint16_t* const ReportSize);
void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
const uint8_t ReportID,
const uint8_t ReportType,
const void* ReportData,
const uint16_t ReportSize);
#endif
/*
MNT Reform 2.0 Keyboard Firmware
See keyboard.c for Copyright
SPDX-License-Identifier: MIT
*/
#include "keyboard.h"
// Every line of `matrix` is a row of the keyboard, starting from the top.
// Check keyboard.h for the definitions of the default rows.
uint8_t matrix[KBD_MATRIX_SZ] = {
MATRIX_DEFAULT_ROW_1,
MATRIX_DEFAULT_ROW_2,
MATRIX_DEFAULT_ROW_3,
MATRIX_DEFAULT_ROW_4,
MATRIX_DEFAULT_ROW_5,
// Marker for layout editor (FIXME)
KBD_EDITOR_MARKER
};
// When holding down HYPER
uint8_t matrix_fn[KBD_MATRIX_SZ] = {
// Media keys (not working, FIXME)
KEY_ESCAPE,
KEY_F1,
KEY_F2,
KEY_F3,
KEY_F4,
KEY_F5,
KEY_F6,
HID_KEYBOARD_SC_MEDIA_BACKWARD,
HID_KEYBOARD_SC_MEDIA_PLAY,
HID_KEYBOARD_SC_MEDIA_FORWARD,
173, // mute
174, // volume down
175, // volume up
KEY_CIRCLE,
MATRIX_DEFAULT_ROW_2,
MATRIX_DEFAULT_ROW_3,
MATRIX_DEFAULT_ROW_4,
MATRIX_DEFAULT_ROW_5,
MATRIX_DEFAULT_ROW_6
};
// Second layer (toggled by HYPER+CIRCLE)
uint8_t matrix_fn_toggled[KBD_MATRIX_SZ] = {
// Custom top row
KEY_ESCAPE,
KEY_F1,
KEY_F2,
KEY_F3,
KEY_F4,
KEY_F5,
KEY_F6,
HID_KEYBOARD_SC_MEDIA_BACKWARD,
HID_KEYBOARD_SC_MEDIA_PLAY,
HID_KEYBOARD_SC_MEDIA_FORWARD,
173,
174,
175,
KEY_CIRCLE,
MATRIX_DEFAULT_ROW_2,
MATRIX_DEFAULT_ROW_3,
MATRIX_DEFAULT_ROW_4,
MATRIX_DEFAULT_ROW_5,
MATRIX_DEFAULT_ROW_6
};
/*
MNT Reform 2.0 Keyboard Firmware
See keyboard.c for Copyright
SPDX-License-Identifier: MIT
*/
#include "keyboard.h"
// Every line of `matrix` is a row of the keyboard, starting from the top.
// Check keyboard.h for the definitions of the default rows.
uint8_t matrix[KBD_MATRIX_SZ] = {
MATRIX_DEFAULT_ROW_1,
MATRIX_DEFAULT_ROW_2,
MATRIX_DEFAULT_ROW_3,
MATRIX_DEFAULT_ROW_4,
MATRIX_DEFAULT_ROW_5,
// Custom row six
HID_KEYBOARD_SC_LEFT_CONTROL,
HID_KEYBOARD_SC_LEFT_ALT,
HID_KEYBOARD_SC_LEFT_GUI,
KEY_SPACE,
KEY_SPACE,
KEY_SPACE,
KEY_SPACE,
HID_KEYBOARD_SC_RIGHT_ALT,
HID_KEYBOARD_SC_EXECUTE,
HID_KEYBOARD_SC_LEFT_ARROW,
HID_KEYBOARD_SC_DOWN_ARROW,
HID_KEYBOARD_SC_RIGHT_ARROW,
// Marker for layout editor (FIXME)
KBD_EDITOR_MARKER
};
// When holding down HYPER
uint8_t matrix_fn[KBD_MATRIX_SZ] = {
// Custom top row
KEY_ESCAPE,
KEY_F1,
KEY_F2,
KEY_F3,
KEY_F4,
KEY_F5,
KEY_F6,
HID_KEYBOARD_SC_MEDIA_BACKWARD,
HID_KEYBOARD_SC_MEDIA_PLAY,
HID_KEYBOARD_SC_MEDIA_FORWARD,
173, // mute
174, // volume down
175, // volume up
KEY_CIRCLE,
MATRIX_DEFAULT_ROW_2,
MATRIX_DEFAULT_ROW_3,
MATRIX_DEFAULT_ROW_4,
// Custom row 5
HID_KEYBOARD_SC_LEFT_SHIFT,
HID_KEYBOARD_SC_NON_US_BACKSLASH_AND_PIPE,
KEY_Z,
KEY_X,
KEY_C,
KEY_V,
KEY_B,
KEY_N,
KEY_M,
HID_KEYBOARD_SC_COMMA_AND_LESS_THAN_SIGN,
HID_KEYBOARD_SC_DOT_AND_GREATER_THAN_SIGN,
KEY_SLASH_AND_QUESTION_MARK,
HID_KEYBOARD_SC_PAGE_UP,
HID_KEYBOARD_SC_RIGHT_SHIFT,
// Custom row 6
HID_KEYBOARD_SC_LEFT_CONTROL,
HID_KEYBOARD_SC_LEFT_ALT,
HID_KEYBOARD_SC_LEFT_GUI,
KEY_SPACE,
KEY_SPACE,
KEY_SPACE,
KEY_SPACE,
HID_KEYBOARD_SC_RIGHT_ALT,
HID_KEYBOARD_SC_EXECUTE,
HID_KEYBOARD_SC_HOME,
HID_KEYBOARD_SC_PAGE_DOWN,
HID_KEYBOARD_SC_END
};
// Second layer (toggled by HYPER+CIRCLE)
uint8_t matrix_fn_toggled[KBD_MATRIX_SZ] = {
// Custom top row
KEY_ESCAPE,
KEY_F1,
KEY_F2,
KEY_F3,
KEY_F4,
KEY_F5,
KEY_F6,
HID_KEYBOARD_SC_MEDIA_BACKWARD,
HID_KEYBOARD_SC_MEDIA_PLAY,
HID_KEYBOARD_SC_MEDIA_FORWARD,
173,
174,
175,
KEY_CIRCLE,
MATRIX_DEFAULT_ROW_2,
MATRIX_DEFAULT_ROW_3,
MATRIX_DEFAULT_ROW_4,
MATRIX_DEFAULT_ROW_5,
// Custom row six
HID_KEYBOARD_SC_LEFT_CONTROL,
HID_KEYBOARD_SC_LEFT_ALT,
HID_KEYBOARD_SC_LEFT_GUI,
KEY_SPACE,
KEY_SPACE,
KEY_SPACE,
KEY_SPACE,
HID_KEYBOARD_SC_RIGHT_ALT,
HID_KEYBOARD_SC_EXECUTE,
HID_KEYBOARD_SC_LEFT_ARROW,
HID_KEYBOARD_SC_DOWN_ARROW,
HID_KEYBOARD_SC_RIGHT_ARROW
};
/*
MNT Reform 2.0 Keyboard Firmware
See keyboard.c for Copyright
SPDX-License-Identifier: MIT
*/
#include "backlight.h"
#include "constants.h"
#include "keyboard.h"
#include "menu.h"
#include "oled.h"
#include "powersave.h"
#include "remote.h"
#include "scancodes.h"
int current_menu_y = 0;
int current_scroll_y = 0;
#ifdef KBD_VARIANT_STANDALONE
#define MENU_NUM_ITEMS 4
const MenuItem menu_items[] = {
{ "Exit Menu ESC", KEY_ESCAPE },
{ "Key Backlight- F1", KEY_F1 },
{ "Key Backlight+ F2", KEY_F2 },
{ "System Status s", KEY_S }
};
#else
#define MENU_NUM_ITEMS 9
const MenuItem menu_items[] = {
{ "Exit Menu ESC", KEY_ESCAPE },
{ "Power On 1", KEY_1 },
{ "Power Off 0", KEY_0 },
{ "Reset r", KEY_R },
{ "Battery Status b", KEY_B },
{ "Key Backlight- F1", KEY_F1 },
{ "Key Backlight+ F2", KEY_F2 },
{ "Wake SPC", KEY_SPACE },
{ "System Status s", KEY_S },
// Only needed for debugging.
// The keyboard will go to sleep when turning off
// main system power.
{ "KBD Power-Off p", KEY_P },
};
#endif
void reset_and_render_menu() {
current_scroll_y = 0;
current_menu_y = 0;
render_menu(current_scroll_y);
}
void render_menu(int y) {
gfx_clear();
gfx_invert_row(current_menu_y-y);
for (int i=0; i<MENU_NUM_ITEMS; i++) {
gfx_poke_str(0,i-y,menu_items[i].title);
}
gfx_on();
gfx_flush();
}
int execute_menu_function(int y) {
if (y>=0 && y<MENU_NUM_ITEMS) {
return execute_meta_function(menu_items[y].keycode);
}
return execute_meta_function(KEY_ESCAPE);
}
// returns 1 for navigation function (stay in meta mode), 0 for terminal function
int execute_meta_function(int keycode) {
if (keycode == KEY_0) {
// TODO: are you sure?
anim_goodbye();
remote_turn_off_som();
keyboard_power_off();
reset_keyboard_state();
// Directly enter menu again
return 2;
}
else if (keycode == KEY_1) {
if (remote_turn_on_som()) {
anim_hello();
}
kbd_brightness_init();
return 0;
}
else if (keycode == KEY_R) {
// TODO: are you sure?
remote_reset_som();
}
else if (keycode == KEY_SPACE) {
remote_wake_som();
}
/*else if (keycode == KEY_V) {
remote_turn_off_aux();
}*/
else if (keycode == KEY_B) {
remote_get_voltages();
return 0;
}
else if (keycode == KEY_S) {
remote_get_status();
return 0;
}
else if (keycode == KEY_F1) {
kbd_brightness_dec();
return 1;
}
else if (keycode == KEY_F2) {
kbd_brightness_inc();
return 1;
}
else if (keycode == HID_KEYBOARD_SC_UP_ARROW) {
current_menu_y--;
if (current_menu_y<0) current_menu_y = 0;
if (current_menu_y<=current_scroll_y) current_scroll_y--;
if (current_scroll_y<0) current_scroll_y = 0;
render_menu(current_scroll_y);
return 1;
}
else if (keycode == HID_KEYBOARD_SC_DOWN_ARROW) {
current_menu_y++;
if (current_menu_y>=MENU_NUM_ITEMS) current_menu_y = MENU_NUM_ITEMS-1;
if (current_menu_y>=current_scroll_y+3) current_scroll_y++;
render_menu(current_scroll_y);
return 1;
}
else if (keycode == KEY_ENTER) {
return execute_menu_function(current_menu_y);
}
else if (keycode == KEY_ESCAPE) {
gfx_clear();
gfx_flush();
}
gfx_clear();
gfx_flush();
return 0;
}
void anim_hello(void) {
gfx_clear();
gfx_on();
for (int y=0; y<3; y++) {
for (int x=0; x<12; x++) {
gfx_poke(x+4,y+1,(5+y)*32+x);
gfx_flush();
}
}
for (int y=0; y<0xff; y++) {
gfx_contrast(y);
Delay_MS(2);
}
for (int y=0; y<0xff; y++) {
gfx_contrast(0xff-y);
Delay_MS(2);
}
}
void anim_goodbye(void) {
gfx_clear();
gfx_on();
for (int y=0; y<3; y++) {
for (int x=0; x<12; x++) {
gfx_poke(x+4,y+1,(5+y)*32+x);
}
}
for (int y=0; y<3; y++) {
for (int x=0; x<12; x++) {
gfx_poke(x+4,y+1,' ');
gfx_flush();
}
}
gfx_off();
}
/*
MNT Reform 2.0 Keyboard Firmware
See keyboard.c for Copyright
SPDX-License-Identifier: MIT
*/
#ifndef _MENU_H_
#define _MENU_H_
typedef struct MenuItem {
char* title;
int keycode;
} MenuItem;
void reset_and_render_menu(void);
void render_menu(int y);
int execute_menu_function(int y);
int execute_meta_function(int keycode);
void anim_hello(void);
void anim_goodbye(void);
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment