1 /***************************************************************************** |
|
2 * Copyright (C) 2015 |
|
3 * |
|
4 * Michiel Broek <mbroek at mbse dot eu> |
|
5 * |
|
6 * This file is part of the mbsePi-apps |
|
7 * |
|
8 * This is free software; you can redistribute it and/or modify it |
|
9 * under the terms of the GNU General Public License as published by the |
|
10 * Free Software Foundation; either version 2, or (at your option) any |
|
11 * later version. |
|
12 * |
|
13 * mbsePi-apps is distributed in the hope that it will be useful, but |
|
14 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 * General Public License for more details. |
|
17 * |
|
18 * You should have received a copy of the GNU General Public License |
|
19 * along with ThermFerm; see the file COPYING. If not, write to the Free |
|
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
|
21 *****************************************************************************/ |
|
22 |
|
23 #include "brewco.h" |
|
24 #include "slcd.h" |
|
25 #include "util.h" |
|
26 #include "xutil.h" |
|
27 |
|
28 |
|
29 |
|
30 struct sockaddr_in sendaddr; /* Server send socket */ |
|
31 int sock = -1; |
|
32 uint16_t keys = 0x0000; |
|
33 uint16_t leds = 0x0400; /* LED's, buzzer, LCD backlight */ |
|
34 uint16_t oleds = 0x0400; |
|
35 |
|
36 extern int debug; |
|
37 extern sys_config Config; |
|
38 |
|
39 |
|
40 |
|
41 void putLCDsocket(int fd, uint16_t data) |
|
42 { |
|
43 socklen_t slen; |
|
44 uint16_t rdat; |
|
45 |
|
46 if (sock == -1) |
|
47 return; |
|
48 |
|
49 if (sendto(sock, &data, sizeof(uint16_t), 0, (struct sockaddr *)&sendaddr, sizeof(sendaddr)) != sizeof(uint16_t)) { |
|
50 syslog(LOG_NOTICE, "Socket %d send failed, closing socket: %s", sock, strerror(errno)); |
|
51 if (shutdown(sock, SHUT_RDWR)) { |
|
52 syslog(LOG_NOTICE, "Can't shutdown socket: %s", strerror(errno)); |
|
53 } |
|
54 sock = -1; |
|
55 } else { |
|
56 if (recvfrom(sock, &rdat, sizeof(uint16_t), 0, (struct sockaddr *) &sendaddr, &slen) != sizeof(uint16_t)) { |
|
57 syslog(LOG_NOTICE, "Socket %d recvfrom failed, closing socket: %s", sock, strerror(errno)); |
|
58 if (shutdown(sock, SHUT_RDWR)) { |
|
59 syslog(LOG_NOTICE, "Can't shutdown socket: %s", strerror(errno)); |
|
60 } |
|
61 sock = -1; |
|
62 } else { |
|
63 if ((rdat & SLCD_MKEYS) == SLCD_KEYS) { |
|
64 if (((rdat & 0x00ff) != keys) && debug) |
|
65 fprintf(stdout, "received keys %04x was %04x\n", rdat & 0x00ff, keys); |
|
66 keys = rdat & 0x00ff; |
|
67 } else { |
|
68 if (debug) |
|
69 fprintf(stdout, "received %04x\n", rdat); |
|
70 } |
|
71 |
|
72 } |
|
73 } |
|
74 } |
|
75 |
|
76 |
|
77 |
|
78 void slcdDummy(int fd) |
|
79 { |
|
80 putLCDsocket(fd, SLCD_NULL); |
|
81 } |
|
82 |
|
83 |
|
84 |
|
85 void slcdLEDs(int fd) |
|
86 { |
|
87 if (leds != oleds) |
|
88 putLCDsocket(fd, leds); |
|
89 oleds = leds; |
|
90 } |
|
91 |
|
92 |
|
93 //void slcdHome(int fd) |
|
94 //{ |
|
95 //} |
|
96 |
|
97 |
|
98 |
|
99 void slcdClear(int fd) |
|
100 { |
|
101 putLCDsocket(fd, SLCD_CLEAR); |
|
102 putLCDsocket(fd, SLCD_HOME); |
|
103 } |
|
104 |
|
105 |
|
106 |
|
107 //void slcdDisplay(int fd, int state) |
|
108 //{ |
|
109 //} |
|
110 |
|
111 |
|
112 |
|
113 //void slcdCursor(int fd, int state) |
|
114 //{ |
|
115 //} |
|
116 |
|
117 |
|
118 |
|
119 //void slcdCursorBlink(int fd, int state) |
|
120 //{ |
|
121 //} |
|
122 |
|
123 |
|
124 |
|
125 //void slcdSendCommand(int fd, unsigned char command) |
|
126 //{ |
|
127 //} |
|
128 |
|
129 |
|
130 |
|
131 void slcdPosition(int fd, int x, int y) |
|
132 { |
|
133 uint16_t data = SLCD_DGRAM; |
|
134 |
|
135 data += (x & 0x1f) + ((y & 0x03) << 5); |
|
136 putLCDsocket(fd, data); |
|
137 } |
|
138 |
|
139 |
|
140 |
|
141 void slcdCharDef(int fd, int idx, unsigned char data[8]) |
|
142 { |
|
143 int i; |
|
144 uint16_t byte; |
|
145 |
|
146 if (debug) |
|
147 fprintf(stdout, "CharDef(%d, %d, %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x)\n", fd, idx, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]); |
|
148 |
|
149 byte = SLCD_CGRAM | ((idx & 7) << 3); |
|
150 putLCDsocket(fd, byte); |
|
151 for (i = 0; i < 8; i++) { |
|
152 byte = data[i] & 0xff; |
|
153 putLCDsocket(fd, byte); |
|
154 } |
|
155 } |
|
156 |
|
157 |
|
158 |
|
159 void slcdPutchar(int fd, unsigned char c) |
|
160 { |
|
161 uint16_t data = SLCD_DATA; |
|
162 |
|
163 data += c & 0x0ff; |
|
164 putLCDsocket(fd, data); |
|
165 } |
|
166 |
|
167 |
|
168 |
|
169 void slcdPuts(int fd, const char *string) |
|
170 { |
|
171 while (*string) |
|
172 slcdPutchar(fd, *string++); |
|
173 } |
|
174 |
|
175 |
|
176 |
|
177 void slcdPrintf(int fd, const char *message, ...) |
|
178 { |
|
179 char buf[81 * sizeof(char)]; |
|
180 va_list va_ptr; |
|
181 |
|
182 va_start(va_ptr, message); |
|
183 vsnprintf(buf, (Config.lcd_cols + 1) * sizeof(char), message, va_ptr); |
|
184 va_end(va_ptr); |
|
185 slcdPuts(fd, buf); |
|
186 } |
|
187 |
|
188 |
|
189 |
|
190 /* |
|
191 * Try to setup a udp connection to 127.0.0.1 so we duplicate the panel |
|
192 * display and keys of the real panel. This should fail on a production |
|
193 * system because there should no brewpanel program be running. If it |
|
194 * succeeds, all io will be duplicated over the network. |
|
195 */ |
|
196 int slcdInit(int fd, int cols, int rows) |
|
197 { |
|
198 if ((sock = socket(AF_INET, SOCK_DGRAM /*| SOCK_NONBLOCK */, 0)) < 0) { |
|
199 syslog(LOG_NOTICE, "slcdInit() can't create socket: %s", strerror(errno)); |
|
200 return -1; |
|
201 } |
|
202 |
|
203 /* |
|
204 * Setup address structure for the server socket. |
|
205 */ |
|
206 memset(&sendaddr, 0, sizeof(struct sockaddr_in)); |
|
207 sendaddr.sin_family = AF_INET; |
|
208 sendaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); |
|
209 sendaddr.sin_port = htons(SEND_PORT); |
|
210 |
|
211 if (connect(sock, (struct sockaddr *)&sendaddr, sizeof(sendaddr)) < 0) { |
|
212 close(sock); |
|
213 sock = -1; |
|
214 syslog(LOG_NOTICE, "slcdInit() can't bind sendsock %s", strerror(errno)); |
|
215 return -1; |
|
216 } |
|
217 |
|
218 syslog(LOG_NOTICE, "slcdInit() socket %d to brewpanel is active", sock); |
|
219 return 0; |
|
220 } |
|
221 |
|
222 |
|