X-Git-Url: https://git.ucc.asn.au/?p=uccvend-snackrom.git;a=blobdiff_plain;f=ROM2%2Fmain_basic.c;h=44e09448cdd7928d69eba988c6e331b3df280425;hp=8d54ee99e6c4441d04dadaf1a95b6441b3dcc791;hb=17af89d1b2f699e0fc9dbe6132b6b8e0c6c81068;hpb=24ecd46fb709e0b5994f6c3fa77c400ae9b92efa diff --git a/ROM2/main_basic.c b/ROM2/main_basic.c index 8d54ee9..44e0944 100644 --- a/ROM2/main_basic.c +++ b/ROM2/main_basic.c @@ -7,44 +7,270 @@ #include "display_basic.h" #include "keypad.h" #include "chime.h" -#include "server.h" #include "coinmech.h" +#include "motors.h" +#include "sci.h" #include "vend.h" -void _start() { - set_bus_expanded(); - display_init(); - /* enable RTI & set rate */ - /* init coin mech */ - /* scan for motors */ - main(); +void motor_reply(char* slotptr, u8 code) { + /* returns a message of the form MXYY - X is return code, YY is motor */ + wait_for_tx_free(); + sci_tx_buf[0] = 'M'; + sci_tx_buf[1] = code + '0'; + sci_tx_buf[2] = *slotptr; + sci_tx_buf[3] = *(slotptr+1); + sci_tx_buf[4] = '\n'; + sci_tx_buf[5] = 0; + send_packet(); +} + +void dispense_something() { + /* process a message VXX in sci_rx_buf where XX is motor number */ + u8 slot; + + if ((sci_rx_buf[1] < '0') || (sci_rx_buf[1] > '9') || + (sci_rx_buf[2] < '0') || (sci_rx_buf[2] > '9')) { + sci_rx_buf[1] = sci_rx_buf[2] = '0'; + motor_reply((char*)&sci_rx_buf[1], MOTOR_NOSLOT); + return; + } + + slot = (sci_rx_buf[1] - '0') * 10; + slot += sci_rx_buf[2] - '0'; + + motor_reply((char*)&sci_rx_buf[1], dispense_motor(slot)); +} + +void write_to_display() { + /* process a message in the form DXXXXXXXXXXX to send to display */ + u8 i; + char buf[10]; + for (i = 0; i < 10; i++) + if (sci_rx_buf[i+1]) + buf[i] = sci_rx_buf[i+1]; + else + break; + + for (; i < 10; i++) /* pad the rest out with spaces */ + buf[i] = ' '; + + set_msg(buf); + send_ack(); +} + +void send_balance() { + wait_for_tx_free(); + sci_tx_buf[0] = 'C'; + sci_tx_buf[1] = have_change?'0':'1'; + sci_tx_buf[2] = (coin_value/10000)%10; + sci_tx_buf[3] = (coin_value/1000)%10; + sci_tx_buf[4] = (coin_value/100)%10; + sci_tx_buf[5] = (coin_value/10)%10; + sci_tx_buf[6] = coin_value%10; + sci_tx_buf[7] = '\n'; + sci_tx_buf[8] = 0; + send_packet(); +} + +void give_change() { + u16 cost; + + if ((sci_rx_buf[1] < '0') || (sci_rx_buf[1] > '9') || + (sci_rx_buf[2] < '0') || (sci_rx_buf[2] > '9') || + (sci_rx_buf[3] < '0') || (sci_rx_buf[3] > '9') || + (sci_rx_buf[4] < '0') || (sci_rx_buf[4] > '9') || + (sci_rx_buf[5] < '0') || (sci_rx_buf[5] > '9')) { + send_nack(); + } + cost = sci_rx_buf[1] - '0'; + cost *= 10; cost = sci_rx_buf[2] - '0'; + cost *= 10; cost = sci_rx_buf[3] - '0'; + cost *= 10; cost = sci_rx_buf[4] - '0'; + cost *= 10; cost = sci_rx_buf[5] - '0'; + + coin_cost(cost); + send_ack(); +} + +void send_keypress(u8 key) { + /* send a packet of the form KX with X being the key, or R for reset */ + wait_for_tx_free(); + sci_tx_buf[0] = 'K'; + if (key == KEY_RESET) + sci_tx_buf[1] = 'R'; + else + sci_tx_buf[1] = (key%10)+'0'; + sci_tx_buf[2] = '\n'; + sci_tx_buf[3] = 0; + send_packet(); +} + +void send_door_msg(bool open) { + wait_for_tx_free(); + sci_tx_buf[0] = 'D'; + sci_tx_buf[1] = open?'1':'0'; + sci_tx_buf[2] = '\n'; + sci_tx_buf[3] = 0; + send_packet(); +} + +void do_chime() { + chime_start(); + send_ack(); } +void ping_pong() { + /* make sure it's really a ping */ + if (sci_rx_buf[1] != 'I' || + sci_rx_buf[2] != 'N' || + sci_rx_buf[3] != 'G' || + sci_rx_buf[4] != '\0') { + send_nack(); + return; + } + /* respond with ack & pong */ + wait_for_tx_free(); + my_strncpy(sci_tx_buf, "PONG\n", BUFFER_LEN); + send_packet(); +} + +u16 last_coin_value; +bool last_door_open; +char display_buf[11]; +/* cur_motor[0] is 0 for nothing, or 1..10, or 0xff to say redraw. + * cur_motor[1] is 0..9 and only meaningful is cur_motor[0] != 0. */ +u8 cur_motor[2]; + int main() { + u8 i; + for (i = 0; i < 11; i++) + display_buf[i] = ' '; + display_buf[10] = '\0'; + + changer_output = 0x7f; + _io_ports[M6811_PORTA] = 0xc0; /* display on. talking to serial port */ + _io_ports[M6811_DDRA] = 0xfc; + _io_ports[M6811_DDRD] = 0x3e; + _io_ports[M6811_SPCR] = M6811_MSTR | M6811_SPR1; + set_misc_output(0x00); + + display_init(); + set_msg(" HELLO "); + delay(1000); + + unlock(); /* enable interrupts */ + + set_msg(" CRUEL "); + + //comm_init(); + //coinmech_init(); + sci_init(); + keypad_init(); + last_coin_value = 0; + last_door_open = 0; + + delay(1000); + + set_msg(" WORLD "); + delay(1000); + + chime_start(); + + my_strncpy(sci_tx_buf, "5N4X0RZRUS\n", BUFFER_LEN); + send_packet(); + + cur_motor[0] = 0xff; while(1) { - /* - * have serial packet? - * - * decode msg & process: - * - dispense motor - * - display string - * - give change - * - beep - */ - - /* - * have keypress? - * - beep - * - send via serial - */ + if (cur_motor[0] == 0xff) { /* signal to say redraw screen */ + set_msg("*5N4X0RZ* "); + cur_motor[0] = 0; + } + + if (door_open() != last_door_open) { + last_door_open = door_open(); + send_door_msg(last_door_open); + chime_start(); + set_msg(last_door_open?"DOOR OPEN ":"DOOR CLOSE"); + } + + if (sci_have_packet) { + switch (sci_rx_buf[0]) { + case 'V': + dispense_something(); + break; + case 'D': + write_to_display(); + break; + case 'B': + do_chime(); + break; + case 'U': + send_balance(); + break; + case 'G': + give_change(); + break; + case 'P': + ping_pong(); + break; + default: + // shurg + send_nack(); + break; + } + msg_clr(); + } + + keypad_read(); + if (keypad_pressed()) { + if (last_key == KEY_RESET) { + cur_motor[0] = 0xff; + } else { + if (cur_motor[0]) { + u8 motor_num; + cur_motor[1] = last_key%10; + display_buf[1] = cur_motor[1]+'0'; + set_msg(display_buf); + + motor_num = cur_motor[0]%10; + motor_num *= 10; + motor_num += cur_motor[1]; + switch (dispense_motor(motor_num)) { + case MOTOR_HOME_FAIL: + set_msg(" HOME FAIL "); + break; + case MOTOR_CURRENT_FAIL: + set_msg(" OVER CRNT "); + break; + case MOTOR_SUCCESS: + set_msg("THANK YOU"); + break; + case MOTOR_NOSLOT: + set_msg(" NO MOTOR "); + break; + default: + set_msg("ERRRRRRRR?"); + break; + } + + display_buf[0] = ' '; + display_buf[1] = ' '; + cur_motor[0] = 0xff; + delay(500); + } else { + cur_motor[0] = last_key; + display_buf[0] = (last_key%10)+'0'; + set_msg(display_buf); + } + } + send_keypress(last_key); + } /* - * have coin balance change? - * - send via serial - */ + if (coin_value != last_coin_value) { + send_balance(); + last_coin_value = coin_value; + } + */ } } - -void rti() { - chime(); /* turn chime on or off as need be */ -}