- printf("connecting to coke machine...\n");
-
- giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600);
- if( giCoke_SerialFD == -1 ) {
- fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
- }
- else {
- // Reset the slot names.
- // - Dunno why this is needed, but the machine plays silly
- // sometimes.
- Writef("n0 Slot0\n");
- WaitForColon();
- Writef("n1 Slot1\n");
- WaitForColon();
- Writef("n2 Slot2\n");
- WaitForColon();
- Writef("n3 Slot3\n");
- WaitForColon();
- Writef("n4 Slot4\n");
- WaitForColon();
- Writef("n5 Slot5\n");
- WaitForColon();
- Writef("n6 Coke\n");
- }
-
- CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
- return 0;
-}
-
-int Coke_CanDispense(int UNUSED(User), int Item)
-{
- char tmp[40], *status;
- regmatch_t matches[4];
- int ret;
-
- // Sanity please
- if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD
-
- // Can't dispense if the machine is not connected
- if( giCoke_SerialFD == -1 )
- return -2;
-
- #if TRACE_COKE
- printf("Coke_CanDispense: Flushing\n");
- #endif
-
-
- // Wait for a prompt
- ret = 0;
- while( WaitForColon() && ret < 3 )
- {
- // Flush the input buffer
- char tmpbuf[512];
- read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
- #if TRACE_COKE
- printf("Coke_CanDispense: sending 'd7'\n");
- #endif
- Writef("d7\r\n");
- ret ++;
- }
- // Check for a timeout error
- if( !(ret < 3) ) {
- fprintf(stderr, "Coke machine timed out\n");
- return -2; // -EMYBAD
- }
-
- // You need to do a 'd7' before reading the status
- // - Otherwise it sometimes reports a full slot as empty
- // [TPG] (2011-02-19)
- if( ret == 0 )