aboutsummaryrefslogtreecommitdiff
path: root/RoundTable/momentary.c
blob: 4394f46cd8cbbfe841f870ca1dda2cfb0f3e9640 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * Momentary: Very simple example UI for RoundTable.
 * Is intended to be the simplest possible RT e-switch UI.
 * The light is in while the button is held; off otherwise.
 *
 * Copyright (C) 2017 Selene ToyKeeper
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define RT_EMISAR_D4_LAYOUT
#define USE_LVP
#define USE_DEBUG_BLINK
#define OWN_DELAY
#define USE_DELAY_MS
#include "round-table.c"
#include "tk-delay.h"

volatile uint8_t brightness;

void light_on() {
    PWM1_LVL = brightness;
    PWM2_LVL = 0;
}

void light_off() {
    PWM1_LVL = 0;
    PWM2_LVL = 0;
}

//State momentary_state {
uint8_t momentary_state(EventPtr event, uint16_t arg) {

    if (event == EV_click1_press) {
        brightness = 255;
        light_on();
        // reset current event queue
        empty_event_sequence();
        return 0;
    }

    else if (event == EV_release) {
        light_off();
        // reset current event queue
        empty_event_sequence();
        return 0;
    }

    /*
    // LVP / low-voltage protection
    //else if ((event == EV_voltage_low)  ||  (event == EV_voltage_critical)) {
    else if (event == EV_voltage_low) {
        if (brightness > 0) brightness >>= 1;
        else {
            light_off();
            standby_mode();
        }
        return 0;
    }
    */

    // event not handled
    return 1;
}

// LVP / low-voltage protection
void low_voltage() {
    if (brightness > 0) brightness >>= 1;
    else {
        light_off();
        standby_mode();
    }
}

void setup() {
    //debug_blink(1);
    /*
    brightness = 255;
    light_on();
    delay_ms(10);
    light_off();
    */

    push_state(momentary_state);
}