From ba8a3d37c3a03c420799ce11d12a1d380ad4e54b Mon Sep 17 00:00:00 2001 From: Selene ToyKeeper Date: Tue, 18 Jul 2023 12:28:24 -0600 Subject: lowercase'd sofirn-lt1s-pro files --- hwdef-Sofirn_LT1S-Pro.c | 269 ------------------------ hwdef-Sofirn_LT1S-Pro.h | 153 -------------- hwdef-sofirn-lt1s-pro.c | 269 ++++++++++++++++++++++++ hwdef-sofirn-lt1s-pro.h | 153 ++++++++++++++ spaghetti-monster/anduril/cfg-sofirn-lt1s-pro.h | 2 +- 5 files changed, 423 insertions(+), 423 deletions(-) delete mode 100644 hwdef-Sofirn_LT1S-Pro.c delete mode 100644 hwdef-Sofirn_LT1S-Pro.h create mode 100644 hwdef-sofirn-lt1s-pro.c create mode 100644 hwdef-sofirn-lt1s-pro.h diff --git a/hwdef-Sofirn_LT1S-Pro.c b/hwdef-Sofirn_LT1S-Pro.c deleted file mode 100644 index 6fe0fef..0000000 --- a/hwdef-Sofirn_LT1S-Pro.c +++ /dev/null @@ -1,269 +0,0 @@ -// BLF LT1S Pro hwdef functions -// Copyright (C) 2023 Selene ToyKeeper -// SPDX-License-Identifier: GPL-3.0-or-later -#pragma once - - -void set_level_red(uint8_t level); -void set_level_white_blend(uint8_t level); -void set_level_auto_2ch_blend(uint8_t level); -void set_level_auto_3ch_blend(uint8_t level); -void set_level_red_white_blend(uint8_t level); - -bool gradual_tick_red(uint8_t gt); -bool gradual_tick_white_blend(uint8_t gt); -bool gradual_tick_auto_2ch_blend(uint8_t gt); -bool gradual_tick_auto_3ch_blend(uint8_t gt); -bool gradual_tick_red_white_blend(uint8_t gt); - - -Channel channels[] = { - { // manual blend of warm and cool white - .set_level = set_level_white_blend, - .gradual_tick = gradual_tick_white_blend, - .has_args = 1 - }, - { // auto blend from warm white to cool white - .set_level = set_level_auto_2ch_blend, - .gradual_tick = gradual_tick_auto_2ch_blend, - .has_args = 0 - }, - { // auto blend from red to warm white to cool white - .set_level = set_level_auto_3ch_blend, - .gradual_tick = gradual_tick_auto_3ch_blend, - .has_args = 0 - }, - { // red only - .set_level = set_level_red, - .gradual_tick = gradual_tick_red, - .has_args = 0 - }, - { // manual white blend + adjustable red - .set_level = set_level_red_white_blend, - .gradual_tick = gradual_tick_red_white_blend, - .has_args = 1 - } -}; - - -// calculate a 3-channel "auto tint" blend -// (like red -> warm white -> cool white) -// results are placed in *a, *b, and *c vars -// level : ramp level to convert into 3 channel levels -// (assumes ramp table is "pwm1_levels") -void calc_auto_3ch_blend( - PWM_DATATYPE *a, - PWM_DATATYPE *b, - PWM_DATATYPE *c, - uint8_t level) { - - PWM_DATATYPE vpwm = PWM_GET(pwm1_levels, level); - - // tint goes from 0 (red) to 127 (warm white) to 255 (cool white) - uint8_t mytint; - mytint = 255 * (uint16_t)level / RAMP_SIZE; - - // red is high at 0, low at 255 (linear) - *a = (((PWM_DATATYPE2)(255 - mytint) - * (PWM_DATATYPE2)vpwm) + 127) / 255; - // warm white is low at 0 and 255, high at 127 (linear triangle) - *b = (((PWM_DATATYPE2)triangle_wave(mytint) - * (PWM_DATATYPE2)vpwm) + 127) / 255; - // cool white is low at 0, high at 255 (linear) - *c = (((PWM_DATATYPE2)mytint - * (PWM_DATATYPE2)vpwm) + 127) / 255; - -} - - -// single set of LEDs with 1 power channel and dynamic PWM -void set_level_red(uint8_t level) { - if (level == 0) { - RED_PWM_LVL = 0; - PWM_CNT = 0; // reset phase - } else { - level --; // PWM array index = level - 1 - RED_PWM_LVL = PWM_GET(pwm1_levels, level); - // pulse frequency modulation, a.k.a. dynamic PWM - PWM_TOP = PWM_GET(pwm_tops, level); - // force reset phase when turning on from zero - // (because otherwise the initial response is inconsistent) - if (! actual_level) PWM_CNT = 0; - } -} - - -// warm + cool blend w/ dynamic PWM -void set_level_white_blend(uint8_t level) { - if (level == 0) { - WARM_PWM_LVL = 0; - COOL_PWM_LVL = 0; - PWM_CNT = 0; // reset phase - return; - } - - level --; // PWM array index = level - 1 - - PWM_DATATYPE warm_PWM, cool_PWM; - PWM_DATATYPE brightness = PWM_GET(pwm1_levels, level); - PWM_DATATYPE top = PWM_GET(pwm_tops, level); - uint8_t blend = cfg.channel_mode_args[cfg.channel_mode]; - - calc_2ch_blend(&warm_PWM, &cool_PWM, brightness, top, blend); - - WARM_PWM_LVL = warm_PWM; - COOL_PWM_LVL = cool_PWM; - PWM_TOP = top; - if (! actual_level) PWM_CNT = 0; // reset phase -} - - -// same as white blend, but tint is calculated from the ramp level -void set_level_auto_2ch_blend(uint8_t level) { - if (level == 0) { - WARM_PWM_LVL = 0; - COOL_PWM_LVL = 0; - PWM_CNT = 0; // reset phase - return; - } - - level --; // PWM array index = level - 1 - - PWM_DATATYPE warm_PWM, cool_PWM; - PWM_DATATYPE brightness = PWM_GET(pwm1_levels, level); - PWM_DATATYPE top = PWM_GET(pwm_tops, level); - uint8_t blend = 255 * (uint16_t)level / RAMP_SIZE; - - calc_2ch_blend(&warm_PWM, &cool_PWM, brightness, top, blend); - - WARM_PWM_LVL = warm_PWM; - COOL_PWM_LVL = cool_PWM; - PWM_TOP = top; - if (! actual_level) PWM_CNT = 0; // reset phase -} - - -// "auto tint" channel mode with dynamic PWM -void set_level_auto_3ch_blend(uint8_t level) { - if (level == 0) { - WARM_PWM_LVL = 0; - COOL_PWM_LVL = 0; - RED_PWM_LVL = 0; - PWM_CNT = 0; // reset phase - return; - } - - level --; // PWM array index = level - 1 - - PWM_DATATYPE a, b, c; - calc_auto_3ch_blend(&a, &b, &c, level); - - // pulse frequency modulation, a.k.a. dynamic PWM - uint16_t top = PWM_GET(pwm_tops, level); - - RED_PWM_LVL = a; - WARM_PWM_LVL = b; - COOL_PWM_LVL = c; - PWM_TOP = top; - if (! actual_level) PWM_CNT = 0; -} - - -// "white + red" channel mode -void set_level_red_white_blend(uint8_t level) { - // set the warm+cool white LEDs first - cfg.channel_mode = CM_WHITE; - set_level_white_blend(level); - cfg.channel_mode = CM_WHITE_RED; - - if (level == 0) { - RED_PWM_LVL = 0; - PWM_CNT = 0; // reset phase - return; - } - - level --; // PWM array index = level - 1 - PWM_DATATYPE vpwm = PWM_GET(pwm1_levels, level); - - // set the red LED as a ratio of the white output level - // 0 = no red - // 255 = red at 100% of white channel PWM - uint8_t ratio = cfg.channel_mode_args[cfg.channel_mode]; - - RED_PWM_LVL = (((PWM_DATATYPE2)ratio * (PWM_DATATYPE2)vpwm) + 127) / 255; - if (! actual_level) PWM_CNT = 0; // reset phase -} - - -///// "gradual tick" functions for smooth thermal regulation ///// - -///// bump each channel toward a target value ///// -bool gradual_adjust(uint16_t red, uint16_t warm, uint16_t cool) { - GRADUAL_ADJUST_SIMPLE(red, RED_PWM_LVL ); - GRADUAL_ADJUST_SIMPLE(warm, WARM_PWM_LVL); - GRADUAL_ADJUST_SIMPLE(cool, COOL_PWM_LVL); - - // check for completion - if ((red == RED_PWM_LVL ) - && (warm == WARM_PWM_LVL) - && (cool == COOL_PWM_LVL)) { - return true; // done - } - return false; // not done yet -} - -bool gradual_tick_red(uint8_t gt) { - uint16_t red = PWM_GET(pwm1_levels, gt); - return gradual_adjust(red, 0, 0); -} - - -bool gradual_tick_white_blend(uint8_t gt) { - // figure out what exact PWM levels we're aiming for - PWM_DATATYPE warm_PWM, cool_PWM; - PWM_DATATYPE brightness = PWM_GET(pwm1_levels, gt); - PWM_DATATYPE top = PWM_GET(pwm_tops, gt); - uint8_t blend = cfg.channel_mode_args[cfg.channel_mode]; - - calc_2ch_blend(&warm_PWM, &cool_PWM, brightness, top, blend); - - return gradual_adjust(0, warm_PWM, cool_PWM); -} - - -// same as white blend, but tint is calculated from the ramp level -bool gradual_tick_auto_2ch_blend(uint8_t gt) { - // figure out what exact PWM levels we're aiming for - PWM_DATATYPE warm_PWM, cool_PWM; - PWM_DATATYPE brightness = PWM_GET(pwm1_levels, gt); - PWM_DATATYPE top = PWM_GET(pwm_tops, gt); - uint8_t blend = 255 * (uint16_t)gt / RAMP_SIZE; - - calc_2ch_blend(&warm_PWM, &cool_PWM, brightness, top, blend); - - return gradual_adjust(0, warm_PWM, cool_PWM); -} - - -bool gradual_tick_auto_3ch_blend(uint8_t gt) { - // figure out what exact PWM levels we're aiming for - PWM_DATATYPE red, warm, cool; - calc_auto_3ch_blend(&red, &warm, &cool, gt); - return gradual_adjust(red, warm, cool); -} - - -bool gradual_tick_red_white_blend(uint8_t gt) { - // figure out what exact PWM levels we're aiming for - PWM_DATATYPE red, warm, cool; - PWM_DATATYPE brightness = PWM_GET(pwm1_levels, gt); - PWM_DATATYPE top = PWM_GET(pwm_tops, gt); - uint8_t blend = cfg.channel_mode_args[CM_WHITE]; - uint8_t ratio = cfg.channel_mode_args[cfg.channel_mode]; - - red = (((PWM_DATATYPE2)ratio * (PWM_DATATYPE2)brightness) + 127) / 255; - calc_2ch_blend(&warm, &cool, brightness, top, blend); - - return gradual_adjust(red, warm, cool); -} - diff --git a/hwdef-Sofirn_LT1S-Pro.h b/hwdef-Sofirn_LT1S-Pro.h deleted file mode 100644 index fa30a09..0000000 --- a/hwdef-Sofirn_LT1S-Pro.h +++ /dev/null @@ -1,153 +0,0 @@ -// BLF LT1S Pro driver layout using the Attiny1616 -// Copyright (C) 2022-2023 (FIXME) -// SPDX-License-Identifier: GPL-3.0-or-later -#pragma once - -/* - * Driver pinout: - * eSwitch: PA5 - * Aux LED: PB5 - * WW PWM: PB0 (TCA0 WO0) - * CW PWM: PB1 (TCA0 WO1) - * Red PWM: PB2 (TCA0 WO2) - * Voltage: VCC - */ - -#define ATTINY 1616 -#include - -#define HWDEF_C_FILE hwdef-Sofirn_LT1S-Pro.c - -// channel modes: -// * 0. warm/cool white blend -// * 1. auto 2ch white blend (warm -> cool by ramp level) -// * 2. auto 3ch blend (red -> warm -> cool by ramp level) -// * 3. red only -// * 4. red + white blend -#define NUM_CHANNEL_MODES 5 -enum channel_modes_e { - CM_WHITE = 0, - CM_AUTO2, - CM_AUTO3, - CM_RED, - CM_WHITE_RED, -}; - -#define CHANNEL_MODES_ENABLED 0b00011111 -#define USE_CHANNEL_MODE_ARGS -// 128=middle CCT, _, _, _, 255=100% red -#define CHANNEL_MODE_ARGS 128,0,0,0,255 - -// can use some of the common handlers -#define USE_CALC_2CH_BLEND -//#define USE_CALC_AUTO_3CH_BLEND - - -// TODO: remove this as soon as it's not needed -#define PWM_CHANNELS 1 - -#define SWITCH_PIN PIN5_bp -#define SWITCH_PORT VPORTA.IN -#define SWITCH_ISC_REG PORTA.PIN2CTRL -#define SWITCH_VECT PORTA_PORT_vect -#define SWITCH_INTFLG VPORTA.INTFLAGS - - -// dynamic PWM -// PWM parameters of all channels are tied together because they share a counter -#define PWM_TOP_INIT 511 // highest value used in the top half of the ramp -#define PWM_TOP TCA0.SINGLE.PERBUF // holds the TOP value for for variable-resolution PWM -#define PWM_CNT TCA0.SINGLE.CNT // for resetting phase after each TOP adjustment - -// warm tint channel -//#define WARM_PWM_PIN PB0 -#define WARM_PWM_LVL TCA0.SINGLE.CMP0BUF // CMP1 is the output compare register for PB0 - -// cold tint channel -//#define COOL_PWM_PIN PB1 -#define COOL_PWM_LVL TCA0.SINGLE.CMP1BUF // CMP0 is the output compare register for PB1 - -// red channel -//#define RED_PWM_PIN PB2 -#define RED_PWM_LVL TCA0.SINGLE.CMP2BUF // CMP2 is the output compare register for PB2 - -// only using 16-bit PWM on this light -#define PWM_BITS 16 -#define PWM_GET PWM_GET16 -#define PWM_DATATYPE uint16_t -#define PWM1_DATATYPE uint16_t -#define PWM_DATATYPE2 uint32_t - - -// average drop across diode on this hardware -#ifndef VOLTAGE_FUDGE_FACTOR -#define VOLTAGE_FUDGE_FACTOR 7 // add 0.35V -#endif - - -// lighted button -#define AUXLED_PIN PIN5_bp -#define AUXLED_PORT PORTB - -// the button lights up -#define USE_INDICATOR_LED -// the button is visible while main LEDs are on -#define USE_INDICATOR_LED_WHILE_RAMPING - - - -inline void hwdef_setup() { - - // set up the system clock to run at 10 MHz instead of the default 3.33 MHz - _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm ); - - //VPORTA.DIR = ...; - // Outputs: - VPORTB.DIR = PIN0_bm // warm white - | PIN1_bm // cool white - | PIN2_bm // red - | PIN5_bm; // aux LED - //VPORTC.DIR = ...; - - // enable pullups on the unused pins to reduce power - PORTA.PIN0CTRL = PORT_PULLUPEN_bm; - PORTA.PIN1CTRL = PORT_PULLUPEN_bm; - PORTA.PIN2CTRL = PORT_PULLUPEN_bm; - PORTA.PIN3CTRL = PORT_PULLUPEN_bm; - PORTA.PIN4CTRL = PORT_PULLUPEN_bm; - PORTA.PIN5CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc; // eSwitch - PORTA.PIN6CTRL = PORT_PULLUPEN_bm; - PORTA.PIN7CTRL = PORT_PULLUPEN_bm; - - //PORTB.PIN0CTRL = PORT_PULLUPEN_bm; // warm tint channel - //PORTB.PIN1CTRL = PORT_PULLUPEN_bm; // cold tint channel - //PORTB.PIN2CTRL = PORT_PULLUPEN_bm; // red LEDs - PORTB.PIN3CTRL = PORT_PULLUPEN_bm; - PORTB.PIN4CTRL = PORT_PULLUPEN_bm; - //PORTB.PIN5CTRL = PORT_PULLUPEN_bm; // Aux LED - - PORTC.PIN0CTRL = PORT_PULLUPEN_bm; - PORTC.PIN1CTRL = PORT_PULLUPEN_bm; - PORTC.PIN2CTRL = PORT_PULLUPEN_bm; - PORTC.PIN3CTRL = PORT_PULLUPEN_bm; - - // set up the PWM - // https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny1614-16-17-DataSheet-DS40002204A.pdf - // PB0 is TCA0:WO0, use TCA_SINGLE_CMP0EN_bm - // PB1 is TCA0:WO1, use TCA_SINGLE_CMP1EN_bm - // PB2 is TCA0:WO2, use TCA_SINGLE_CMP2EN_bm - // For Fast (Single Slope) PWM use TCA_SINGLE_WGMODE_SINGLESLOPE_gc - // For Phase Correct (Dual Slope) PWM use TCA_SINGLE_WGMODE_DSBOTTOM_gc - // TODO: add references to MCU documentation - TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm - | TCA_SINGLE_CMP1EN_bm - | TCA_SINGLE_CMP2EN_bm - | TCA_SINGLE_WGMODE_DSBOTTOM_gc; - PWM_TOP = PWM_TOP_INIT; - TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc - | TCA_SINGLE_ENABLE_bm; -} - - -#define LAYOUT_DEFINED - diff --git a/hwdef-sofirn-lt1s-pro.c b/hwdef-sofirn-lt1s-pro.c new file mode 100644 index 0000000..6fe0fef --- /dev/null +++ b/hwdef-sofirn-lt1s-pro.c @@ -0,0 +1,269 @@ +// BLF LT1S Pro hwdef functions +// Copyright (C) 2023 Selene ToyKeeper +// SPDX-License-Identifier: GPL-3.0-or-later +#pragma once + + +void set_level_red(uint8_t level); +void set_level_white_blend(uint8_t level); +void set_level_auto_2ch_blend(uint8_t level); +void set_level_auto_3ch_blend(uint8_t level); +void set_level_red_white_blend(uint8_t level); + +bool gradual_tick_red(uint8_t gt); +bool gradual_tick_white_blend(uint8_t gt); +bool gradual_tick_auto_2ch_blend(uint8_t gt); +bool gradual_tick_auto_3ch_blend(uint8_t gt); +bool gradual_tick_red_white_blend(uint8_t gt); + + +Channel channels[] = { + { // manual blend of warm and cool white + .set_level = set_level_white_blend, + .gradual_tick = gradual_tick_white_blend, + .has_args = 1 + }, + { // auto blend from warm white to cool white + .set_level = set_level_auto_2ch_blend, + .gradual_tick = gradual_tick_auto_2ch_blend, + .has_args = 0 + }, + { // auto blend from red to warm white to cool white + .set_level = set_level_auto_3ch_blend, + .gradual_tick = gradual_tick_auto_3ch_blend, + .has_args = 0 + }, + { // red only + .set_level = set_level_red, + .gradual_tick = gradual_tick_red, + .has_args = 0 + }, + { // manual white blend + adjustable red + .set_level = set_level_red_white_blend, + .gradual_tick = gradual_tick_red_white_blend, + .has_args = 1 + } +}; + + +// calculate a 3-channel "auto tint" blend +// (like red -> warm white -> cool white) +// results are placed in *a, *b, and *c vars +// level : ramp level to convert into 3 channel levels +// (assumes ramp table is "pwm1_levels") +void calc_auto_3ch_blend( + PWM_DATATYPE *a, + PWM_DATATYPE *b, + PWM_DATATYPE *c, + uint8_t level) { + + PWM_DATATYPE vpwm = PWM_GET(pwm1_levels, level); + + // tint goes from 0 (red) to 127 (warm white) to 255 (cool white) + uint8_t mytint; + mytint = 255 * (uint16_t)level / RAMP_SIZE; + + // red is high at 0, low at 255 (linear) + *a = (((PWM_DATATYPE2)(255 - mytint) + * (PWM_DATATYPE2)vpwm) + 127) / 255; + // warm white is low at 0 and 255, high at 127 (linear triangle) + *b = (((PWM_DATATYPE2)triangle_wave(mytint) + * (PWM_DATATYPE2)vpwm) + 127) / 255; + // cool white is low at 0, high at 255 (linear) + *c = (((PWM_DATATYPE2)mytint + * (PWM_DATATYPE2)vpwm) + 127) / 255; + +} + + +// single set of LEDs with 1 power channel and dynamic PWM +void set_level_red(uint8_t level) { + if (level == 0) { + RED_PWM_LVL = 0; + PWM_CNT = 0; // reset phase + } else { + level --; // PWM array index = level - 1 + RED_PWM_LVL = PWM_GET(pwm1_levels, level); + // pulse frequency modulation, a.k.a. dynamic PWM + PWM_TOP = PWM_GET(pwm_tops, level); + // force reset phase when turning on from zero + // (because otherwise the initial response is inconsistent) + if (! actual_level) PWM_CNT = 0; + } +} + + +// warm + cool blend w/ dynamic PWM +void set_level_white_blend(uint8_t level) { + if (level == 0) { + WARM_PWM_LVL = 0; + COOL_PWM_LVL = 0; + PWM_CNT = 0; // reset phase + return; + } + + level --; // PWM array index = level - 1 + + PWM_DATATYPE warm_PWM, cool_PWM; + PWM_DATATYPE brightness = PWM_GET(pwm1_levels, level); + PWM_DATATYPE top = PWM_GET(pwm_tops, level); + uint8_t blend = cfg.channel_mode_args[cfg.channel_mode]; + + calc_2ch_blend(&warm_PWM, &cool_PWM, brightness, top, blend); + + WARM_PWM_LVL = warm_PWM; + COOL_PWM_LVL = cool_PWM; + PWM_TOP = top; + if (! actual_level) PWM_CNT = 0; // reset phase +} + + +// same as white blend, but tint is calculated from the ramp level +void set_level_auto_2ch_blend(uint8_t level) { + if (level == 0) { + WARM_PWM_LVL = 0; + COOL_PWM_LVL = 0; + PWM_CNT = 0; // reset phase + return; + } + + level --; // PWM array index = level - 1 + + PWM_DATATYPE warm_PWM, cool_PWM; + PWM_DATATYPE brightness = PWM_GET(pwm1_levels, level); + PWM_DATATYPE top = PWM_GET(pwm_tops, level); + uint8_t blend = 255 * (uint16_t)level / RAMP_SIZE; + + calc_2ch_blend(&warm_PWM, &cool_PWM, brightness, top, blend); + + WARM_PWM_LVL = warm_PWM; + COOL_PWM_LVL = cool_PWM; + PWM_TOP = top; + if (! actual_level) PWM_CNT = 0; // reset phase +} + + +// "auto tint" channel mode with dynamic PWM +void set_level_auto_3ch_blend(uint8_t level) { + if (level == 0) { + WARM_PWM_LVL = 0; + COOL_PWM_LVL = 0; + RED_PWM_LVL = 0; + PWM_CNT = 0; // reset phase + return; + } + + level --; // PWM array index = level - 1 + + PWM_DATATYPE a, b, c; + calc_auto_3ch_blend(&a, &b, &c, level); + + // pulse frequency modulation, a.k.a. dynamic PWM + uint16_t top = PWM_GET(pwm_tops, level); + + RED_PWM_LVL = a; + WARM_PWM_LVL = b; + COOL_PWM_LVL = c; + PWM_TOP = top; + if (! actual_level) PWM_CNT = 0; +} + + +// "white + red" channel mode +void set_level_red_white_blend(uint8_t level) { + // set the warm+cool white LEDs first + cfg.channel_mode = CM_WHITE; + set_level_white_blend(level); + cfg.channel_mode = CM_WHITE_RED; + + if (level == 0) { + RED_PWM_LVL = 0; + PWM_CNT = 0; // reset phase + return; + } + + level --; // PWM array index = level - 1 + PWM_DATATYPE vpwm = PWM_GET(pwm1_levels, level); + + // set the red LED as a ratio of the white output level + // 0 = no red + // 255 = red at 100% of white channel PWM + uint8_t ratio = cfg.channel_mode_args[cfg.channel_mode]; + + RED_PWM_LVL = (((PWM_DATATYPE2)ratio * (PWM_DATATYPE2)vpwm) + 127) / 255; + if (! actual_level) PWM_CNT = 0; // reset phase +} + + +///// "gradual tick" functions for smooth thermal regulation ///// + +///// bump each channel toward a target value ///// +bool gradual_adjust(uint16_t red, uint16_t warm, uint16_t cool) { + GRADUAL_ADJUST_SIMPLE(red, RED_PWM_LVL ); + GRADUAL_ADJUST_SIMPLE(warm, WARM_PWM_LVL); + GRADUAL_ADJUST_SIMPLE(cool, COOL_PWM_LVL); + + // check for completion + if ((red == RED_PWM_LVL ) + && (warm == WARM_PWM_LVL) + && (cool == COOL_PWM_LVL)) { + return true; // done + } + return false; // not done yet +} + +bool gradual_tick_red(uint8_t gt) { + uint16_t red = PWM_GET(pwm1_levels, gt); + return gradual_adjust(red, 0, 0); +} + + +bool gradual_tick_white_blend(uint8_t gt) { + // figure out what exact PWM levels we're aiming for + PWM_DATATYPE warm_PWM, cool_PWM; + PWM_DATATYPE brightness = PWM_GET(pwm1_levels, gt); + PWM_DATATYPE top = PWM_GET(pwm_tops, gt); + uint8_t blend = cfg.channel_mode_args[cfg.channel_mode]; + + calc_2ch_blend(&warm_PWM, &cool_PWM, brightness, top, blend); + + return gradual_adjust(0, warm_PWM, cool_PWM); +} + + +// same as white blend, but tint is calculated from the ramp level +bool gradual_tick_auto_2ch_blend(uint8_t gt) { + // figure out what exact PWM levels we're aiming for + PWM_DATATYPE warm_PWM, cool_PWM; + PWM_DATATYPE brightness = PWM_GET(pwm1_levels, gt); + PWM_DATATYPE top = PWM_GET(pwm_tops, gt); + uint8_t blend = 255 * (uint16_t)gt / RAMP_SIZE; + + calc_2ch_blend(&warm_PWM, &cool_PWM, brightness, top, blend); + + return gradual_adjust(0, warm_PWM, cool_PWM); +} + + +bool gradual_tick_auto_3ch_blend(uint8_t gt) { + // figure out what exact PWM levels we're aiming for + PWM_DATATYPE red, warm, cool; + calc_auto_3ch_blend(&red, &warm, &cool, gt); + return gradual_adjust(red, warm, cool); +} + + +bool gradual_tick_red_white_blend(uint8_t gt) { + // figure out what exact PWM levels we're aiming for + PWM_DATATYPE red, warm, cool; + PWM_DATATYPE brightness = PWM_GET(pwm1_levels, gt); + PWM_DATATYPE top = PWM_GET(pwm_tops, gt); + uint8_t blend = cfg.channel_mode_args[CM_WHITE]; + uint8_t ratio = cfg.channel_mode_args[cfg.channel_mode]; + + red = (((PWM_DATATYPE2)ratio * (PWM_DATATYPE2)brightness) + 127) / 255; + calc_2ch_blend(&warm, &cool, brightness, top, blend); + + return gradual_adjust(red, warm, cool); +} + diff --git a/hwdef-sofirn-lt1s-pro.h b/hwdef-sofirn-lt1s-pro.h new file mode 100644 index 0000000..3cc0ca9 --- /dev/null +++ b/hwdef-sofirn-lt1s-pro.h @@ -0,0 +1,153 @@ +// BLF LT1S Pro driver layout using the Attiny1616 +// Copyright (C) 2022-2023 (FIXME) +// SPDX-License-Identifier: GPL-3.0-or-later +#pragma once + +/* + * Driver pinout: + * eSwitch: PA5 + * Aux LED: PB5 + * WW PWM: PB0 (TCA0 WO0) + * CW PWM: PB1 (TCA0 WO1) + * Red PWM: PB2 (TCA0 WO2) + * Voltage: VCC + */ + +#define ATTINY 1616 +#include + +#define HWDEF_C_FILE hwdef-sofirn-lt1s-pro.c + +// channel modes: +// * 0. warm/cool white blend +// * 1. auto 2ch white blend (warm -> cool by ramp level) +// * 2. auto 3ch blend (red -> warm -> cool by ramp level) +// * 3. red only +// * 4. red + white blend +#define NUM_CHANNEL_MODES 5 +enum channel_modes_e { + CM_WHITE = 0, + CM_AUTO2, + CM_AUTO3, + CM_RED, + CM_WHITE_RED, +}; + +#define CHANNEL_MODES_ENABLED 0b00011111 +#define USE_CHANNEL_MODE_ARGS +// 128=middle CCT, _, _, _, 255=100% red +#define CHANNEL_MODE_ARGS 128,0,0,0,255 + +// can use some of the common handlers +#define USE_CALC_2CH_BLEND +//#define USE_CALC_AUTO_3CH_BLEND + + +// TODO: remove this as soon as it's not needed +#define PWM_CHANNELS 1 + +#define SWITCH_PIN PIN5_bp +#define SWITCH_PORT VPORTA.IN +#define SWITCH_ISC_REG PORTA.PIN2CTRL +#define SWITCH_VECT PORTA_PORT_vect +#define SWITCH_INTFLG VPORTA.INTFLAGS + + +// dynamic PWM +// PWM parameters of all channels are tied together because they share a counter +#define PWM_TOP_INIT 511 // highest value used in the top half of the ramp +#define PWM_TOP TCA0.SINGLE.PERBUF // holds the TOP value for for variable-resolution PWM +#define PWM_CNT TCA0.SINGLE.CNT // for resetting phase after each TOP adjustment + +// warm tint channel +//#define WARM_PWM_PIN PB0 +#define WARM_PWM_LVL TCA0.SINGLE.CMP0BUF // CMP1 is the output compare register for PB0 + +// cold tint channel +//#define COOL_PWM_PIN PB1 +#define COOL_PWM_LVL TCA0.SINGLE.CMP1BUF // CMP0 is the output compare register for PB1 + +// red channel +//#define RED_PWM_PIN PB2 +#define RED_PWM_LVL TCA0.SINGLE.CMP2BUF // CMP2 is the output compare register for PB2 + +// only using 16-bit PWM on this light +#define PWM_BITS 16 +#define PWM_GET PWM_GET16 +#define PWM_DATATYPE uint16_t +#define PWM1_DATATYPE uint16_t +#define PWM_DATATYPE2 uint32_t + + +// average drop across diode on this hardware +#ifndef VOLTAGE_FUDGE_FACTOR +#define VOLTAGE_FUDGE_FACTOR 7 // add 0.35V +#endif + + +// lighted button +#define AUXLED_PIN PIN5_bp +#define AUXLED_PORT PORTB + +// the button lights up +#define USE_INDICATOR_LED +// the button is visible while main LEDs are on +#define USE_INDICATOR_LED_WHILE_RAMPING + + + +inline void hwdef_setup() { + + // set up the system clock to run at 10 MHz instead of the default 3.33 MHz + _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm ); + + //VPORTA.DIR = ...; + // Outputs: + VPORTB.DIR = PIN0_bm // warm white + | PIN1_bm // cool white + | PIN2_bm // red + | PIN5_bm; // aux LED + //VPORTC.DIR = ...; + + // enable pullups on the unused pins to reduce power + PORTA.PIN0CTRL = PORT_PULLUPEN_bm; + PORTA.PIN1CTRL = PORT_PULLUPEN_bm; + PORTA.PIN2CTRL = PORT_PULLUPEN_bm; + PORTA.PIN3CTRL = PORT_PULLUPEN_bm; + PORTA.PIN4CTRL = PORT_PULLUPEN_bm; + PORTA.PIN5CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc; // eSwitch + PORTA.PIN6CTRL = PORT_PULLUPEN_bm; + PORTA.PIN7CTRL = PORT_PULLUPEN_bm; + + //PORTB.PIN0CTRL = PORT_PULLUPEN_bm; // warm tint channel + //PORTB.PIN1CTRL = PORT_PULLUPEN_bm; // cold tint channel + //PORTB.PIN2CTRL = PORT_PULLUPEN_bm; // red LEDs + PORTB.PIN3CTRL = PORT_PULLUPEN_bm; + PORTB.PIN4CTRL = PORT_PULLUPEN_bm; + //PORTB.PIN5CTRL = PORT_PULLUPEN_bm; // Aux LED + + PORTC.PIN0CTRL = PORT_PULLUPEN_bm; + PORTC.PIN1CTRL = PORT_PULLUPEN_bm; + PORTC.PIN2CTRL = PORT_PULLUPEN_bm; + PORTC.PIN3CTRL = PORT_PULLUPEN_bm; + + // set up the PWM + // https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny1614-16-17-DataSheet-DS40002204A.pdf + // PB0 is TCA0:WO0, use TCA_SINGLE_CMP0EN_bm + // PB1 is TCA0:WO1, use TCA_SINGLE_CMP1EN_bm + // PB2 is TCA0:WO2, use TCA_SINGLE_CMP2EN_bm + // For Fast (Single Slope) PWM use TCA_SINGLE_WGMODE_SINGLESLOPE_gc + // For Phase Correct (Dual Slope) PWM use TCA_SINGLE_WGMODE_DSBOTTOM_gc + // TODO: add references to MCU documentation + TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm + | TCA_SINGLE_CMP1EN_bm + | TCA_SINGLE_CMP2EN_bm + | TCA_SINGLE_WGMODE_DSBOTTOM_gc; + PWM_TOP = PWM_TOP_INIT; + TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc + | TCA_SINGLE_ENABLE_bm; +} + + +#define LAYOUT_DEFINED + diff --git a/spaghetti-monster/anduril/cfg-sofirn-lt1s-pro.h b/spaghetti-monster/anduril/cfg-sofirn-lt1s-pro.h index 76ddbba..9744244 100644 --- a/spaghetti-monster/anduril/cfg-sofirn-lt1s-pro.h +++ b/spaghetti-monster/anduril/cfg-sofirn-lt1s-pro.h @@ -4,7 +4,7 @@ #pragma once #define MODEL_NUMBER "0623" -#include "hwdef-Sofirn_LT1S-Pro.h" +#include "hwdef-sofirn-lt1s-pro.h" // ATTINY: 1616 // off mode: low (1) -- cgit v1.2.3