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 giCoke_SerialFD = open(gsCoke_SerialPort, O_RDWR);
40 regcomp(&gCoke_StatusRegex, "^slot\\s+(\\d)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
44 int Coke_CanDispense(int User, int Item)
46 char tmp[32], *status;
47 regmatch_t matches[4];
50 if( Item < 0 || Item > 6 ) return -1;
52 // Ask the coke machine
53 sprintf(tmp, "s%i\n", Item);
54 write(giCoke_SerialFD, tmp, 2);
57 read(giCoke_SerialFD, tmp, sizeof(tmp)-1);
58 regexec(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0);
60 tmp[ matches[3].rm_eo ] = '\0';
61 status = &tmp[ matches[3].rm_so ];
63 printf("Machine responded slot status '%s'\n", status);
65 if( strcmp(status, "full") == 0 )
72 * \brief Actually do a dispense from the coke machine
74 int Coke_DoDispense(int User, int Item)
76 char tmp[32], *status;
77 regmatch_t matches[4];
80 if( Item < 0 || Item > 6 ) return -1;
83 sprintf(tmp, "d%i\n", Item);
84 write(giCoke_SerialFD, tmp, 2);
87 read(giCoke_SerialFD, tmp, sizeof(tmp)-1);
88 regexec(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0);
90 tmp[ matches[3].rm_eo ] = '\0';
91 status = &tmp[ matches[3].rm_so ];
93 printf("Machine responded slot status '%s'\n", status);