#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Test du port série
# Modifié pour simuler la réponse d'une carte F6KBF (le caractère d'acquitement est 'A' (65 décimal)
#
# initialisation d'AD et DEC au valeurs des codeurs en position PARKING au lancement
#
# 12-nov-2022 Version modifiée pour Python 3.9.2 sur simulator.f4klo.ampr.org (serialPort.write format byte)
# 22-nov-2022 Suppression de tous les chr() et utilisation de pack.struct pour forcer écriture d'octets sur le port série
#						https://docs.python.org/3/library/struct.html
# 					struct Interpret bytes as packed binary data
#						Ligne 60 Track était remis à 0 invalidant la commande 7 de tracking !
#						Il existait deux "if command = Track " !

# TODO : erreurs avec commandes raquette E / W
#        erreur de communication à l'arrivée sur la cible.
#
import serial
import struct

port = "/dev/ttyAMA0"

Clock         =  150
LSB_Azim  =  160
MSB_Azim =  128
LSB_Elev    =  224
MSB_Elev =  192
SignalIn =  149
Stop_Az  =  0
PV_Plus  =  1
PV_Moins=  2
GV_Plus =  5
GV_Moins=  6
Track=  7
Affichage=  20
Extinction= 21
Stop_El  =  16
Dec_plus =  18
Dec_moins =  17
Null=  0
Pre1_Off =  11
Pre1_On  =  10
Pre2_Off =  13
Pre2_On  =  12
PWR_ON   =  8
PWR_OFF  =  9

def repondre (code):
		reponse = struct.pack('B',code)
		bytes_sent = serialPort.write(reponse)
		if bytes_sent != 1 :
			print ("Erreur à l'émission sur port série serialPort() : ", reponse)
		return bytes_sent

AD = 0		# Initialisation en position de PARKING
DEC = 556	# Initialisation en position de PARKING 
Off_AD = 0		# OFFSET des codeurs optiques en AD
Off_DEC = 0	# OFFSET des codeurs optiques en DEC
Depl_AD = 0
Depl_Dec = 0
Tracking = 0   # c'était Track !
cpt_trk = 0
max_trk = 23

Commandes_reconnues = [Clock,LSB_Azim,MSB_Azim,LSB_Elev,MSB_Elev,SignalIn,Stop_Az,PV_Plus,PV_Moins,GV_Plus,GV_Moins,Track,Stop_El,Dec_plus,Dec_moins,Pre1_On,Pre1_Off,Pre2_Off,Pre2_On,PWR_ON,PWR_OFF,Affichage,Extinction]

serialPort = serial.Serial(port, 9600, timeout = 2)

print ("Port Série ", port, " ouvert pour le test :")

while(1) :
	reception = serialPort.read(1)
	if len(reception) == 1 :
		commande =  (reception[0])
		print ("Commande reçue     : ", reception[0])
		if commande == LSB_Azim :
			repondre( (AD % 256))
			print ("LSB_Azim = ", AD % 256)
		if commande == MSB_Azim :
			repondre( ((AD // 256) % 256))
			print ("MSB_Azim = ", AD // 256)
		if commande == LSB_Elev :
			repondre( (DEC % 256))
			print("LSB_Elev = ", DEC % 256 )
		if commande == MSB_Elev :
			repondre( ((DEC // 256) & 255))
			print ("MSB_Elev = ", DEC // 256)
#  (65) est le caractère 'A' d'acquittement
		if commande == SignalIn :
			repondre (65)
			print ("SignalIn")
		if commande == Clock :
			repondre( (65))
			print ("Clock")
			AD += Depl_AD
			ADv = AD - Off_AD
			DEC += Depl_Dec
			DECv = DEC + Off_DEC
			print ("Ascension Droite = ", ADv, "Declinaison = ", DECv)
		if commande == Track :
			cpt_trk += 1
		if cpt_trk >= max_trk :
			cpt_trk = 0
			AD += 1
			repondre( (65))
			print("Track")
		if commande == Track :
			Tracking = 1
			Depl_AD = 0
			repondre ( (65))
			print ("Poursuite")
		if commande == Stop_Az :
			Depl_AD = 0
			Tracking = 0
			repondre( (65))
			print("Stop_Az")
		if commande == PV_Plus :
			Depl_AD = 1
			Tracking = 0
			repondre( (65))
			print ("Petite Vitesse Plus")
		if commande == PV_Moins :
			Depl_AD = -1
			Tracking = 0
			repondre( (65))
			print ("Petite Vitesse Moins")
		if commande == GV_Plus :
			Tracking = 0
			Depl_AD = 5
			repondre( (65))
			print ("Grande Vitesse Plus")
		if commande == GV_Moins :
			Tracking = 0
			Depl_AD = -5
			repondre( (65))
			print ("Grande Vitesse Moins")
		if commande == Affichage :
			repondre( (65))
#			print("Affichage KBF ON") 
		if commande == Extinction :
			repondre( (65))
#			print("Affichage KBF OFF")
		if commande == Stop_El :
			Depl_Dec = 0
			repondre( (65))
			print("Stop Elevation")
		if commande == Dec_plus :
			Depl_Dec = 1
			repondre( (65))
			print ("Dec_plus")
		if commande == Dec_moins :
			Depl_Dec = -1
			repondre( (65))
			print ("Dec_moins")
		if commande == Pre1_Off :
			repondre( (65))
#			print ("Preamp1 OFF")
		if commande == Pre1_On :
			repondre( (65))
#			print ("Preamp1 ON")
		if commande == Pre2_Off :
			repondre( (65))
#			print ("Preamp2 OFF")
		if commande == Pre2_On :
			repondre( (65))
#			print ("Preamp2 ON")
		if commande == PWR_ON :
			repondre( (65))
			print ("Puissance moteur en marche")
		if commande == PWR_OFF :
			repondre( (65))
			print ("Arrêt de la puissance moteur")
		if not (commande in Commandes_reconnues) : 
			repondre(65)
			print ("Commande non reconnue : ", commande , " \n")
serialPort.close()

