~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-emisar-d4v2-nofet.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 (no DD FET, 1x7135 only) 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
    PWM_CNT = 0;  // reset phase
 
27
}
 
28
 
 
29
// single set of LEDs with just a 1x7135 chip, max 350 mA or ~130 lm
 
30
void set_level_main(uint8_t level) {
 
31
    PWM_DATATYPE ch1_pwm = PWM_GET(pwm1_levels, level);
 
32
    // pulse frequency modulation, a.k.a. dynamic PWM
 
33
    uint16_t top = PWM_GET16(pwm_tops, level);
 
34
 
 
35
    CH1_PWM = ch1_pwm;
 
36
    // wait to sync the counter and avoid flashes
 
37
    while(actual_level && (PWM_CNT > (top - 32))) {}
 
38
    PWM_TOP = top;
 
39
    // force reset phase when turning on from zero
 
40
    // (because otherwise the initial response is inconsistent)
 
41
    if (! actual_level) PWM_CNT = 0;
 
42
}
 
43
 
 
44
bool gradual_tick_main(uint8_t gt) {
 
45
    PWM_DATATYPE pwm1 = PWM_GET(pwm1_levels, gt);
 
46
 
 
47
    GRADUAL_ADJUST_SIMPLE (pwm1, CH1_PWM);
 
48
 
 
49
    if (   (pwm1 == CH1_PWM)
 
50
       ) {
 
51
        return true;  // done
 
52
    }
 
53
    return false;  // not done yet
 
54
}
 
55