diff --git a/Arduino-WDT/Arduino-WDT.ino b/Arduino-WDT/Arduino-WDT.ino new file mode 100644 index 0000000..210fa92 --- /dev/null +++ b/Arduino-WDT/Arduino-WDT.ino @@ -0,0 +1,131 @@ +const int statePin = 3; // Connect LattePanda's S3 pin to Arduino's D3 +const int resetPin = 4; // Connect LattePanda's RST pin to Arduino's D4 +const int D10 = 10; +const int D11 = 11; +const int D12 = 12; + +bool watchdogActive = false; +unsigned long lastReceivedTime; +unsigned long currentTime; + +bool lastLEDState = LOW; +unsigned long lastLEDTime; +unsigned long currentLEDTime; + +void setup() +{ + pinMode(LED_BUILTIN, OUTPUT); + + digitalWrite (LED_BUILTIN, LOW); + lastLEDTime = millis (); + + pinMode(statePin, INPUT); // Set as input to read the state pin's level + pinMode(resetPin, INPUT); // By default, the RST pin on LattePanda is 3.3V. Initialize Arduino pin as INPUT to match this level. + + pinMode(D10, OUTPUT); + pinMode(D11, OUTPUT); + pinMode(D12, OUTPUT); + + Serial.begin(9600); +} + +void loop() +{ + //-- Check to see if it's time to change the LED state. + currentLEDTime = millis (); + if (currentLEDTime > lastLEDTime) //-- Check for millis overflow + { + if (currentLEDTime - lastLEDTime > 1000U) + { + if (lastLEDState == LOW) + { + digitalWrite (LED_BUILTIN, HIGH); + lastLEDState = HIGH; + } + else + { + digitalWrite (LED_BUILTIN, LOW); + lastLEDState = LOW; + } + + lastLEDTime = millis (); + } + } + else + lastLEDTime = currentLEDTime; + + // Check if the watchdog should be activated + if (digitalRead (statePin) == HIGH) + { + if (Serial.available () > 0) + { + char receivedChar = Serial.read (); + if (receivedChar == 'A') + { + watchdogActive = true; + lastReceivedTime = millis(); + } + else if (receivedChar == 'Q') + { + watchdogActive = false; + } + else if (receivedChar == '0') + { + digitalWrite (D10, LOW); + digitalWrite (D11, LOW); + digitalWrite (D12, LOW); + } + else if (receivedChar == '1') + { + digitalWrite (D10, LOW); + digitalWrite (D11, LOW); + digitalWrite (D12, LOW); + + digitalWrite (D10, HIGH); + } + else if (receivedChar == '2') + { + digitalWrite (D10, LOW); + digitalWrite (D11, LOW); + digitalWrite (D12, LOW); + + digitalWrite (D11, HIGH); + } + else if (receivedChar == '3') + { + digitalWrite (D10, LOW); + digitalWrite (D11, LOW); + digitalWrite (D12, LOW); + + digitalWrite (D10, HIGH); + digitalWrite (D11, HIGH); + } + else if (receivedChar == '4') + { + digitalWrite (D10, LOW); + digitalWrite (D11, LOW); + digitalWrite (D12, LOW); + + digitalWrite (D12, HIGH); + } + } + } + else + { + watchdogActive = false; + } + + // If watchdog is active and 's' character isn't received within 1 minute, reboot LattePanda board + currentTime = millis (); + if (currentTime > lastReceivedTime) //-- Check for millis overflow and wait for the next cycle if it has + { + if (watchdogActive && (currentTime - lastReceivedTime > 60000U)) + { + pinMode(resetPin,OUTPUT); // Set pin to OUTPUT mode + digitalWrite(resetPin, LOW); // Pull down the RST pin to reboot the LattePanda board + delay(50); // Maintain a LOW level for 50ms + pinMode(resetPin, INPUT); // Set pin back to INPUT mode to match default 3.3V level of LattePanda's RST pin + watchdogActive = false; // Stop the watchdog function after rebooting the LattePanda board + } + } +} diff --git a/Desktop/QuitWDT.lnk b/Desktop/QuitWDT.lnk new file mode 100644 index 0000000..33d262b Binary files /dev/null and b/Desktop/QuitWDT.lnk differ diff --git a/Desktop/Reboot.lnk b/Desktop/Reboot.lnk new file mode 100644 index 0000000..d0efcfe Binary files /dev/null and b/Desktop/Reboot.lnk differ diff --git a/Desktop/TestWDT.lnk b/Desktop/TestWDT.lnk new file mode 100644 index 0000000..85f0171 Binary files /dev/null and b/Desktop/TestWDT.lnk differ diff --git a/EMSAStartup.py b/EMSAStartup.py new file mode 100644 index 0000000..8f8e0de --- /dev/null +++ b/EMSAStartup.py @@ -0,0 +1,5 @@ +import os +import time + +time.sleep(30) +os.system('"C:\\Program Files\\Erisys\\EMSADiagnosticSoftware\\EMSADiagnosticSoftware.exe"') diff --git a/Feed-WDT.py b/Feed-WDT.py new file mode 100644 index 0000000..8ac74ce --- /dev/null +++ b/Feed-WDT.py @@ -0,0 +1,108 @@ +import serial +import serial.tools.list_ports +import socket +import sys +import time +import traceback +from threading import Timer + +class RepeatedTimer(object): + def __init__(self, interval, function, *args, **kwargs): + self._timer = None + self.interval = interval + self.function = function + self.args = args + self.kwargs = kwargs + self.is_running = False + self.start() + + def _run(self): + self.is_running = False + self.start() + self.function(*self.args, **self.kwargs) + + def start(self): + if not self.is_running: + self._timer = Timer(self.interval, self._run) + self._timer.start() + self.is_running = True + + def stop(self): + self._timer.cancel() + self.is_running = False + +def feedWDT (s): + sendSerChar (s, b'A') + +def sendSerChar (s, c): + print ('Sending ' + str (c)) + s.write(c) + +def find_arduino_leonardo(): + # Scan for available COM ports + ports = serial.tools.list_ports.comports() + for port in ports: + # Check if "LattePanda Leonardo" is in the description of any found port + if "LattePanda Leonardo" in port.description: + return port.device + return None + +def main(): + try: + # Locate the Arduino Leonardo on the LattePanda board + device = find_arduino_leonardo() + + # Exit if the device is not found + if device is None: + print("LattePanda Leonardo device not found. Please check if the device is connected and drivers are installed.") + input("Press any key to exit...") + sys.exit(1) + + # Open a Serial connection to the Arduino Leonardo + ser = serial.Serial(device, 9600) + + # Start a repeating timer to feed the watchdog + rt = RepeatedTimer (30, feedWDT, ser) # it auto-starts, no need of rt.start() + + # Setup a listening socket + s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) + s.bind (('localhost', 9999)) + s.listen (5) + print ('Server is listening') + + while True: + print ('Waiting for connection...') + + c, addr = s.accept () + + print ('Got connection from', addr) + + data = c.recv (1).decode ('utf-8') + if not data: + continue + + print ('From online user: ' + data) + + if (data == 'T'): + break + + b = bytes (data, 'utf-8') + sendSerChar (ser, b) + + c.close() + + if (data == 'Q'): + break + + RepeatedTimer.stop (rt) + + except Exception as e: + # Handle exceptions and print the error message + print("An error occurred:") + print(str(e)) + print(traceback.format_exc()) + input("Press any key to exit...") + +if __name__ == "__main__": + main() + diff --git a/QuitWDT.py b/QuitWDT.py new file mode 100644 index 0000000..cd93d0f --- /dev/null +++ b/QuitWDT.py @@ -0,0 +1,6 @@ +import socket + +s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) +s.connect (('localhost', 9999)) +s.send (b'Q') +s.close \ No newline at end of file diff --git a/Startup/00-Feed-WDT.lnk b/Startup/00-Feed-WDT.lnk new file mode 100644 index 0000000..49a1d12 Binary files /dev/null and b/Startup/00-Feed-WDT.lnk differ diff --git a/Startup/01-SignalVu-PC.lnk b/Startup/01-SignalVu-PC.lnk new file mode 100644 index 0000000..f6f319d Binary files /dev/null and b/Startup/01-SignalVu-PC.lnk differ diff --git a/Startup/02-EMSAStartup.lnk b/Startup/02-EMSAStartup.lnk new file mode 100644 index 0000000..3394e84 Binary files /dev/null and b/Startup/02-EMSAStartup.lnk differ diff --git a/TestWDT.py b/TestWDT.py new file mode 100644 index 0000000..17023c3 --- /dev/null +++ b/TestWDT.py @@ -0,0 +1,6 @@ +import socket + +s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) +s.connect (('localhost', 9999)) +s.send (b'T') +s.close diff --git a/port0.py b/port0.py new file mode 100644 index 0000000..058aa2e --- /dev/null +++ b/port0.py @@ -0,0 +1,6 @@ +import socket + +s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) +s.connect (('localhost', 9999)) +s.send (b'0') +s.close \ No newline at end of file diff --git a/port1.py b/port1.py new file mode 100644 index 0000000..d8d68a7 --- /dev/null +++ b/port1.py @@ -0,0 +1,6 @@ +import socket + +s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) +s.connect (('localhost', 9999)) +s.send (b'1') +s.close \ No newline at end of file diff --git a/port2.py b/port2.py new file mode 100644 index 0000000..a3b6fb9 --- /dev/null +++ b/port2.py @@ -0,0 +1,6 @@ +import socket + +s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) +s.connect (('localhost', 9999)) +s.send (b'2') +s.close \ No newline at end of file diff --git a/port3.py b/port3.py new file mode 100644 index 0000000..99717c4 --- /dev/null +++ b/port3.py @@ -0,0 +1,6 @@ +import socket + +s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) +s.connect (('localhost', 9999)) +s.send (b'3') +s.close \ No newline at end of file diff --git a/port4.py b/port4.py new file mode 100644 index 0000000..49c9fd5 --- /dev/null +++ b/port4.py @@ -0,0 +1,6 @@ +import socket + +s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) +s.connect (('localhost', 9999)) +s.send (b'4') +s.close \ No newline at end of file diff --git a/reboot.py b/reboot.py new file mode 100644 index 0000000..cd3c033 --- /dev/null +++ b/reboot.py @@ -0,0 +1,10 @@ +import socket +import os + +s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) +s.connect (('localhost', 9999)) +s.send (b'Q') +s.close + +print("REBOOTING") +os.system("shutdown -t 0 -r -f")