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.
21 int Coke_InitHandler();
22 int Coke_CanDispense(int User, int Item);
23 int Coke_DoDispense(int User, int Item);
26 tHandler gCoke_Handler = {
32 char *gsCoke_SerialPort = "/dev/ttyS0";
34 regex_t gCoke_StatusRegex;
37 int Coke_InitHandler()
39 printf("connecting to coke machine...\n");
41 giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600);
42 if( giCoke_SerialFD == -1 ) {
43 fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
46 CompileRegex(&gCoke_StatusRegex, "^slot\\s+(\\d)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
50 int Coke_CanDispense(int User, int Item)
52 char tmp[32], *status;
53 regmatch_t matches[4];
57 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
59 // Ask the coke machine
60 sprintf(tmp, "s%i\n", Item);
61 write(giCoke_SerialFD, tmp, 2);
68 ret = read(giCoke_SerialFD, tmp, sizeof(tmp)-1);
69 //printf("ret = %i\n", ret);
71 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
74 ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
79 tmp[ matches[3].rm_eo ] = '\0';
80 status = &tmp[ matches[3].rm_so ];
82 printf("Machine responded slot status '%s'\n", status);
84 if( strcmp(status, "full") == 0 )
91 * \brief Actually do a dispense from the coke machine
93 int Coke_DoDispense(int User, int Item)
95 char tmp[32], *status;
96 regmatch_t matches[4];
99 if( Item < 0 || Item > 6 ) return -1;
102 sprintf(tmp, "d%i\n", Item);
103 write(giCoke_SerialFD, tmp, 2);
109 read(giCoke_SerialFD, tmp, sizeof(tmp)-1);
110 regexec(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0);
112 tmp[ matches[3].rm_eo ] = '\0';
113 status = &tmp[ matches[3].rm_so ];
115 printf("Machine responded slot status '%s'\n", status);