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.
20 int Coke_InitHandler();
21 int Coke_CanDispense(int User, int Item);
22 int Coke_DoDispense(int User, int Item);
25 tHandler gCoke_Handler = {
31 char *gsCoke_SerialPort = "/dev/ttyS0";
33 regex_t gCoke_StatusRegex;
36 int Coke_InitHandler()
38 giCoke_SerialFD = open(gsCoke_SerialPort, O_RDWR);
39 regcomp(&gCoke_StatusRegex, "^$", REG_EXTENDED);
43 int Coke_CanDispense(int User, int Item)
46 regmatch_t matches[4];
49 if( Item < 0 || Item > 6 ) return -1;
51 // Ask the coke machine
52 sprintf(tmp, "s%i", Item);
53 write(giCoke_SerialFD, tmp, 2);
56 read(giCoke_SerialFD, tmp, sizeof(tmp)-1);
57 regexec(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0);
59 printf("s%i response '%s'\n", Item, tmp);
65 * \brief Actually do a dispense from the coke machine
67 int Coke_DoDispense(int User, int Item)
70 regmatch_t matches[4];
73 if( Item < 0 || Item > 6 ) return -1;
76 sprintf(tmp, "d%i", Item);
77 write(giCoke_SerialFD, tmp, 2);
80 read(giCoke_SerialFD, tmp, sizeof(tmp)-1);
81 regexec(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0);
83 printf("d%i response '%s'\n", Item, tmp);