Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make WPM able to sync between keyboard halves on Ergodox Infinity. #9526

Merged
merged 2 commits into from
Sep 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 57 additions & 28 deletions keyboards/ergodox_infinity/ergodox_infinity.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
#include "lcd_backlight.h"
#endif

#ifdef WPM_ENABLE
# include "serial_link/protocol/transport.h"
# include "wpm.h"

MASTER_TO_ALL_SLAVES_OBJECT(current_wpm, uint8_t);
static remote_object_t* remote_objects[] = {
REMOTE_OBJECT(current_wpm),
};
static uint8_t last_sent_wpm = 0;
#endif

void init_serial_link_hal(void) {
PORTA->PCR[1] = PORTx_PCRn_PE | PORTx_PCRn_PS | PORTx_PCRn_PFE | PORTx_PCRn_MUX(2);
PORTA->PCR[2] = PORTx_PCRn_DSE | PORTx_PCRn_SRE | PORTx_PCRn_MUX(2);
Expand Down Expand Up @@ -39,30 +50,30 @@ void init_serial_link_hal(void) {
// Which will reduce the brightness range
#define PRESCALAR_DEFINE 0
void lcd_backlight_hal_init(void) {
// Setup Backlight
// Setup Backlight
SIM->SCGC6 |= SIM_SCGC6_FTM0;
FTM0->CNT = 0; // Reset counter

// PWM Period
// 16-bit maximum
FTM0->MOD = 0xFFFF;
// PWM Period
// 16-bit maximum
FTM0->MOD = 0xFFFF;

// Set FTM to PWM output - Edge Aligned, Low-true pulses
// Set FTM to PWM output - Edge Aligned, Low-true pulses
#define CNSC_MODE FTM_SC_CPWMS | FTM_SC_PS(4) | FTM_SC_CLKS(0)
CHANNEL_RED.CnSC = CNSC_MODE;
CHANNEL_GREEN.CnSC = CNSC_MODE;
CHANNEL_BLUE.CnSC = CNSC_MODE;
CHANNEL_RED.CnSC = CNSC_MODE;
CHANNEL_GREEN.CnSC = CNSC_MODE;
CHANNEL_BLUE.CnSC = CNSC_MODE;

// System clock, /w prescalar setting
FTM0->SC = FTM_SC_CLKS(1) | FTM_SC_PS(PRESCALAR_DEFINE);
// System clock, /w prescalar setting
FTM0->SC = FTM_SC_CLKS(1) | FTM_SC_PS(PRESCALAR_DEFINE);

CHANNEL_RED.CnV = 0;
CHANNEL_GREEN.CnV = 0;
CHANNEL_BLUE.CnV = 0;
CHANNEL_RED.CnV = 0;
CHANNEL_GREEN.CnV = 0;
CHANNEL_BLUE.CnV = 0;

RGB_PORT_GPIO->PDDR |= (1 << RED_PIN);
RGB_PORT_GPIO->PDDR |= (1 << GREEN_PIN);
RGB_PORT_GPIO->PDDR |= (1 << BLUE_PIN);
RGB_PORT_GPIO->PDDR |= (1 << RED_PIN);
RGB_PORT_GPIO->PDDR |= (1 << GREEN_PIN);
RGB_PORT_GPIO->PDDR |= (1 << BLUE_PIN);

#define RGB_MODE PORTx_PCRn_SRE | PORTx_PCRn_DSE | PORTx_PCRn_MUX(4)
RGB_PORT->PCR[RED_PIN] = RGB_MODE;
Expand Down Expand Up @@ -94,9 +105,9 @@ static uint16_t cie_lightness(uint16_t v) {
}

void lcd_backlight_hal_color(uint16_t r, uint16_t g, uint16_t b) {
CHANNEL_RED.CnV = cie_lightness(r);
CHANNEL_GREEN.CnV = cie_lightness(g);
CHANNEL_BLUE.CnV = cie_lightness(b);
CHANNEL_RED.CnV = cie_lightness(r);
CHANNEL_GREEN.CnV = cie_lightness(g);
CHANNEL_BLUE.CnV = cie_lightness(b);
}

__attribute__ ((weak))
Expand All @@ -109,21 +120,39 @@ void matrix_scan_user(void) {


void matrix_init_kb(void) {
// put your keyboard start-up code here
// runs once when the firmware starts up
// put your keyboard start-up code here
// runs once when the firmware starts up

matrix_init_user();
// The backlight always has to be initialized, otherwise it will stay lit
matrix_init_user();
// The backlight always has to be initialized, otherwise it will stay lit
#ifndef VISUALIZER_ENABLE
lcd_backlight_hal_init();
lcd_backlight_hal_init();
#endif
#ifdef WPM_ENABLE
add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*));
#endif
}

void matrix_scan_kb(void) {
// put your looping keyboard code here
// runs every cycle (a lot)

matrix_scan_user();
// put your looping keyboard code here
// runs every cycle (a lot)

#ifdef WPM_ENABLE
if (is_serial_link_master()) {
uint8_t current_wpm = get_current_wpm();
if (current_wpm != last_sent_wpm) {
*begin_write_current_wpm() = current_wpm;
end_write_current_wpm();
last_sent_wpm = current_wpm;
}
} else if (is_serial_link_connected()) {
uint8_t* new_wpm = read_current_wpm();
if (new_wpm) {
set_current_wpm(*new_wpm);
}
}
#endif
matrix_scan_user();
}

bool is_keyboard_master(void) {
Expand Down