brewco/slcd.c

changeset 434
eb724767860d
child 443
6b80a37fdf8d
equal deleted inserted replaced
433:1421ece29197 434:eb724767860d
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 "futil.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 index, unsigned char data[8])
142 //{
143 //}
144
145
146
147 void slcdPutchar(int fd, unsigned char c)
148 {
149 uint16_t data = SLCD_DATA;
150
151 data += c & 0x0ff;
152 putLCDsocket(fd, data);
153 }
154
155
156
157 void slcdPuts(int fd, const char *string)
158 {
159 while (*string)
160 slcdPutchar(fd, *string++);
161 }
162
163
164
165 void slcdPrintf(int fd, const char *message, ...)
166 {
167 char buf[81 * sizeof(char)];
168 va_list va_ptr;
169
170 va_start(va_ptr, message);
171 vsnprintf(buf, (Config.lcd_cols + 1) * sizeof(char), message, va_ptr);
172 va_end(va_ptr);
173 slcdPuts(fd, buf);
174 }
175
176
177
178 /*
179 * Try to setup a udp connection to 127.0.0.1 so we duplicate the panel
180 * display and keys of the real panel. This should fail on a production
181 * system because there should no brewpanel program be running. If it
182 * succeeds, all io will be duplicated over the network.
183 */
184 int slcdInit(int fd, int cols, int rows)
185 {
186 if ((sock = socket(AF_INET, SOCK_DGRAM /*| SOCK_NONBLOCK */, 0)) < 0) {
187 syslog(LOG_NOTICE, "slcdInit() can't create socket: %s", strerror(errno));
188 return -1;
189 }
190
191 /*
192 * Setup address structure for the server socket.
193 */
194 memset(&sendaddr, 0, sizeof(struct sockaddr_in));
195 sendaddr.sin_family = AF_INET;
196 sendaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
197 sendaddr.sin_port = htons(SEND_PORT);
198
199 if (connect(sock, (struct sockaddr *)&sendaddr, sizeof(sendaddr)) < 0) {
200 close(sock);
201 sock = -1;
202 syslog(LOG_NOTICE, "slcdInit() can't bind sendsock %s", strerror(errno));
203 return -1;
204 }
205
206 syslog(LOG_NOTICE, "slcdInit() socket %d to brewpanel is active", sock);
207 return 0;
208 }
209
210

mercurial