~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-noctigon-kr4.c

  • Committer: Selene ToyKeeper
  • Date: 2023-11-04 15:09:10 UTC
  • mfrom: (483.1.175 anduril2)
  • Revision ID: bzr@toykeeper.net-20231104150910-ddd3afw4nhfvof2l
merged anduril2 branch -> fsm, with *years* of changes
(this also means this code is now Anduril 2 instead of Anduril 1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Noctigon KR4 PWM helper functions
 
2
// Copyright (C) 2020-2023 Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
 
4
#pragma once
 
5
 
 
6
#include "chan-rgbaux.c"
 
7
 
 
8
void set_level_zero();
 
9
 
 
10
void set_level_main(uint8_t level);
 
11
bool gradual_tick_main(uint8_t gt);
 
12
 
 
13
 
 
14
Channel channels[] = {
 
15
    { // channel 1 only
 
16
        .set_level    = set_level_main,
 
17
        .gradual_tick = gradual_tick_main
 
18
    },
 
19
    RGB_AUX_CHANNELS
 
20
};
 
21
 
 
22
 
 
23
void set_level_zero() {
 
24
    CH1_PWM = 0;
 
25
    CH2_PWM = 0;
 
26
    PWM_CNT = 0;  // reset phase
 
27
    CH1_ENABLE_PORT &= ~(1 << CH1_ENABLE_PIN);  // disable opamp
 
28
}
 
29
 
 
30
// single set of LEDs with 2 stacked power channels, linear + DD FET
 
31
void set_level_main(uint8_t level) {
 
32
    CH1_ENABLE_PORT |= (1 << CH1_ENABLE_PIN);  // enable opamp
 
33
 
 
34
    PWM_DATATYPE ch1_pwm = PWM_GET(pwm1_levels, level);
 
35
    PWM_DATATYPE ch2_pwm = PWM_GET(pwm2_levels, level);
 
36
    // pulse frequency modulation, a.k.a. dynamic PWM
 
37
    uint16_t top = PWM_GET16(pwm_tops, level);
 
38
 
 
39
    CH1_PWM = ch1_pwm;
 
40
    CH2_PWM = ch2_pwm;
 
41
    // wait to sync the counter and avoid flashes
 
42
    while(actual_level && (PWM_CNT > (top - 32))) {}
 
43
    PWM_TOP = top;
 
44
    // force reset phase when turning on from zero
 
45
    // (because otherwise the initial response is inconsistent)
 
46
    if (! actual_level) PWM_CNT = 0;
 
47
}
 
48
 
 
49
bool gradual_tick_main(uint8_t gt) {
 
50
    PWM_DATATYPE pwm1 = PWM_GET(pwm1_levels, gt);
 
51
    PWM_DATATYPE pwm2 = PWM_GET(pwm2_levels, gt);
 
52
 
 
53
    GRADUAL_ADJUST_STACKED(pwm1, CH1_PWM, PWM_TOP_INIT);
 
54
    GRADUAL_ADJUST_SIMPLE (pwm2, CH2_PWM);
 
55
 
 
56
    if (   (pwm1 == CH1_PWM)
 
57
        && (pwm2 == CH2_PWM)
 
58
       ) {
 
59
        return true;  // done
 
60
    }
 
61
    return false;  // not done yet
 
62
}
 
63