- // Ask the coke machine
- sprintf(tmp, "s%i\r\n", Item);
- write(giCoke_SerialFD, tmp, 4);
-
- #if TRACE_COKE
- printf("Coke_CanDispense: reading response\n");
- #endif
- // Read from the machine (ignoring empty lines)
- while( (ret = ReadLine(sizeof(tmp)-1, tmp)) == 0 );
- printf("ret = %i, tmp = '%s'\n", ret, tmp);
- if( tmp[0] == ':' ) {
- ret = ReadLine(sizeof(tmp)-1, tmp);
- printf("ret = %i, tmp = '%s'\n", ret, tmp);
- }
-
- // Catch an error
- if( ret <= 0 ) {
- fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
- if( ret == -1 ) {
- perror("Coke Machine");
+ pthread_mutex_lock(&gCoke_Mutex);
+
+ while( ReadLine(sizeof tmp, tmp) >= 0 ) ;
+ TRACE("send d7\n");
+ Writef("d7\r\n"); // Update slot statuses
+ if( WaitForColon() ) goto ret;
+ TRACE("send s\n");
+ Writef("s\r\n");
+ do {
+ i = ReadLine(sizeof tmp, tmp); // Read back what we just said
+ if( i == -1 ) {
+ TRACE("Eat read failed");
+ goto ret;