3 * UCC (University [of WA] Computer Club) Electronic Accounting System
5 * handler_coke.c - Coke controller code
7 * This file is licenced under the 3-clause BSD Licence. See the file
8 * COPYING for full details.
11 * - Remember, the coke machine echoes your text back to you!
19 #include <modbus/modbus.h>
22 #define MIN_DISPENSE_PERIOD 2
23 #define COKE_RECONNECT_RATELIMIT 2
26 const int ciCoke_MinPeriod = 5;
27 const int ciCoke_DropBitBase = 1024;
28 const int ciCoke_StatusBitBase = 16;
33 int Coke_InitHandler();
34 int Coke_CanDispense(int User, int Item);
35 int Coke_DoDispense(int User, int Item);
36 int Coke_int_ConnectToPLC(void);
37 int Coke_int_GetSlotFromItem(int Item, int bDispensing);
38 int Coke_int_IsSlotEmpty(int Slot);
39 int Coke_int_DropSlot(int Slot);
40 static int _ReadBit(int BitNum, uint8_t *Value);
41 static int _WriteBit(int BitNum, uint8_t Value);
44 tHandler gCoke_Handler = {
50 const char *gsCoke_ModbusAddress = "130.95.13.73";
51 modbus_t *gCoke_Modbus;
52 time_t gtCoke_LastDispenseTime;
53 time_t gtCoke_LastReconnectTime;
54 int gbCoke_DummyMode = 1;
55 int giCoke_NextCokeSlot = 0;
58 int Coke_InitHandler()
60 // Configuable dummy/blank mode (all dispenses succeed)
61 // TODO: Find a better way of handling missing/invalid options
62 if( Config_GetValueCount("coke_dummy_mode") > 0 )
64 gbCoke_DummyMode = Config_GetValue_Bool("coke_dummy_mode", 0);
65 if(gbCoke_DummyMode == -1) gbCoke_DummyMode = 0;
69 if( !gbCoke_DummyMode )
71 Coke_int_ConnectToPLC();
77 int Coke_CanDispense(int UNUSED(User), int Item)
81 // Check for 'dummy' mode
82 if( gbCoke_DummyMode )
86 slot = Coke_int_GetSlotFromItem(Item, 0);
87 if(slot < 0) return -1;
89 return Coke_int_IsSlotEmpty(slot);
93 * \brief Actually do a dispense from the coke machine
95 int Coke_DoDispense(int UNUSED(User), int Item)
98 // Check for 'dummy' mode
99 if( gbCoke_DummyMode )
103 slot = Coke_int_GetSlotFromItem(Item, 1);
104 if(slot < 0) return -1;
106 // Make sure there are not two dispenses within n seconds
107 if( time(NULL) - gtCoke_LastDispenseTime < ciCoke_MinPeriod )
109 int delay = ciCoke_MinPeriod - (time(NULL) - gtCoke_LastDispenseTime);
110 printf("Wait %i seconds?\n", delay);
112 printf("wait done\n");
114 gtCoke_LastDispenseTime = time(NULL);
116 return Coke_int_DropSlot(slot);
119 // --- INTERNAL FUNCTIONS ---
120 int Coke_int_ConnectToPLC(void)
123 if( time(NULL) - gtCoke_LastReconnectTime < COKE_RECONNECT_RATELIMIT )
128 gCoke_Modbus = modbus_new_tcp(gsCoke_ModbusAddress, 502);
131 perror("coke - modbus_new_tcp");
132 gtCoke_LastReconnectTime = time(NULL);
136 printf("Connecting to coke PLC machine on '%s'\n", gsCoke_ModbusAddress);
137 fprintf(stderr, "Connecting to coke PLC machine on '%s'\n", gsCoke_ModbusAddress);
139 if( modbus_connect(gCoke_Modbus) )
141 gtCoke_LastReconnectTime = time(NULL);
142 perror("coke - modbus_connect");
143 modbus_free(gCoke_Modbus);
151 int Coke_int_GetSlotFromItem(int Item, int bDispensing)
153 if( Item < 0 || Item > 6 ) return -1;
159 // Iterate though coke slots and find the first one with a drink avaliable
160 // `giCoke_NextCokeSlot` ensures that the slots rotate
161 for( int i = 0; i < 4; i ++ )
163 int slot = 6 + (i + giCoke_NextCokeSlot) % 4;
164 if( !Coke_int_IsSlotEmpty(slot) )
167 giCoke_NextCokeSlot ++;
168 if(giCoke_NextCokeSlot == 4) giCoke_NextCokeSlot = 0;
170 return slot; // Drink avaliable
175 // - Return 6, even if it's empty, the checks elsewhere will avoid problems
179 int Coke_int_IsSlotEmpty(int Slot)
183 if( Slot < 0 || Slot > 9 ) return -1;
186 if( _ReadBit(ciCoke_StatusBitBase + Slot, &status) )
188 perror("Coke_int_IsSlotEmpty - modbus_read_bits");
195 int Coke_int_DropSlot(int Slot)
199 if(Slot < 0 || Slot > 9) return -1;
201 // Check if a dispense is in progress
202 if( _ReadBit(ciCoke_DropBitBase + Slot, &res) )
204 perror("Coke_int_DropSlot - modbus_read_bits#1");
209 // Manual dispense in progress
214 if( _WriteBit(ciCoke_DropBitBase + Slot, 1) )
216 perror("Coke_int_DropSlot - modbus_write_bit");
220 // Check that it started
222 if( _ReadBit(ciCoke_DropBitBase + Slot, &res) )
224 perror("Coke_int_DropSlot - modbus_read_bits#2");
230 printf("Drink dispense failed, bit lowered too quickly\n");
237 int _ReadBit(int BitNum, uint8_t *Value)
240 if( !gCoke_Modbus && Coke_int_ConnectToPLC() )
242 if( modbus_read_bits( gCoke_Modbus, BitNum, 1, Value) >= 0 )
244 if( Coke_int_ConnectToPLC() )
246 if( modbus_read_bits( gCoke_Modbus, BitNum, 1, Value) >= 0 )
251 int _WriteBit(int BitNum, uint8_t Value)
254 if( !gCoke_Modbus && Coke_int_ConnectToPLC() )
256 if( modbus_write_bit( gCoke_Modbus, BitNum, Value != 0 ) >= 0 )
258 if( Coke_int_ConnectToPLC() )
260 if( modbus_write_bit( gCoke_Modbus, BitNum, Value != 0 ) >= 0 )