-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbutton.c
114 lines (91 loc) · 1.96 KB
/
button.c
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
/*****************************************************************************
read button press/release events from a PCF8574X Port expander connected
via I2C over CH341 USB Bridge Controller
Using kernel driver from Allan Bian
https://github.com/allanbian1017/i2c-ch341-usb
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input-event-codes.h>
#include "tasmota.h"
#include "fronius.h"
#include "button.h"
#include "utils.h"
#include "dac.h"
#include "i2c.h"
#include "mcp.h"
#include "mpd.h"
#ifndef I2C
#define I2C "/dev/i2c-3"
#endif
#define SHIFT 128
static int i2cfd;
static void handle_button(unsigned char c) {
if (c == 128)
return; // this is the shift button
xlog("BUTTON handle %d", c);
switch (c) {
case 1:
mpdclient_handle(KEY_PAUSE);
break;
case 2:
dac_volume_down();
break;
case 4:
dac_volume_up();
break;
case 8:
mpdclient_handle(KEY_NEXTSONG);
break;
case 16:
system("/m/party.sh");
break;
#ifdef FRONIUS
case 32:
fronius_override_seconds("tisch", 3600);
break;
case 32 + SHIFT:
fronius_override_seconds("wozi", 3600);
break;
case 64:
fronius_boiler1();
break;
case 64 + SHIFT:
fronius_boiler3();
break;
#endif
default:
}
}
static void button() {
unsigned char c, c_old, hold;
if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)) {
xlog("Error setting pthread_setcancelstate");
return;
}
i2c_get(i2cfd, ADDR, &c_old);
while (1) {
msleep(100);
i2c_get(i2cfd, ADDR, &c);
if (c == 0xff)
hold = 0;
else if (c == c_old)
hold++;
if (hold > 5 || (c != 0xff && c != c_old))
handle_button(0xff - c);
c_old = c;
}
}
static int init() {
if ((i2cfd = open(I2C, O_RDWR)) < 0)
return xerr("error opening %s", I2C);
return 0;
}
static void stop() {
if (i2cfd > 0)
close(i2cfd);
}
MCP_REGISTER(button, 5, &init, &stop, &button);