~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-noctigon-k1.c

  • Committer: Selene ToyKeeper
  • Date: 2023-07-21 21:47:52 UTC
  • mto: (483.1.175 anduril2)
  • mto: This revision was merged to the branch mainline in revision 491.
  • Revision ID: bzr@toykeeper.net-20230721214752-o95ojcz2zteh8g8q
converted noctigon-k1 to multi-channel

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Noctigon K1 PWM helper functions
 
2
// Copyright (C) 2019-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_main(uint8_t level);
 
10
bool gradual_tick_main(uint8_t gt);
 
11
 
 
12
 
 
13
Channel channels[] = {
 
14
    { // channel 1 only
 
15
        .set_level    = set_level_main,
 
16
        .gradual_tick = gradual_tick_main
 
17
    },
 
18
    RGB_AUX_CHANNELS
 
19
};
 
20
 
 
21
 
 
22
// single set of LEDs with 2 stacked power channels, linear + DD FET
 
23
void set_level_main(uint8_t level) {
 
24
    if (level == 0) {
 
25
        CH1_PWM = 0;
 
26
        //CH2_PWM = 0;
 
27
        //PWM_CNT = 0;  // reset phase
 
28
        CH1_ENABLE_PORT &= ~(1 << CH1_ENABLE_PIN);  // disable opamp
 
29
        return;
 
30
    }
 
31
 
 
32
    CH1_ENABLE_PORT |= (1 << CH1_ENABLE_PIN);  // enable opamp
 
33
 
 
34
    level --;  // PWM array index = level - 1
 
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
    //while(actual_level && (PWM_CNT > (top - 32))) {}
 
44
    //PWM_TOP = top;
 
45
    // force reset phase when turning on from zero
 
46
    // (because otherwise the initial response is inconsistent)
 
47
    //if (! actual_level) PWM_CNT = 0;
 
48
}
 
49
 
 
50
bool gradual_tick_main(uint8_t gt) {
 
51
    PWM_DATATYPE pwm1 = PWM_GET(pwm1_levels, gt);
 
52
    //PWM_DATATYPE pwm2 = PWM_GET(pwm2_levels, gt);
 
53
 
 
54
    GRADUAL_ADJUST_SIMPLE (pwm1, CH1_PWM);
 
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