aboutsummaryrefslogtreecommitdiffstats
path: root/two_hosts_auto_powerswitch/src/logic.c
blob: 04bf3ff60823f43bbcb2da8849bccb42f1096f12 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Skip table:
serial skip update -> update skip table
computer state table update -> reset skip lines of updated computers

Computer state table:
Host state change detection -> lookup skip table -> update computer state table
computer button toggle -> update computer state table
serial logical computer state toggle -> update computer state table

Outlet table:
outlet toggle button -> update table
computer state table update -> update table
serial toggle command -> update table

Outlet table update can start timer (doing eventually a relay switch on or off
action), switch on relay directly, switch off relay directly.
*/

/* sources of wakeup are:
 * - every button press
 * - computer state input change
 * - uart rx
 */

#define OUTLET_BUTTONS_MASK 0x1F
#define COMPUTER_BUTTONS_MASK 0x60
#define OUTLET_BUTTONS_SHIFT 0
#define COMPUTER_BUTTONS_SHIFT 5

static struct tables_t {
    volatile uint8_t buttons:7; // accessed by interrupt functions
    uint8_t buttons_old:7;
    volatile uint8_t computers:2; // accessed by interrupt functions
    uint8_t computers_old:2;
    uint8_t computer_state:2;
    uint8_t computer_state_old:2;
} tables;

static struct uart_t {
    volatile uint8_t rxfifo[8];
    volatile uint8_t txfifo[8];
    volatile uint8_t rxindex:4;
    volatile uint8_t txindex:4;
} uart;

static void interrupt_pin_change(void)
{
    update_button_table();
    update_computer_input_table();
}

static void interrupt_uart_rx(void)
{
    echo_back();
    update_rx_fifo();
}

static void process_uart_rx(void)
{
    switch (get_next_char()) {
        case 'C':
            break;
        case 'T':
            break;
        case 'c':
            break;
        case 't':
            break;
        case 's':
            break;

        case 255:
            break;
    }
}

static void update_outlet_table(uint8_t changed)
{
}

static void update_computer_table(uint8_t changed)
{
}


static void main_loop(void)
{
    uint8_t changed;
    for (;;) {

        // read tables.buttons only one time (8bits access, 8bits cpu, atomic)
        changed = tables.buttons ^ tables.buttons_old;
        tables.buttons_old ^= changed;

        if (changed & COMPUTER_BUTTONS_MASK) {
            update_computer_table(changed >> COMPUTER_BUTTONS_SHIFT);
        }

        changed = tables.computer_state ^ tables.computer_state_old;
        tables.computer_state_old ^= changed;

        if (changed & OUTLET_BUTTONS_MASK) {
            update_outlet_table(changed >> OUTLET_BUTTONS_SHIFT);
        }

        changed = tables.computer_state ^ tables.computer_state_old;
        tables.computer_state_old ^= changed;

        // read computers state change
        changed = tables.computers ^ tables.computers_old;

        if (changed) {
        }

        process_uart_rx();

        standby();
    }
}