Skip to content

Commit

Permalink
TFT display final#6 buttons #286
Browse files Browse the repository at this point in the history
  • Loading branch information
WouterJD committed Apr 18, 2021
1 parent 8b0705d commit c0d3a6f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 20 deletions.
2 changes: 1 addition & 1 deletion StartUp/FortiusAnt (gui, autostart).bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
..\pythoncode\FortiusAnt.py -g -a -A -H0 -l
..\pythoncode\FortiusAnt.py -g -a -A -H0
pause
42 changes: 36 additions & 6 deletions pythoncode/FortiusAntBody.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#-------------------------------------------------------------------------------
# Version info
#-------------------------------------------------------------------------------
__version__ = "2021-03-22"
__version__ = "2021-04-15"
# 2021-04-15 Messages were flushed in AntDongle.Write() which is resolved
# in our loop there is only one place where ANT-messages are
# received: AntDongle.Write(msg, True, False)
# 2021-04-15 Raspberry display integrated, buttons can control loop
# 2021-03-22 StatusLeds also on GUI
# 2021-03-03 AntDongle.ConfigMsg set to False after first cycle.
# This avoids repeated sets of messages.
Expand Down Expand Up @@ -314,8 +318,7 @@ def Terminate():
# ------------------------------------------------------------------------------
def IdleFunction(FortiusAntGui):
global TacxTrainer, rpi
rtn = 0
TacxTrainer.tacxEvent = False
rtn = 0
if TacxTrainer and TacxTrainer.OK:
FortiusAntGui.SetLeds(False, False, TacxTrainer.PedalEcho == 1, None, TacxTrainer.tacxEvent)
rpi.SetLeds (False, False, TacxTrainer.PedalEcho == 1, None, TacxTrainer.tacxEvent)
Expand Down Expand Up @@ -459,6 +462,7 @@ def Runoff(FortiusAntGui):
# Initialize
#---------------------------------------------------------------------------
TacxTrainer.SetPower(clv.RunoffPower)
rpi.DisplayState(constants.faTrainer)
rolldown = False
rolldown_time = 0
#ShortMessage = TacxTrainer.Message + " | Runoff - "
Expand Down Expand Up @@ -488,6 +492,16 @@ def Runoff(FortiusAntGui):
#-----------------------------------------------------------------------
TacxTrainer.Refresh(True, usbTrainer.modeResistance) # This cannot be an ANT trainer

FortiusAntGui.SetLeds(False, False, TacxTrainer.PedalEcho == 1, None, TacxTrainer.tacxEvent)
rpi.SetLeds (False, False, TacxTrainer.PedalEcho == 1, None, TacxTrainer.tacxEvent)
if rpi.CheckShutdown(FortiusAntGui): FortiusAntGui.RunningSwitch = False

if rpi.buttonUp and rpi.buttonDown: pass # No cancel action
elif rpi.buttonUp: TacxTrainer.Buttons = usbTrainer.UpButton
elif rpi.buttonDown: TacxTrainer.Buttons = usbTrainer.DownButton
rpi.buttonUp = False
rpi.buttonDown = False

#-----------------------------------------------------------------------
# Show what happens
#-----------------------------------------------------------------------
Expand Down Expand Up @@ -721,7 +735,7 @@ def Tacx2DongleSub(FortiusAntGui, Restart):
# No pairing-loop: HRM perhaps not yet active and avoid delay
#-------------------------------------------------------------------
# msg = ant.msg4D_RequestMessage(ant.channel_HRM_s, ant.msgID_ChannelID)
# AntDongle.Write([msg], False, False)
# AntDongle.Write([msg], False)

if clv.Tacx_Vortex:
#-------------------------------------------------------------------
Expand All @@ -731,7 +745,7 @@ def Tacx2DongleSub(FortiusAntGui, Restart):
AntDongle.SlaveVTX_ChannelConfig(0)

# msg = ant.msg4D_RequestMessage(ant.channel_VTX_s, ant.msgID_ChannelID)
# AntDongle.Write([msg], False, False)
# AntDongle.Write([msg], False)

#-------------------------------------------------------------------
# Create ANT slave channel for VHU
Expand Down Expand Up @@ -840,10 +854,15 @@ def Tacx2DongleSub(FortiusAntGui, Restart):
#-------------------------------------------------------------------
TacxTrainer.tacxEvent = False
TacxTrainer.Refresh(True, usbTrainer.modeCalibrate)

FortiusAntGui.SetLeds(antEvent, bleEvent, pedalEvent, None, TacxTrainer.tacxEvent)
rpi.SetLeds (antEvent, bleEvent, pedalEvent, None, TacxTrainer.tacxEvent)
if rpi.CheckShutdown(FortiusAntGui): FortiusAntGui.RunningSwitch = False

if rpi.buttonUp and rpi.buttonDown: TacxTrainer.Buttons = usbTrainer.CancelButton
rpi.buttonUp = False
rpi.buttonDown = False

#-------------------------------------------------------------------
# When calibration IS supported, the following condition will NOT occur.
# iFlow 1932 is expected to support calibration but does not.
Expand Down Expand Up @@ -1026,15 +1045,26 @@ def Tacx2DongleSub(FortiusAntGui, Restart):

#-------------------------------------------------------------------
# Raspberry PI leds
# here:
# - we set the leds, and analogously GUI-leds are added
# - we check whether both bottons are pressed to shutdown
# - we check that a single button is pressed, to emulate UP/DOWN
#-------------------------------------------------------------------
if QuarterSecond:
FortiusAntGui.SetLeds(antEvent, bleEvent, pedalEvent, None, TacxTrainer.tacxEvent)
rpi.SetLeds (antEvent, bleEvent, pedalEvent, None, TacxTrainer.tacxEvent)
if rpi.CheckShutdown(FortiusAntGui): FortiusAntGui.RunningSwitch = False

if rpi.buttonUp and rpi.buttonDown: pass # No cancel action
elif rpi.buttonUp: TacxTrainer.Buttons = usbTrainer.UpButton
elif rpi.buttonDown: TacxTrainer.Buttons = usbTrainer.DownButton

bleEvent = False
antEvent = False
pedalEvent = False
TacxTrainer.tacxEvent = False
rpi.buttonUp = False
rpi.buttonDown = False

#-------------------------------------------------------------------
# If NO Speed Cadence Sensor defined, use Trainer-info
Expand Down Expand Up @@ -1616,7 +1646,7 @@ def Tacx2DongleSub(FortiusAntGui, Restart):
#-------------------------------------------------------
if not AntHRMpaired:
msg = ant.msg4D_RequestMessage(ant.channel_HRM_s, ant.msgID_ChannelID)
AntDongle.Write([msg], False, False)
AntDongle.Write([msg], False)

#-------------------------------------------------------
# Data page 0...4 HRM data
Expand Down
9 changes: 6 additions & 3 deletions pythoncode/antDongle.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#---------------------------------------------------------------------------
# Version info
#---------------------------------------------------------------------------
__version__ = "2021-04-01"
__version__ = "2021-04-15"
# 2021-04-15 flush improved, #286
# 2021-04-01 DongleReconnected WAS initially True but should be False
# (Although the field should only be used when AntDongle.OK = True)
# 2021-03-03 Message in ...Config() function only given if self.ConfigMsg:
Expand Down Expand Up @@ -550,6 +551,8 @@ def __GetDongle(self):
# filled ANT-dongle (device driver) because many
# messages are waiting to be processed.
# Default = True, which is safe behaviour
# 2021-04-15 initial flush only done when data is
# to be received, messages were lost!
#
# function write all strings to antDongle
# read responses from antDongle
Expand All @@ -564,7 +567,7 @@ def Write(self, messages, receive=True, drop=True, flush=True):
# able to write (if too many messages pending: Write exception,
# message lost)
#---------------------------------------------------------------
if flush:
if receive and flush:
rtn = self.Read(drop) # Flush -> default timeout = proven!

for message in messages:
Expand Down Expand Up @@ -815,7 +818,7 @@ def SlavePair_ChannelConfig(self, channel_pair, \
msg60_ChannelTransmitPower (channel_pair, TransmitPower_0dBm),
msg4B_OpenChannel (channel_pair)
]
self.Write(messages, True, False)
self.Write(messages) # 2021-04-15 ", True, False" removed because it's inconsistent

def Trainer_ChannelConfig(self):
if self.OK:
Expand Down
39 changes: 29 additions & 10 deletions pythoncode/raspberry.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ class clsRaspberry:

buttonA = None
buttonB = None
buttonUp = False # button was pressed, must be reset by user
buttonDown = False # same

ButtonDefaultValue = True # The value when the button is NOT pressed

def __init__(self, clv):
global MySelf
Expand Down Expand Up @@ -367,7 +371,9 @@ def PowerupTest(self):
# Returns True when button pressed firmly
# --------------------------------------------------------------------------
def CheckShutdown(self, FortiusAntGui=None):
repeat = 5 # timeout = n * .25 seconds # 5 blinks is enough
repeat = 7 # timeout = n * .25 seconds # 5 blinks is enough
# 7 because the first two cycles are blink to allow
# for a "short button press" without messages.
rtn = True # Assume button will remain pressed
# If we don't use leds/buttons ==> False
ResetLeds= False
Expand All @@ -382,17 +388,18 @@ def CheckShutdown(self, FortiusAntGui=None):
# Blink the (red) Shutdown led while button pressed
# ------------------------------------------------------------------
while repeat:
repeat -= 1
if self.ShutdownButtonIsHeld():
self.SetLeds (False, False, False, True, False)
if FortiusAntGui != None:
FortiusAntGui.SetLeds(False, False, False, True, False)
if repeat <= 5:
self.SetLeds (False, False, False, True, False)
if FortiusAntGui != None:
FortiusAntGui.SetLeds(False, False, False, True, False)
logfile.Console('Raspberry will be shutdown ... %s ' % repeat)
ResetLeds = True
time.sleep(.25)
logfile.Console('Raspberry will be shutdown ... %s ' % repeat)
else:
rtn = False
break
repeat -= 1

# ------------------------------------------------------------------
# Final warning
Expand Down Expand Up @@ -434,8 +441,8 @@ def ShutdownButtonIsHeld(self):
# ----------------------------------------------------------------------
if self.buttonA != None \
and self.buttonB != None \
and self.buttonA.value == False \
and self.buttonB.value == False: return True
and self.buttonA.value != self.ButtonDefaultValue \
and self.buttonB.value != self.ButtonDefaultValue: return True
return False

# --------------------------------------------------------------------------
Expand Down Expand Up @@ -498,6 +505,7 @@ def _SetupDisplaySt7789(self):
self.buttonA.switch_to_input()
self.buttonB.switch_to_input()

self.ButtonDefaultValue = self.buttonA.value
# ------------------------------------------------------------------
# Startup image is in directory of the .py [or embedded in .exe]
# ------------------------------------------------------------------
Expand Down Expand Up @@ -681,6 +689,17 @@ def _DrawLedsSt7789(self):
# ----------------------------------------------------------------------
self.st7789.image(self.image, self.rotation)

# ----------------------------------------------------------------------
# Translate buttons on the ST7789 display to Up/DOWN
# Rotation-dependant: Left/Bottom = Down, Right/Top = Up
# ----------------------------------------------------------------------
if self.rotation in (180,270):
if self.buttonA.value != self.ButtonDefaultValue: self.buttonUp = True
if self.buttonB.value != self.ButtonDefaultValue: self.buttonDown = True
else:
if self.buttonA.value != self.ButtonDefaultValue: self.buttonDown = True
if self.buttonB.value != self.ButtonDefaultValue: self.buttonUp = True

# --------------------------------------------------------------------------
# [ O U T P U T ] D i s p l a y S t a t e - implementations for the -L displays
# --------------------------------------------------------------------------
Expand Down Expand Up @@ -747,8 +766,8 @@ def _DisplayStateSt7789(self, FortiusAntState=None):
[ 'Give pedal kick', c2 ],\
[ 'Calibrating...', c3 ],\
[ 'Activate ' + device, c4 ],\
[ 'Ready to Zwift', c5 ],\
[ 'Bridge stopped', c6 ],\
[ 'Ready for training', c5 ],\
[ 'Trainer stopped', c6 ],\
[ device + ' stopped', c7 ],\
[ 'FortiusAnt stopped', c8 ]])

Expand Down

0 comments on commit c0d3a6f

Please sign in to comment.