+int Coke_int_DropSlot(int Slot)
+{
+ uint8_t res;
+
+ if(Slot < 0 || Slot > 9) return -1;
+
+ // Can't dispense if the machine is not connected
+ if( !gCoke_Modbus )
+ return -2;
+
+ // Check if a dispense is in progress
+ if( modbus_read_bits(gCoke_Modbus, ciCoke_DropBitBase + Slot, 1, &res) <= 0 )
+ {
+ perror("Coke_int_DropSlot - modbus_read_bits#1");
+ return -2;
+ }
+ if( res != 0 )
+ {
+ // Manual dispense in progress
+ return -1;
+ }
+
+ // Dispense
+ if( modbus_write_bit(gCoke_Modbus, ciCoke_DropBitBase + Slot, 1) < 0 )
+ {
+ perror("Coke_int_DropSlot - modbus_write_bit");
+ return -2;
+ }
+
+ // Check that it started
+ usleep(1000); // 1ms
+ if( modbus_read_bits(gCoke_Modbus, ciCoke_DropBitBase + Slot, 1, &res) <= 0 )
+ {
+ perror("Coke_int_DropSlot - modbus_read_bits#2");
+ return -2;
+ }
+ if( res == 0 )
+ {
+ // Oops!, no drink
+ printf("Drink dispense failed, bit lowered too quickly\n");
+ return 1;
+ }
+
+ return 0;
+}