39
36
// TODO: blend mode should enable this automatically?
40
37
#define USE_CHANNEL_MODE_ARGS
41
38
// TODO: or maybe if args are defined, the USE_ should be auto-set?
39
// 128=middle CCT, N/A, N/A, 255=100% red
42
40
#define CHANNEL_MODE_ARGS 128,0,0,255
43
#define SET_LEVEL_MODES set_level_2ch_blend, \
44
set_level_auto_3ch_blend, \
41
#define SET_LEVEL_MODES set_level_2ch_dyn_blend, \
42
set_level_auto_3ch_dyn_blend, \
46
44
set_level_red_white_blend
47
45
// TODO: gradual ticking for thermal regulation
48
46
#define GRADUAL_TICK_MODES gradual_tick_2ch_blend, \
49
47
gradual_tick_auto_3ch_blend, \
50
48
gradual_tick_1ch, \
51
49
gradual_tick_red_white_blend
52
// can use some of the common handlers
53
#define USE_SET_LEVEL_2CH_BLEND
50
// can use some of the common handlers?
51
//#define USE_SET_LEVEL_2CH_BLEND
54
52
//#define USE_SET_LEVEL_AUTO_3CH_BLEND
55
#define USE_SET_LEVEL_1CH
53
//#define USE_SET_LEVEL_1CH
56
54
//#define USE_SET_LEVEL_RED_WHITE_BLEND
58
56
//#define USE_GRADUAL_TICK_2CH_BLEND
78
76
#define SWITCH_INTFLG VPORTA.INTFLAGS
80
// PWM parameters of all channels are tied together because they share a counter
81
#define PWM_TOP_INIT 511 // highest value used in the top half of the ramp
82
#define PWM_TOP TCA0.SINGLE.PERBUF // holds the TOP value for for variable-resolution PWM
83
#define PWM_CNT TCA0.SINGLE.CNT // for resetting phase after each TOP adjustment
81
85
// warm tint channel
82
#define WARM_PWM_PIN PB0
83
#define WARM_PWM_LVL TCA0.SINGLE.CMP0 // CMP1 is the output compare register for PB0
86
//#define WARM_PWM_PIN PB0
87
#define WARM_PWM_LVL TCA0.SINGLE.CMP0BUF // CMP1 is the output compare register for PB0
85
89
// cold tint channel
86
#define COOL_PWM_PIN PB1
87
#define COOL_PWM_LVL TCA0.SINGLE.CMP1 // CMP0 is the output compare register for PB1
90
//#define COOL_PWM_PIN PB1
91
#define COOL_PWM_LVL TCA0.SINGLE.CMP1BUF // CMP0 is the output compare register for PB1
90
#define RED_PWM_PIN PB0 //
91
#define RED_PWM_LVL TCA0.SINGLE.CMP2 // CMP2 is the output compare register for PB2
93
// translate cfg names to FSM names
94
#define LOW_PWM_LEVELS RED_PWM_LEVELS
95
#define LOW_PWM_LVL RED_PWM_LVL
96
#define LOW_PWM_PIN RED_PWM_PIN
98
// only using 8-bit on this light
99
#define PWM_GET PWM_GET8
100
#define PWM_DATATYPE uint8_t
101
#define BLEND_PWM_DATATYPE uint8_t
94
//#define RED_PWM_PIN PB2
95
#define RED_PWM_LVL TCA0.SINGLE.CMP2BUF // CMP2 is the output compare register for PB2
97
// only using 16-bit PWM on this light
99
#define PWM_GET PWM_GET16
100
#define PWM_DATATYPE uint16_t
101
#define PWM1_DATATYPE uint16_t
102
#define PWM_DATATYPE2 uint32_t
104
105
// average drop across diode on this hardware
120
121
// custom channel modes
121
void set_level_auto_3ch_blend(uint8_t level);
122
void set_level_1ch_dyn(uint8_t level);
123
void set_level_2ch_dyn_blend(uint8_t level);
124
void set_level_auto_3ch_dyn_blend(uint8_t level);
122
125
void set_level_red_white_blend(uint8_t level);
128
131
_PROTECTED_WRITE( CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm );
130
133
//VPORTA.DIR = ...;
131
VPORTB.DIR = PIN0_bm | PIN1_bm | PIN2_bm | PIN5_bm; // Outputs: Aux LED and PWMs
135
VPORTB.DIR = PIN0_bm // warm white
136
| PIN1_bm // cool white
138
| PIN5_bm; // aux LED
132
139
//VPORTC.DIR = ...;
134
141
// enable pullups on the unused pins to reduce power
154
161
PORTC.PIN3CTRL = PORT_PULLUPEN_bm;
156
163
// set up the PWM
164
// https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny1614-16-17-DataSheet-DS40002204A.pdf
165
// PB0 is TCA0:WO0, use TCA_SINGLE_CMP0EN_bm
166
// PB1 is TCA0:WO1, use TCA_SINGLE_CMP1EN_bm
167
// PB2 is TCA0:WO2, use TCA_SINGLE_CMP2EN_bm
168
// For Fast (Single Slope) PWM use TCA_SINGLE_WGMODE_SINGLESLOPE_gc
169
// For Phase Correct (Dual Slope) PWM use TCA_SINGLE_WGMODE_DSBOTTOM_gc
157
170
// TODO: add references to MCU documentation
158
TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | TCA_SINGLE_CMP1EN_bm | TCA_SINGLE_CMP2EN_bm | TCA_SINGLE_WGMODE_DSBOTTOM_gc;
159
TCA0.SINGLE.PER = 255;
160
TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm;
171
TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm
172
| TCA_SINGLE_CMP1EN_bm
173
| TCA_SINGLE_CMP2EN_bm
174
| TCA_SINGLE_WGMODE_DSBOTTOM_gc;
175
PWM_TOP = PWM_TOP_INIT;
176
TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc
177
| TCA_SINGLE_ENABLE_bm;