rendered paste body// USBIO.cpp// custom logic board cpp file#include "SDL.h"#include "SDL_Thread.h"#include "../usb/usb.h"#include "usbio.h"#include "../game_data.h"#include "../alarms.h"#include "../music.h"#include "service_usbio.cpp"USBIO *UsbIO;// External objectsextern GAME_DATA *GameData;extern ALARMS *Alarms;extern MUSIC *Sound;extern int USB_THREAD_EXIT;extern int IO_ENABLED;extern int GAME_CURRENCY;// note: Can not go in a object!!int USBIOThreadFunc(void *unused){ if (IO_ENABLED) { UsbIO->reset(); UsbIO->constructor(); } unsigned int old_credit=0; for(;;) { if (USB_THREAD_EXIT) return(0); SDL_Delay(1); if (IO_ENABLED) { if (old_credit!=GameData->gdata.credit) { old_credit=GameData->gdata.credit; if (GameData->gdata.credit<99) UsbIO->send_led(GameData->gdata.credit); else UsbIO->send_led('9'+'9'); } UsbIO->process_buttons(); UsbIO->process_credit(); UsbIO->auto_lift(); UsbIO->payout_bank(); UsbIO->payout_buffered_coins(); if (UsbIO->shopper_dump) { UsbIO->hopper_dump(); UsbIO->shopper_dump=0; } if (UsbIO->shopper_float) { UsbIO->hopper_float(); UsbIO->shopper_float=0; } UsbIO->process_hopper_logic(); } } }//constructorvoid USBIO::constructor(void){ meters_enabled=1;coin_in[0]=0;coin_in[1]=0;coin_in[2]=0; powerup=0;left_status=0;right_status=0;bank_toggle=0; reset_timeout=SDL_GetTicks(); icntr=0;slidef=0; for (int i=0;i<=3;i++) GameData->mdata.coin_buffer[i]=0; GameData->gdata.bank=0; //debug left_count=0; right_count=0; shopper_dump=0; shopper_float=0;}//payoutunsigned char USBIO::payout_bank(void){ if (GameData->gdata.bank>=1) { if (bank_toggle) { hopper_top_left(1); GameData->gdata.bank--; add_meter(1,METER_FEATURE_OUT); bank_toggle=0; } else { bank_toggle=1; hopper_top_right(1); GameData->gdata.bank--; add_meter(1,METER_FEATURE_OUT); } }}// inhibitunsigned char USBIO::inhibit(unsigned char status){ istatus=status; unsigned char string[80]; string[0]='@';string[1]=1;string[2]=CMD_INHIBIT;string[3]=status;string[4]='*'; usbio.WriteBuffer(string,5); Uint32 time=SDL_GetTicks()+1000; unsigned char data[80]; do { unsigned char reply=comms_recieve(data); if (reply==CMD_ACK) return(1); else if (reply) printf("Inhibit, recieved WRONG %d\n",reply); }while(time>SDL_GetTicks()); return(0); }//get coin entryunsigned char USBIO::get_entry(unsigned char *data){ unsigned char string[80]; string[0]='@';string[1]=0;string[2]=CMD_ENTRY;string[3]='*'; usbio.WriteBuffer(string,4); Uint32 time=SDL_GetTicks()+1000; do { unsigned char reply=comms_recieve(data); if (reply==CMD_ENTRY) return(1); else if (reply) printf("Get_Entry, recieved WRONG %d\n",reply); }while(time>SDL_GetTicks()); return(0); }// get levelsunsigned char USBIO::get_level(unsigned char *data){ unsigned char string[80]; string[0]='@';string[1]=0;string[2]=CMD_LEVEL;string[3]='*'; usbio.WriteBuffer(string,4); Uint32 time=SDL_GetTicks()+1000; do { unsigned char reply=comms_recieve(data); if (reply==CMD_LEVEL) return(1); else if (reply) printf("Get_Level, recieved WRONG %d\n",reply); }while(time>SDL_GetTicks()); return(0); }// get buttonsunsigned char USBIO::get_buttons(unsigned char *data){ unsigned char string[80]; string[0]='@';string[1]=0;string[2]=CMD_BUTTONS;string[3]='*'; usbio.WriteBuffer(string,4); Uint32 time=SDL_GetTicks()+1000; do { unsigned char reply=comms_recieve(data); if (reply==CMD_BUTTONS) return(1); else if (reply) printf("Get_Buttons, recieved WRONG %d\n",reply); }while(time>SDL_GetTicks()); return(0); }//get alarmsunsigned char USBIO::get_alarms(unsigned char *data){ unsigned char string[80]; string[0]='@';string[1]=0;string[2]=CMD_GET_ALARM;string[3]='*'; usbio.WriteBuffer(string,4); Uint32 time=SDL_GetTicks()+1000; do { unsigned char reply=comms_recieve(data); if (reply==CMD_ENTRY) //CMD_GET_ALARM) return(1); else if (reply) printf("Get_Alarms, recieved WRONG %d\n",reply); }while(time>SDL_GetTicks()); return(0); }// Reset Alarmsunsigned char USBIO::reset_alarms(void){ unsigned char string[80]; string[0]='@';string[1]=0;string[2]=CMD_ALARM_CLEAR;string[3]='*'; usbio.WriteBuffer(string,4); Uint32 time=SDL_GetTicks()+1000; unsigned char data[80]; do { unsigned char reply=comms_recieve(data); return(1); }while(time>SDL_GetTicks()); return(0); }// led displayunsigned char USBIO::send_led(unsigned char number){ unsigned char string[80]; string[0]='@';string[1]=1;string[2]=CMD_LED;string[3]=number;string[4]='*'; usbio.WriteBuffer(string,5); unsigned char data[256]; Uint32 time=SDL_GetTicks()+1000; do { unsigned char reply=comms_recieve(data); if (reply==CMD_ACK) return(1); else if (reply) printf("Send_Led, recieved WRONG %d\n",reply); }while(time>SDL_GetTicks()); return(0);}// cycle liftvoid USBIO::auto_lift(void){ static Uint32 tmr=0; static int status=0; int lift_ammount=100; if (GameData->mdata.lift_coin_count[HOPPER_LOWER_LEFT]>=GameData->GetBucket() || GameData->mdata.lift_coin_count[HOPPER_LOWER_RIGHT]>=GameData->GetBucket()) { if (status==0) { tmr=SDL_GetTicks()+2000; status=1; } if (usbio_lift_cycle()==CMD_ACK) { GameData->mdata.lift_coin_count[HOPPER_LOWER_LEFT] = 0 ; GameData->mdata.lift_coin_count[HOPPER_LOWER_RIGHT] = 0 ; } else { printf("LIFT FAILED\n"); } }}/// PAYOUT LEFTvoid USBIO::hopper_top_left(unsigned char coins){ if (GameData->mdata.hopper_level[2]==1 && GameData->mdata.hopper_level[3]==0) { hopper_top_right(coins); return; } if (Alarms->GetHopperAlarm()&(1<<HOPPER_LOWER_LEFT) || Alarms->GetHopperAlarm()&(1<<HOPPER_UPPER_LEFT)) { if (Alarms->GetHopperAlarm()&(1<<HOPPER_LOWER_RIGHT) || Alarms->GetHopperAlarm()&(1<<HOPPER_UPPER_RIGHT)) { return; } else { hopper_top_right(coins); return; } } int result=usbio_payout(HOPPER_UPPER_LEFT,coins); if (result==CMD_ERROR_HOPPER) { Alarms->SetHopperAlarm(HOPPER_UPPER_LEFT); } else if (result == CMD_ERROR_LIFT) { GameData->mdata.coin_buffer[HOPPER_UPPER_LEFT]+=coins; } else if (result == CMD_ERROR_BUSY) { GameData->mdata.coin_buffer[HOPPER_UPPER_LEFT]+=coins; } else if (result==CMD_ACK) { GameData->mdata.lift_coin_count[HOPPER_UPPER_LEFT]+=coins; result=usbio_payout(HOPPER_LOWER_LEFT,coins); if (result==CMD_ERROR_HOPPER) // set alarm Alarms->SetHopperAlarm(HOPPER_LOWER_LEFT); else if (result == CMD_ERROR_LIFT) GameData->mdata.coin_buffer[HOPPER_LOWER_LEFT]+=coins; else if(result == CMD_ERROR_BUSY) GameData->mdata.coin_buffer[HOPPER_LOWER_LEFT]+=coins; else if (result == CMD_ACK) GameData->mdata.lift_coin_count[HOPPER_LOWER_LEFT]+=coins; }}/// PAYOUT RIGHTvoid USBIO::hopper_top_right(unsigned char coins){ if (GameData->mdata.hopper_level[3]==1 && GameData->mdata.hopper_level[2]==0) { hopper_top_left(coins); return; } if (Alarms->GetHopperAlarm()&(1<<HOPPER_LOWER_RIGHT) || Alarms->GetHopperAlarm()&(1<<HOPPER_UPPER_RIGHT)) { if (Alarms->GetHopperAlarm()&(1<<HOPPER_LOWER_LEFT) || Alarms->GetHopperAlarm()&(1<<HOPPER_UPPER_LEFT)) { return; } else { hopper_top_left(coins); return; } } int result=usbio_payout(HOPPER_UPPER_RIGHT,coins); // errors if (result==CMD_ERROR_HOPPER) Alarms->SetHopperAlarm(HOPPER_UPPER_RIGHT); else if (result == CMD_ERROR_LIFT) GameData->mdata.coin_buffer[HOPPER_UPPER_RIGHT]+=coins; else if (result == CMD_ERROR_BUSY) GameData->mdata.coin_buffer[HOPPER_UPPER_RIGHT]+=coins; else if (result==CMD_ACK) { GameData->mdata.lift_coin_count[HOPPER_UPPER_RIGHT]+=coins; result=usbio_payout(HOPPER_LOWER_RIGHT,coins); if (result==CMD_ERROR_HOPPER) Alarms->SetHopperAlarm(HOPPER_LOWER_RIGHT);///error=HOPPER_ERROR;//hopper_error(); else if (result == CMD_ERROR_LIFT) GameData->mdata.coin_buffer[HOPPER_LOWER_RIGHT]+=coins; else if (result == CMD_ERROR_BUSY) GameData->mdata.coin_buffer[HOPPER_LOWER_RIGHT]+=coins; else if (result == CMD_ACK) GameData->mdata.lift_coin_count[HOPPER_LOWER_RIGHT]+=coins; }}// spare coinsunsigned char USBIO::payout_buffered_coins(void){ for (int i=0;i<=3;i++) if (GameData->mdata.coin_buffer[i]) { int result=usbio_payout(i,GameData->mdata.coin_buffer[i]); // set alarm if (result==CMD_ERROR_HOPPER) Alarms->SetHopperAlarm(i);//error=HOPPER_ERROR; else if (result==CMD_ACK) { GameData->mdata.lift_coin_count[i]+= GameData->mdata.coin_buffer[i]; if (i==HOPPER_UPPER_LEFT || i==HOPPER_UPPER_RIGHT) { int res=usbio_payout(i,GameData->mdata.coin_buffer[i]); // set alarm if (res==CMD_ERROR_HOPPER) Alarms->SetHopperAlarm(i); else if (res==CMD_ACK) GameData->mdata.lift_coin_count[i]+= GameData->mdata.coin_buffer[i]; else if (res==CMD_ERROR_LIFT) { if (i==HOPPER_UPPER_LEFT) GameData->mdata.coin_buffer[HOPPER_LOWER_LEFT]+=GameData->mdata.coin_buffer[i]; if (i==HOPPER_UPPER_RIGHT) GameData->mdata.coin_buffer[HOPPER_LOWER_RIGHT]+=GameData->mdata.coin_buffer[i]; } else if (res==CMD_ERROR_BUSY ) { if (i==HOPPER_UPPER_LEFT) GameData->mdata.coin_buffer[HOPPER_LOWER_LEFT]+=GameData->mdata.coin_buffer[i]; if (i==HOPPER_UPPER_RIGHT) GameData->mdata.coin_buffer[HOPPER_LOWER_RIGHT]+=GameData->mdata.coin_buffer[i]; } } GameData->mdata.coin_buffer[i]=0; } }}// setup usbio modulevoid USBIO::usbio_setup(void){ if (! (usbio.OpenDevDesc("HCDEV004")) ) { fflush(stdout);exit(0); } usbio.SetBaudRate(921600); usbio.SetTimeout(200); usbio.ResetDev(); usbio.SetResetPipeRetryCount(500); usbio_clear_errors(); SDL_Thread *IOthread; IOthread = SDL_CreateThread(USBIOThreadFunc, NULL);}// Clear all errorsunsigned char USBIO::usbio_clear_errors(void){ unsigned char string[80]; string[0]='@';string[1]=0;string[2]=CMD_CERROR;string[3]='*'; usbio.WriteBuffer(string,4); Uint32 time=SDL_GetTicks()+1000; do { unsigned char reply=comms_recieve(string); if (reply==CMD_ACK) return(1); else if (reply) printf("Clear_Errors, recieved WRONG %d\n",reply); }while(time>SDL_GetTicks()); return(0);}// send ack to usbio moduleunsigned char USBIO::usbio_ack(void){ unsigned char string[80]; string[0]='@';string[1]=0;string[2]=CMD_ACK;string[3]='*'; usbio.WriteBuffer(string,4); Uint32 time=SDL_GetTicks()+1000; do { unsigned char reply=comms_recieve(string); if (reply==CMD_ACK) return(1); else if (reply) printf("USBIO_ACK, recieved WRONG %d\n",reply); }while(time>SDL_GetTicks()); return(0);}// Get status of hopperunsigned char USBIO::usbio_hopper_status(unsigned char *data){ unsigned char string[80]; string[0]='@';string[1]=0;string[2]=CMD_HSTATUS;string[3]='*'; usbio.WriteBuffer(string,4); Uint32 time=SDL_GetTicks()+1000; do { unsigned char reply=comms_recieve(data); if (reply==10) return(1); else if (reply) printf("Hopper_status, recieved WRONG %d\n",reply); }while(time>SDL_GetTicks()); return(0);}// Send lift into fill cycleunsigned char USBIO::usbio_lift_cycle(){ unsigned char string[80]; string[0]='@';string[1]=0;string[2]=CMD_LIFT_CYCLE;string[3]='*'; usbio.WriteBuffer(string,4); Uint32 time=SDL_GetTicks()+1000; do { unsigned char reply=comms_recieve(string); if (reply) { if (reply==CMD_ACK) ; return(reply); } }while(time>SDL_GetTicks()); return(0);}// Payout unsigned char USBIO::usbio_payout(unsigned char hopper,unsigned char coins){ unsigned char string[80]; if (coins==0) return(CMD_ACK); GameData->mdata.target_timeout=SDL_GetTicks()+TARGET_TIMEOUT; string[0]='@';string[1]=2;string[2]=CMD_HOPPER;string[3]=hopper;string[4]=coins;string[5]='*'; usbio.WriteBuffer(string,6); Uint32 time=SDL_GetTicks()+1000; do { unsigned char reply=comms_recieve(string); if (reply) return(reply); }while(time>SDL_GetTicks()); printf("Payout Fail, NO REPLAY!!!!!!!!\n"); return(0);}// recieve data from moduleint USBIO::comms_recieve(unsigned char *input){ unsigned char no_of_data_bytes = 0; unsigned char command = 0; unsigned char temp [512]; usbio.ReadBuffer(temp,1); if (temp[0]!='@') return(FALSE); usbio.ReadBuffer(&no_of_data_bytes,1); usbio.ReadBuffer(&command,1); int num_bytes = usbio.ReadBuffer(temp,no_of_data_bytes); if (num_bytes != no_of_data_bytes ) return(FALSE); // read data for (int i=0;i<no_of_data_bytes;i++) input[i]=temp[i]; temp[0]=0; usbio.ReadBuffer(temp,1); if (temp[0]!='*') return(FALSE); return(command);}// get errorsunsigned char USBIO::get_error(void){ return(error);}// process misc hopper stuffvoid USBIO::process_hopper_logic(void){ static unsigned char level_count[4] = {0,0,0,0}; static Uint32 ltimer[4] = {0,0,0,0}; unsigned char temp[1024]; if (get_level(temp)==0) return; GameData->mdata.inpdata[0]=temp[0]; GameData->mdata.inpdata[1]=temp[1]; GameData->mdata.inpdata[2]=temp[2]; GameData->mdata.inpdata[3]=temp[3]; for (int i=0;i<=3;i++) { int shift=i+4; if (temp[0]&1<<shift) { if (ltimer[i]<SDL_GetTicks()) { ltimer[i]=SDL_GetTicks()+1000; if (level_count[i]<15) level_count[i]++; else GameData->mdata.hopper_level[i]=1; } } else { if (ltimer[i]<SDL_GetTicks()) { ltimer[i]=SDL_GetTicks()+1000; if (level_count[i]>0) level_count[i]--; else GameData->mdata.hopper_level[i]=0; } } }}// bug, memory overflow tracked to here.// routine functions normal unless this is run!!!void USBIO::process_credit(void){ unsigned char temp[1024]; if( get_entry(temp) ) { int added[3]={0,0,0}; if (powerup==0) { coin_in[0]=temp[0]; coin_in[1]=temp[1]; coin_in[0]=temp[2]; inhibit(1); powerup=83; return; } if (temp[2]==0) { if (icntr==0) { inhibit(1);//Enable coin_in[2]=temp[2]; icntr=1; } } else icntr=0; for (int i=0;i<=2;i++) { if (temp[i]>coin_in[i] && temp[i]) { added[i]=temp[i]-coin_in[i];// difference coin_in[i]=temp[i]; } else if (temp[i]<coin_in[i] && temp[i]) { int parta=255-coin_in[i]; int partb=temp[i]; added[i]=parta+partb; coin_in[i]=temp[i]; } } if (added[0] && added[0]<=50) { left_count+=added[0];//debug Sound->PlayCoinSlide(); hopper_top_left(added[0]); add_meter(added[0],METER_COIN_IN); GameData->AddCoinsIn(added[0]); } if (added[1] && added[1]<=50) { right_count+=added[1];//debug Sound->PlayCoinSlide(); hopper_top_right(added[1]); add_meter(added[1],METER_COIN_IN); GameData->AddCoinsIn(added[1]);// } if (added[2] && added[2]<=50) { if (GAME_CURRENCY==1) GameData->gdata.credit+=added[2]; else GameData->gdata.credit+=added[2]*5; add_meter(added[2],METER_COIN_IN); GameData->AddCoinsIn(added[2]);// Sound->PlayMech(); } } auto_in();}// Auto Inhibit..void USBIO::auto_in(void){ if (1) { int hopper_low=0; if ( GameData->gdata.credit>400 && !GAME_CURRENCY) hopper_low++; for (int i=0;i<=3;i++) if(GameData->mdata.hopper_level[i]) hopper_low++; if ( GameData->gdata.credit>79 && GAME_CURRENCY) hopper_low++; if (hopper_low) { if (istatus==1) // mech enabled inhibit(0); } else { if (istatus==0 && GameData->gdata.credit<=79) inhibit(1); } }}// process service buttonsvoid USBIO::process_buttons(void){ unsigned char temp[255]; static char old_lbutton=0,old_rbutton=0; temp[0]=0;temp[1]=0;temp[2]=0;temp[3]=0; if (get_buttons(temp)) { GameData->mdata.inputs[0]=temp[0]; GameData->mdata.inputs[1]=temp[1]; GameData->mdata.inputs[2]=temp[2]; unsigned char gistatus=GameData->gdata.credit,left_button=0; if(temp[BUTTON_LEFT_FIRE_POS]&BUTTON_LEFT_FIRE) left_button=1; if (!left_status && left_button) left_status=1; if (left_status && !left_button) left_status=2; if (left_status==2) { left_count++;//debug if (GameData->gdata.credit) { Sound->PlayButton(); hopper_top_left(1); GameData->gdata.credit--; } left_status=0; } if(GAME_CURRENCY && temp[BUTTON_RIGHT_FIRE_POS]&BUTTON_RIGHT_FIRE && temp[BUTTON_LEFT_FIRE_POS]&BUTTON_LEFT_FIRE && old_lbutton && gistatus==BSTATUS) { Sound->PlayButton(); hopper_top_left(1); hopper_top_right(1); return; } unsigned char right_button=0; if(temp[BUTTON_RIGHT_FIRE_POS]&BUTTON_RIGHT_FIRE) right_button=1; if (right_status==0 && right_button==1) right_status=1; if (right_status==1 && right_button==0) right_status=2; if (right_status==2) { left_count++; if (GameData->gdata.credit) { Sound->PlayButton(); hopper_top_right(1); GameData->gdata.credit--; } right_status=0; } if(temp[BUTTON_RESET_POS]&BUTTON_RESET && reset_timeout<SDL_GetTicks()) { reset(); usbio_clear_errors(); Alarms->Reset();; reset_timeout=SDL_GetTicks()+5000; } if(temp[BUTTON_TILT_POS]&BUTTON_TILT) Alarms->SetTiltAlarm(); if(temp[BUTTON_SLIDE_ALARM_POS]&BUTTON_SLIDE_ALARM) Alarms->SetSlideAlarm(); if(temp[BUTTON_MOTOR_JAMM_POS]&BUTTON_MOTOR_JAMM) Alarms->SetMotorAlarm(); old_lbutton=left_button; old_rbutton=right_button; }}// Resetunsigned char USBIO::reset(void){ unsigned char string[80]; string[0]='@';string[1]=0;string[2]=CMD_RESET;string[3]='*'; usbio.WriteBuffer(string,4); Uint32 time=SDL_GetTicks()+1000; unsigned char data[80]; do { unsigned char reply=comms_recieve(data); if (reply==CMD_ACK) return(1); else if (reply) printf("RESET, recieved WRONG %d\n",reply); }while(time>SDL_GetTicks()); return(0); }void USBIO::meters_enable(int enable){ meters_enabled=enable;}// Add to meterunsigned char USBIO::add_meter(unsigned char count,unsigned char meter){ if (meters_enabled==0) return(1); unsigned char string[80]; string[0]='@';string[1]=2;string[2]=CMD_SET_METER;string[3]=count;string[4]=meter;string[5]='*'; usbio.WriteBuffer(string,6); Uint32 time=SDL_GetTicks()+1000; unsigned char data[80]; do { unsigned char reply=comms_recieve(data); if (reply==CMD_ACK) return(1); if (reply==CMD_NACK) return(0); }while(time>SDL_GetTicks()); return(0); }/// Hopper Dump unsigned char USBIO::hopper_dump_left(unsigned char coins){ int result=usbio_payout(HOPPER_UPPER_LEFT,coins);}unsigned char USBIO::hopper_dump_right(unsigned char coins){ int result=usbio_payout(HOPPER_UPPER_RIGHT,coins);}//Lift Status,unsigned char USBIO::get_lift_status(unsigned char *data){ unsigned char string[80]; string[0]='@';string[1]=0;string[2]=CMD_LSTATUS;string[3]='*'; usbio.WriteBuffer(string,4); Uint32 time=SDL_GetTicks()+1000; do { unsigned char reply=comms_recieve(data); if (reply==CMD_LSTATUS) return(1); else if (reply) printf("get_lift_status, recieved WRONG %d\n",reply); }while(time>SDL_GetTicks()); return(0); }