|
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 "thermferm.h" |
|
24 #include "slcd.h" |
|
25 #include "futil.h" |
|
26 #include "xutil.h" |
|
27 |
|
28 |
|
29 #define SLCD_NULL 0x0000 |
|
30 #define SLCD_CLEAR 0x0001 |
|
31 #define SLCD_MCLEAR 0x1fff |
|
32 #define SLCD_HOME 0x0002 |
|
33 #define SLCD_MHOME 0x1ffe |
|
34 #define SLCD_DGRAM 0x0080 |
|
35 #define SLCD_MDGRAM 0x1f80 |
|
36 #define SLCD_DATA 0x0200 |
|
37 #define SLCD_MDATA 0x1e00 |
|
38 #define SLCD_LEDS 0x0400 |
|
39 #define SLCD_MLEDS 0x1c00 |
|
40 #define SLCD_KEYS 0x0800 |
|
41 #define SLCD_MKEYS 0x1800 |
|
42 #define SLCD_MDEV 0xe000 |
|
43 |
|
44 #define SEND_PORT 6554 |
|
45 |
|
46 |
|
47 struct sockaddr_in sendaddr; /* Server send socket */ |
|
48 int sock = -1; |
|
49 |
|
50 extern int debug; |
|
51 |
|
52 |
|
53 void putLCDsocket(int fd, uint16_t data) |
|
54 { |
|
55 if (sock == -1) |
|
56 return; |
|
57 |
|
58 if (sendto(sock, &data, sizeof(uint16_t), 0, (struct sockaddr *)&sendaddr, sizeof(sendaddr)) != sizeof(uint16_t)) { |
|
59 syslog(LOG_NOTICE, "Socket send failed, closing socket: %s", strerror(errno)); |
|
60 if (shutdown(sock, SHUT_RDWR)) { |
|
61 syslog(LOG_NOTICE, "Can't shutdown socket: %s", strerror(errno)); |
|
62 } |
|
63 sock = -1; |
|
64 // } else { |
|
65 } |
|
66 } |
|
67 |
|
68 |
|
69 |
|
70 //void slcdHome(int fd) |
|
71 //{ |
|
72 //} |
|
73 |
|
74 |
|
75 |
|
76 void slcdClear(int fd) |
|
77 { |
|
78 putLCDsocket(fd, SLCD_CLEAR); |
|
79 putLCDsocket(fd, SLCD_HOME); |
|
80 } |
|
81 |
|
82 |
|
83 |
|
84 //void slcdDisplay(int fd, int state) |
|
85 //{ |
|
86 //} |
|
87 |
|
88 |
|
89 |
|
90 //void slcdCursor(int fd, int state) |
|
91 //{ |
|
92 //} |
|
93 |
|
94 |
|
95 |
|
96 //void slcdCursorBlink(int fd, int state) |
|
97 //{ |
|
98 //} |
|
99 |
|
100 |
|
101 |
|
102 //void slcdSendCommand(int fd, unsigned char command) |
|
103 //{ |
|
104 //} |
|
105 |
|
106 |
|
107 |
|
108 void slcdPosition(int fd, int x, int y) |
|
109 { |
|
110 uint16_t data = SLCD_DGRAM; |
|
111 |
|
112 data += (x & 0x1f) + ((y & 0x03) << 5); |
|
113 putLCDsocket(fd, data); |
|
114 } |
|
115 |
|
116 |
|
117 |
|
118 //void slcdCharDef(int fd, int index, unsigned char data[8]) |
|
119 //{ |
|
120 //} |
|
121 |
|
122 |
|
123 |
|
124 void slcdPutchar(int fd, unsigned char c) |
|
125 { |
|
126 uint16_t data = SLCD_DATA; |
|
127 |
|
128 data += c & 0x0ff; |
|
129 putLCDsocket(fd, data); |
|
130 } |
|
131 |
|
132 |
|
133 |
|
134 void slcdPuts(int fd, const char *string) |
|
135 { |
|
136 while (*string) |
|
137 slcdPutchar(fd, *string++); |
|
138 } |
|
139 |
|
140 |
|
141 |
|
142 //void slcdPrintf(int fd, const char *message, ...) |
|
143 //{ |
|
144 //} |
|
145 |
|
146 |
|
147 |
|
148 /* |
|
149 * Try to setup a udp connection to 127.0.0.1 so we duplicate the panel |
|
150 * display and keys of the real panel. This should fail on a production |
|
151 * system because there should no brewpanel program be running. If it |
|
152 * succeeds, all io will be duplicated over the network. |
|
153 */ |
|
154 int slcdInit(int fd, int cols, int rows) |
|
155 { |
|
156 if ((sock = socket(AF_INET, SOCK_DGRAM /*| SOCK_NONBLOCK */, 0)) < 0) { |
|
157 syslog(LOG_NOTICE, "slcdInit() can't create socket: %s", strerror(errno)); |
|
158 return -1; |
|
159 } |
|
160 |
|
161 /* |
|
162 * Setup address structure for the server socket. |
|
163 */ |
|
164 memset(&sendaddr, 0, sizeof(struct sockaddr_in)); |
|
165 sendaddr.sin_family = AF_INET; |
|
166 sendaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); |
|
167 sendaddr.sin_port = htons(SEND_PORT); |
|
168 |
|
169 if (connect(sock, (struct sockaddr *)&sendaddr, sizeof(sendaddr)) < 0) { |
|
170 close(sock); |
|
171 sock = -1; |
|
172 syslog(LOG_NOTICE, "slcdInit() can't bind sendsock %s", strerror(errno)); |
|
173 return -1; |
|
174 } |
|
175 |
|
176 syslog(LOG_NOTICE, "slcdInit() socket %d to brewpanel is active", sock); |
|
177 return 0; |
|
178 } |
|
179 |
|
180 |