- sprintf(tmp, "d%i\n", Item);
- write(giCoke_SerialFD, tmp, 2);
+ Writef("d%i\r\n", Item);
+
+ // Read empty lines and echo-backs
+ do {
+ ret = ReadLine(sizeof(tmp)-1, tmp);
+ if( ret == -1 ) {
+ pthread_mutex_unlock(&gCoke_Mutex);
+ return -1;
+ }
+ TRACE("read %i '%s'\n", ret, tmp);
+ } while( ret == 0 || tmp[0] == ':' || tmp[0] == 'd' );
+
+ WaitForColon(); // Eat up rest of response
+
+ TRACE("done\n");
+
+ // TODO: Regex instead?
+ if( strcmp(tmp, "ok") == 0 ) {
+ // We think dispense worked
+ // - The machine returns 'ok' if an empty slot is dispensed, even if
+ // it doesn't actually try to dispense (no sound)
+ ret = 0;
+ }
+ else {
+ printf("Coke_DoDispense: Machine returned unknown value '%s'\n", tmp);
+ ret = -1;
+ }
+
+ TRACE("Updating slot status\n");
+
+ // Update status
+ WaitForColon();
+ Writef("s%i\r\n", Item);
+ len = ReadLine(sizeof tmp, tmp);
+ if(len == -1) gaCoke_CachedStatus[Item] = -1;
+ Coke_int_GetSlotStatus(tmp, Item);
+ {
+ char buf[512];
+ read(giCoke_SerialFD, buf, 512); // Flush
+ }