-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbutton.c
More file actions
121 lines (97 loc) · 2.22 KB
/
button.c
File metadata and controls
121 lines (97 loc) · 2.22 KB
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
/*****************************************************************************
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-devices.h"
#include "ledstrip.h"
#include "tasmota.h"
#include "button.h"
#include "solar.h"
#include "utils.h"
#include "curl.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;
// execute tasmota POWER TOGGLE command via http backlog command (workaround for buggy mqtt command)
int power_toggle_http(const char *ip) {
char url[128];
snprintf(url, 128, "http://%s/cm?cmnd=backlog+power+TOGGLE", ip);
curl_oneshot(url);
xlog("BUTTON power toggle %s", ip);
return 0;
}
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;
case 32:
power_toggle_http(LICHT_TISCH_HOST);
break;
case 64:
ledstrip_toggle();
break;
case 64 + SHIFT:
ledstrip_blink_red(3);
break;
default:
}
}
static void loop() {
unsigned char c, c_old, hold = 0;
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, &loop);