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 = open(gsSnack_SerialPort, O_RDWR | O_NOCTTY | O_NONBLOCK);
41 if( giSnack_SerialFD == -1 ) {
42 fprintf(stderr, "ERROR: Unable to open snack serial port ('%s')\n", gsSnack_SerialPort);
45 InitSerial(giSnack_SerialFD, 9600);
47 regcomp(&gSnack_ResponseRegex, "^(\\d\\d\\d)(.*)$", REG_EXTENDED);
51 int Snack_CanDispense(int User, int Item)
54 if( Item < 0 || Item > 99 ) return -1;
56 // Hmm... could we implement slot statuses?
62 * \brief Actually do a dispense from the coke machine
64 int Snack_DoDispense(int User, int Item)
67 regmatch_t matches[4];
70 if( Item < 0 || Item > 99 ) return -1;
73 sprintf(tmp, "V%02i\n", Item);
74 write(giSnack_SerialFD, tmp, 2);
77 read(giSnack_SerialFD, tmp, sizeof(tmp)-1);
78 regexec(&gSnack_ResponseRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0);