1
// thefreeman linear t1616 DAC driver helper functions
2
// Copyright (C) 2023 Selene ToyKeeper
3
// SPDX-License-Identifier: GPL-3.0-or-later
10
void set_level_main(uint8_t level);
11
bool gradual_tick_main(uint8_t gt);
14
Channel channels[] = {
16
.set_level = set_level_main,
17
.gradual_tick = gradual_tick_main
23
void set_level_zero() {
24
DAC_LVL = 0; // DAC off
25
DAC_VREF = V055; // low Vref
26
HDR_ENABLE_PORT &= ~(1 << HDR_ENABLE_PIN); // HDR off
28
// prevent post-off flash
29
//IN_NFET_ENABLE_PORT |= (1 << IN_NFET_ENABLE_PIN);
30
//delay_4ms(IN_NFET_DELAY_TIME/4);
31
//IN_NFET_ENABLE_PORT &= ~(1 << IN_NFET_ENABLE_PIN);
33
// turn off opamp last
34
OPAMP_ENABLE_PORT &= ~(1 << OPAMP_ENABLE_PIN); // Opamp off
37
// single set of LEDs with 1 regulated power channel
38
// and low/high HDR plus low/high Vref as different "gears"
39
void set_level_main(uint8_t level) {
40
#if 0 // unsure if this helps anything
43
// when turning on from off, try to prevent a flash
44
if ((! actual_level) && (level < HDR_ENABLE_LEVEL_MIN)) {
49
// Opamp on first, to give it a few extra microseconds to spin up
50
OPAMP_ENABLE_PORT |= (1 << OPAMP_ENABLE_PIN);
52
// pre-load ramp data so it can be assigned faster later
53
PWM_DATATYPE dac_lvl = PWM_GET(pwm1_levels, level);
54
PWM_DATATYPE dac_vref = PWM_GET(pwm_tops, level);
56
// enable HDR on top half of ramp
57
if (level >= (HDR_ENABLE_LEVEL_MIN-1))
58
HDR_ENABLE_PORT |= (1 << HDR_ENABLE_PIN);
60
HDR_ENABLE_PORT &= ~(1 << HDR_ENABLE_PIN);
64
// wait for flash prevention to finish
65
delay_4ms(OPAMP_ON_DELAY/4);
69
// set these in successive clock cycles to avoid getting out of sync
70
// (minimizes ramp bumps when changing gears)
75
bool gradual_tick_main(uint8_t gt) {
76
// if HDR and Vref "engine gear" is the same, do a small adjustment...
77
// otherwise, simply jump to the next ramp level
78
// and let set_level() handle any gear changes
80
PWM_DATATYPE dac_next = PWM_GET(pwm1_levels, gt);
81
PWM_DATATYPE vref_next = PWM_GET(pwm_tops, gt);
83
// different gear = full adjustment
84
if (vref_next != DAC_VREF) return true; // let parent set_level() for us
86
// same gear = small adjustment
87
GRADUAL_ADJUST_SIMPLE(dac_next, DAC_LVL);
88
if (dac_next == DAC_LVL) return true; // done
90
return false; // not done yet