~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-emisar-d4v2.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
// Emisar D4v2 PWM helper functions
 
2
// Copyright (C) 2017-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
    { // main LEDs
 
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
    while(actual_level && (PWM_CNT > (top - 32))) {}
 
41
    PWM_TOP = top;
 
42
    // force reset phase when turning on from zero
 
43
    // (because otherwise the initial response is inconsistent)
 
44
    if (! actual_level) PWM_CNT = 0;
 
45
}
 
46
 
 
47
bool gradual_tick_main(uint8_t gt) {
 
48
    PWM_DATATYPE pwm1 = PWM_GET(pwm1_levels, gt);
 
49
    PWM_DATATYPE pwm2 = PWM_GET(pwm2_levels, gt);
 
50
 
 
51
    GRADUAL_ADJUST_STACKED(pwm1, CH1_PWM, PWM_TOP_INIT);
 
52
    GRADUAL_ADJUST_SIMPLE (pwm2, CH2_PWM);
 
53
 
 
54
    if (   (pwm1 == CH1_PWM)
 
55
        && (pwm2 == CH2_PWM)
 
56
       ) {
 
57
        return true;  // done
 
58
    }
 
59
    return false;  // not done yet
 
60
}
 
61