~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

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