~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-sofirn-sp10-pro.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
// Sofirn SP10 Pro PWM helper functions
 
2
// Copyright (C) 2023 Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
 
4
#pragma once
 
5
 
 
6
void set_level_zero();
 
7
 
 
8
void set_level_main(uint8_t level);
 
9
bool gradual_tick_main(uint8_t gt);
 
10
 
 
11
 
 
12
Channel channels[] = {
 
13
    { // main LEDs
 
14
        .set_level    = set_level_main,
 
15
        .gradual_tick = gradual_tick_main
 
16
    },
 
17
};
 
18
 
 
19
 
 
20
void set_level_zero() {
 
21
    CH1_PWM = 0;
 
22
    CH2_PWM = 0;
 
23
    PWM_CNT = 0;  // reset phase
 
24
    BST_ENABLE_PORT &= ~(1 << BST_ENABLE_PIN);  // boost off
 
25
}
 
26
 
 
27
// single set of LEDs with 2 stacked power channels
 
28
void set_level_main(uint8_t level) {
 
29
    PWM_DATATYPE ch1_pwm = PWM_GET(pwm1_levels, level);
 
30
    PWM_DATATYPE ch2_pwm = PWM_GET(pwm2_levels, level);
 
31
    // pulse frequency modulation, a.k.a. dynamic PWM
 
32
    uint16_t top = PWM_GET16(pwm_tops, level);
 
33
 
 
34
    BST_ENABLE_PORT |= (1 << BST_ENABLE_PIN);  // boost on
 
35
 
 
36
    CH1_PWM = ch1_pwm;
 
37
    CH2_PWM = ch2_pwm;
 
38
    PWM_TOP = top;
 
39
 
 
40
    // force reset phase when turning on from zero
 
41
    // (because otherwise the initial response is inconsistent)
 
42
    if (! actual_level) PWM_CNT = 0;
 
43
}
 
44
 
 
45
bool gradual_tick_main(uint8_t gt) {
 
46
    PWM_DATATYPE pwm1 = PWM_GET(pwm1_levels, gt);
 
47
    PWM_DATATYPE pwm2 = PWM_GET(pwm2_levels, gt);
 
48
 
 
49
    // ch1 sometimes makes huge leaps; don't make it gradual
 
50
    // if either current or new level is in the leap zone, just leap
 
51
    if ((CH1_PWM + pwm1) > 128) CH1_PWM = pwm1;
 
52
    else GRADUAL_ADJUST_SIMPLE(pwm1, CH1_PWM);
 
53
 
 
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