~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-wurkkos-ts10.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 TS10 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-aux.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
    { // main LEDs
 
17
        .set_level    = set_level_main,
 
18
        .gradual_tick = gradual_tick_main
 
19
    },
 
20
    { // aux LEDs
 
21
        .set_level    = set_level_aux,
 
22
        .gradual_tick = gradual_tick_null
 
23
    }
 
24
};
 
25
 
 
26
 
 
27
void set_level_zero() {
 
28
    CH1_PWM = 0;
 
29
    CH2_PWM = 0;
 
30
    PWM_CNT = 0;  // reset phase
 
31
}
 
32
 
 
33
// single set of LEDs with 2 stacked power channels, DDFET+1 or DDFET+linear
 
34
void set_level_main(uint8_t level) {
 
35
    PWM_DATATYPE ch1_pwm = PWM_GET(pwm1_levels, level);
 
36
    PWM_DATATYPE ch2_pwm = PWM_GET(pwm2_levels, level);
 
37
    // pulse frequency modulation, a.k.a. dynamic PWM
 
38
    uint16_t top = PWM_GET16(pwm_tops, level);
 
39
 
 
40
    CH1_PWM = ch1_pwm;
 
41
    CH2_PWM = ch2_pwm;
 
42
    // wait to sync the counter and avoid flashes
 
43
    // (unnecessary w/ buffered registers)
 
44
    //while(actual_level && (PWM_CNT > (top - 32))) {}
 
45
    PWM_TOP = top;
 
46
    // force reset phase when turning on from zero
 
47
    // (because otherwise the initial response is inconsistent)
 
48
    if (! actual_level) PWM_CNT = 0;
 
49
}
 
50
 
 
51
bool gradual_tick_main(uint8_t gt) {
 
52
    PWM_DATATYPE pwm1 = PWM_GET(pwm1_levels, gt);
 
53
    PWM_DATATYPE pwm2 = PWM_GET(pwm2_levels, gt);
 
54
 
 
55
    GRADUAL_ADJUST_STACKED(pwm1, CH1_PWM, PWM_TOP_INIT);
 
56
    GRADUAL_ADJUST_SIMPLE (pwm2, CH2_PWM);
 
57
 
 
58
    if (   (pwm1 == CH1_PWM)
 
59
        && (pwm2 == CH2_PWM)
 
60
       ) {
 
61
        return true;  // done
 
62
    }
 
63
    return false;  // not done yet
 
64
}
 
65