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");
40 giCoke_SerialFD = open(gsCoke_SerialPort, O_RDWR | O_NOCTTY | O_NONBLOCK);
41 if( giCoke_SerialFD == -1 ) {
42 fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
45 InitSerial(giCoke_SerialFD, 9600);
47 CompileRegex(&gCoke_StatusRegex, "^slot\\s+(\\d)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
51 int Coke_CanDispense(int User, int Item)
53 char tmp[32], *status;
54 regmatch_t matches[4];
58 if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
60 // Ask the coke machine
61 sprintf(tmp, "s%i\n", Item);
62 write(giCoke_SerialFD, tmp, 2);
66 ret = read(giCoke_SerialFD, tmp, sizeof(tmp)-1);
67 //printf("ret = %i\n", ret);
69 fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
72 ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
77 tmp[ matches[3].rm_eo ] = '\0';
78 status = &tmp[ matches[3].rm_so ];
80 printf("Machine responded slot status '%s'\n", status);
82 if( strcmp(status, "full") == 0 )
89 * \brief Actually do a dispense from the coke machine
91 int Coke_DoDispense(int User, int Item)
93 char tmp[32], *status;
94 regmatch_t matches[4];
97 if( Item < 0 || Item > 6 ) return -1;
100 sprintf(tmp, "d%i\n", Item);
101 write(giCoke_SerialFD, tmp, 2);
104 read(giCoke_SerialFD, tmp, sizeof(tmp)-1);
105 regexec(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0);
107 tmp[ matches[3].rm_eo ] = '\0';
108 status = &tmp[ matches[3].rm_so ];
110 printf("Machine responded slot status '%s'\n", status);