Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

InitOpamp

Initialize shield strength, hull strength


Reset counter for incrementing shield strength
Set default values for power status
Set initial value for if ship is disabled
Post INIT event

RunOpamp
ES_INIT_OPAMP:
Initialize joystick
Initialize timer
Initialize UART
ES_NEW_KEY:
‘h’:
Initialize ES_SHIP_HIT event
Posy ES_SHIP_HIT to this service
‘n’:
Set sent power status values to input from potentiometer
‘s:
Change sent power status to preset value
‘e’:
Change sent power status to preset value
‘w’:
Change sent power status to preset value
ES_NEW_LOCAL:
Initialize local packet
Fill local packet
Complete local packet
Write packet to UART
Repeat for the other team station
ES_RXPACKET:
Copies received packet into RxPacket and gets packet type
Decode packet
ES_SHIP_HIT:
Update shield and hull strength
Initialize timer for LED
Turn on LED
ES_TIMEOUT:
Turn off LED

ISR Timer
Read input from potentiometers
calculate set ratios for power status
calculate power for navigation and weapon based on analog input
adjust weapon power based on navigation power
charge shield while ship is not disabled
update OLED display

updateStrength
if ship is already inactive
do nothing
else if ship still active
if damage only occurs to shield
decrement shield strength by damage
else if damage occurs to hull (shield can't hold damage)
calculate remaining damage after shield defense
if hull can hold remaining damage
decrement hull by remaining damage
set shield strength to 0
else if hull can't hold remaining damage
mark ship as inactive
set shield strength to 0
set flag for ship disabled

chargeShield
if this is first time within 1 sec
increment power counter
if shield already at maximum charge
do nothing and skip charging
else if there is enough capacity for all charge
charge by allocated shield power
else if there is not enough capacity for all charge
set shield strength to maximum capacity
else if next time the ISR fires is in new second
reset counter
else if in middle of a second
increment counter
InitOpampOLED
initialize deferral queue
post the initial transition event
set initial state to Initial State

RunOpampOLED
InitOLEDState:
If ThisEvent.EventType is ES_INIT_OPAMP_OLED
Initialize SPI settings
build up the u8g2 structure with the proper values for our display
use the next 5 lines verbatim in your initialization
pass all that stuff on to the display to initialize it
turn off power save so that the display will be on
choose the font. this one is mono-spaced and has a reasonable size
overwrite the background color of newly written character
Set CurrentState to NotWriting
initialize timer for refresh, refresh every half sec
NotWriting:
If ThisEvent.EventType is ES_TIMEOUT
Set CurrentState to Writing
Call the FirstPage function to start writing to OLED
call drawNavipower function
Set previous page value to true
initialize timer for refresh, refresh every half sec
Writing:
if ThisEvent is Next Page event
Change CurrentState to NotWriting
If any events have been deferred
Recall deferred events
Reset the counter for deferred events
If any other event is received
Defer the event as OLED is Writing
Increment counter DeferredChar

CheckOLEDBusy
Create a Return value and initialize to false
If CurrentState is Writing
If value of NextPage function is 0 and previous page value is 1
Assign true to return value
Post Next Page event to OLED service
update value for prevWrite
EndIf
EndIf
Return the return value

You might also like