3 * UCC (University [of WA] Computer Club) Electronic Accounting System
5 * handler_coke.c - Coke controller code
7 * This file is licenced under the 3-clause BSD Licence. See the file
8 * COPYING for full details.
11 * - Remember, the coke machine echoes your text back to you!
21 #define READ_TIMEOUT 2 // 2 seconds for ReadChar
27 int Coke_InitHandler();
28 int Coke_CanDispense(int User, int Item);
29 int Coke_DoDispense(int User, int Item);
31 int ReadLine(int len, char *output);
34 tHandler gCoke_Handler = {
40 char *gsCoke_SerialPort = "/dev/ttyS0";
42 regex_t gCoke_StatusRegex;
45 int Coke_InitHandler()
47 printf("connecting to coke machine...\n");
49 giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600);
50 if( giCoke_SerialFD == -1 ) {
51 fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
54 // Reset the slot names.
55 // - Dunno why this is needed, but the machine plays silly
57 write(giCoke_SerialFD, "n0 Slot0\n", 9);
59 write(giCoke_SerialFD, "n1 Slot0\n", 9);
61 write(giCoke_SerialFD, "n2 Slot0\n", 9);
63 write(giCoke_SerialFD, "n3 Slot0\n", 9);
65 write(giCoke_SerialFD, "n4 Slot0\n", 9);
67 write(giCoke_SerialFD, "n5 Slot0\n", 9);
69 write(giCoke_SerialFD, "n6 Coke\n", 8);
72 CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
76 int Coke_CanDispense(int UNUSED(User), int Item)
78 char tmp[40], *status;
79 regmatch_t matches[4];
83 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
85 // Can't dispense if the machine is not connected
86 if( giCoke_SerialFD == -1 )
90 printf("Coke_CanDispense: Flushing\n");
96 while( WaitForColon() && ret < 3 )
98 // Flush the input buffer
100 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
102 printf("Coke_CanDispense: sending 'd7'\n");
104 write(giCoke_SerialFD, "d7\r\n", 4);
109 fprintf(stderr, "Coke machine timed out\n");
110 return -2; // -EMYBAD
113 // TODO: Handle "not ok" response to D7
116 printf("Coke_CanDispense: sending 's%i'\n", Item);
119 // Ask the coke machine
120 sprintf(tmp, "s%i\r\n", Item);
121 write(giCoke_SerialFD, tmp, 4);
124 printf("Coke_CanDispense: reading response\n");
126 // Read from the machine (ignoring empty lines)
127 while( (ret = ReadLine(sizeof(tmp)-1, tmp)) == 0 );
128 printf("ret = %i, tmp = '%s'\n", ret, tmp);
129 // Read back-echoed lines
130 while( tmp[0] == ':' || tmp[1] != 'l' )
132 ret = ReadLine(sizeof(tmp)-1, tmp);
133 printf("ret = %i, tmp = '%s'\n", ret, tmp);
138 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
140 perror("Coke Machine");
146 printf("Coke_CanDispense: wait for the prompt again\n");
149 // Eat rest of response
152 // Parse status response
153 ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
159 tmp[ matches[3].rm_eo ] = '\0';
160 status = &tmp[ matches[3].rm_so ];
162 printf("Machine responded slot status '%s'\n", status);
165 printf("Coke_CanDispense: done");
168 if( strcmp(status, "full") == 0 )
175 * \brief Actually do a dispense from the coke machine
177 int Coke_DoDispense(int UNUSED(User), int Item)
183 if( Item < 0 || Item > 6 ) return -1;
185 // Can't dispense if the machine is not connected
186 if( giCoke_SerialFD == -1 )
190 printf("Coke_DoDispense: flushing input\n");
195 while( WaitForColon() && ret < 3 )
197 // Flush the input buffer
199 read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
201 printf("Coke_DoDispense: sending 'd7'\n");
203 write(Item, "d7\r\n", 4);
207 printf("Coke_DoDispense: sending 'd%i'\n", Item);
210 sprintf(tmp, "d%i\r\n", Item);
211 write(giCoke_SerialFD, tmp, 4);
213 // Read empty lines and echo-backs
215 ret = ReadLine(sizeof(tmp)-1, tmp);
216 if( ret == -1 ) return -1;
218 printf("Coke_DoDispense: read %i '%s'\n", ret, tmp);
220 } while( ret == 0 || tmp[0] == ':' || tmp[0] == 'd' );
222 WaitForColon(); // Eat up rest of response
225 printf("Coke_DoDispense: done\n");
229 if( strcmp(tmp, "ok") == 0 ) {
230 // We think dispense worked
231 // - The machine returns 'ok' if an empty slot is dispensed, even if
232 // it doesn't actually try to dispense (no sound)
236 printf("Machine returned unknown value '%s'\n", tmp);
246 struct timeval timeout;
248 timeout.tv_sec = READ_TIMEOUT;
252 FD_SET(giCoke_SerialFD, &readfs);
254 ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
255 if( ret == 0 ) return 0; // Timeout
257 printf("readchar return %i\n", ret);
261 ret = read(giCoke_SerialFD, &ch, 1);
263 printf("ret = %i\n", ret);
275 FD_SET(giCoke_SerialFD, &readfs);
277 while( (ch = ReadChar()) != ':' && ch != 0);
279 if( ch == 0 ) return -1; // Timeout
284 int ReadLine(int len, char *output)
299 if( ch == '\n' || ch == '\r' ) {
306 //printf("ReadLine: output=%s\n", output);