3 * UCC (University [of WA] Computer Club) Electronic Accounting System
6 * handler_snack.c - Snack machine code
8 * This file is licenced under the 3-clause BSD Licence. See the file
9 * COPYING for full details.
22 int Snack_InitHandler();
23 int Snack_CanDispense(int User, int Item);
24 int Snack_DoDispense(int User, int Item);
27 tHandler gSnack_Handler = {
33 char *gsSnack_SerialPort = "/dev/ttyS1";
36 regex_t gSnack_ResponseRegex;
40 int Snack_InitHandler()
43 giSnack_SerialFD = InitSerial(gsSnack_SerialPort, 9600);
44 if( giSnack_SerialFD == -1 ) {
45 fprintf(stderr, "ERROR: Unable to open snack serial port ('%s')\n", gsSnack_SerialPort);
48 regcomp(&gSnack_ResponseRegex, "^(\\d\\d\\d)(.*)$", REG_EXTENDED);
53 int Snack_CanDispense(int UNUSED(User), int Item)
56 if( Item < 0 || Item > 99 ) return -1;
58 // Hmm... could we implement slot statuses?
64 * \brief Actually do a dispense from the coke machine
66 int Snack_DoDispense(int UNUSED(User), int Item)
69 if( Item < 0 || Item > 99 ) return -1;
73 regmatch_t matches[4];
76 sprintf(tmp, "V%02i\n", Item);
77 write(giSnack_SerialFD, tmp, 2);
80 read(giSnack_SerialFD, tmp, sizeof(tmp)-1);
81 regexec(&gSnack_ResponseRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0);