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";
35 regex_t gSnack_ResponseRegex;
38 int Snack_InitHandler()
40 giSnack_SerialFD = InitSerial(gsSnack_SerialPort, 9600);
41 if( giSnack_SerialFD == -1 ) {
42 fprintf(stderr, "ERROR: Unable to open snack serial port ('%s')\n", gsSnack_SerialPort);
45 regcomp(&gSnack_ResponseRegex, "^(\\d\\d\\d)(.*)$", REG_EXTENDED);
49 int Snack_CanDispense(int UNUSED(User), int Item)
52 if( Item < 0 || Item > 99 ) return -1;
54 // Hmm... could we implement slot statuses?
60 * \brief Actually do a dispense from the coke machine
62 int Snack_DoDispense(int UNUSED(User), int Item)
65 regmatch_t matches[4];
68 if( Item < 0 || Item > 99 ) return -1;
71 sprintf(tmp, "V%02i\n", Item);
72 write(giSnack_SerialFD, tmp, 2);
75 read(giSnack_SerialFD, tmp, sizeof(tmp)-1);
76 regexec(&gSnack_ResponseRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0);