1
// Noctigon KR4 (no DD FET) PWM helper functions
2
// Copyright (C) 2020-2023 Selene ToyKeeper
3
// SPDX-License-Identifier: GPL-3.0-or-later
7
#include "chan-rgbaux.c"
11
void set_level_main(uint8_t level);
12
bool gradual_tick_main(uint8_t gt);
15
Channel channels[] = {
17
.set_level = set_level_main,
18
.gradual_tick = gradual_tick_main
24
void set_level_zero() {
27
PWM_CNT = 0; // reset phase
28
CH1_ENABLE_PORT &= ~(1 << CH1_ENABLE_PIN); // disable opamp
31
// single set of LEDs with linear power channel
32
void set_level_main(uint8_t level) {
33
CH1_ENABLE_PORT |= (1 << CH1_ENABLE_PIN); // enable opamp
35
PWM_DATATYPE ch1_pwm = PWM_GET(pwm1_levels, level);
36
// pulse frequency modulation, a.k.a. dynamic PWM
37
uint16_t top = PWM_GET16(pwm_tops, level);
41
// wait to sync the counter and avoid flashes
42
while(actual_level && (PWM_CNT > (top - 32))) {}
44
// force reset phase when turning on from zero
45
// (because otherwise the initial response is inconsistent)
46
if (! actual_level) PWM_CNT = 0;
49
bool gradual_tick_main(uint8_t gt) {
50
PWM_DATATYPE pwm1 = PWM_GET(pwm1_levels, gt);
52
GRADUAL_ADJUST_SIMPLE(pwm1, CH1_PWM);
54
if ( (pwm1 == CH1_PWM)
58
return false; // not done yet