From 08cee36f4a1af5ba44f9bcc2fd11106352025c0a Mon Sep 17 00:00:00 2001
From: Charles JAVERLIAT <charles.javerliat@gmail.com>
Date: Sun, 31 Jan 2021 18:22:56 +0100
Subject: [PATCH] =?UTF-8?q?Int=C3=A9gration=20mat=C3=A9rielle=20(par=20Mar?=
 =?UTF-8?q?c=20LGLS)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .vscode/c_cpp_properties.json | 101 +++++++++++++++++++++------
 .vscode/launch.json           |  16 +++--
 platformio.ini                |   5 ++
 src/main.cpp                  | 127 +++++++++++++++++++++++++++++-----
 4 files changed, 202 insertions(+), 47 deletions(-)

diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json
index ce98e89..6630f93 100644
--- a/.vscode/c_cpp_properties.json
+++ b/.vscode/c_cpp_properties.json
@@ -11,14 +11,40 @@
                 "/home/charles/Documents/Associatif/Clubelek/Robotique/Coupe de france/module-tirette/include",
                 "/home/charles/Documents/Associatif/Clubelek/Robotique/Coupe de france/module-tirette/src",
                 "/home/charles/Documents/Associatif/Clubelek/Robotique/Coupe de france/module-tirette/lib/ros_lib",
-                "/home/charles/.platformio/packages/framework-arduino-avr/libraries/SPI/src",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/SPI/src",
                 "/home/charles/Documents/Associatif/Clubelek/Robotique/Coupe de france/module-tirette/lib/LiquidCrystal_I2C-1.1.3",
-                "/home/charles/.platformio/packages/framework-arduino-avr/libraries/Wire/src",
-                "/home/charles/.platformio/packages/framework-arduino-avr/cores/arduino",
-                "/home/charles/.platformio/packages/framework-arduino-avr/variants/standard",
-                "/home/charles/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src",
-                "/home/charles/.platformio/packages/framework-arduino-avr/libraries/HID/src",
-                "/home/charles/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/Wire/src",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/cores/arduino/avr",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/cores/arduino/stm32",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/cores/arduino/stm32/LL",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/cores/arduino/stm32/usb",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/cores/arduino/stm32/OpenAMP",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/cores/arduino/stm32/usb/hid",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/cores/arduino/stm32/usb/cdc",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/system/Drivers/STM32F3xx_HAL_Driver/Inc",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/system/Drivers/STM32F3xx_HAL_Driver/Src",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/system/STM32F3xx",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/system/Middlewares/OpenAMP",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/system/Middlewares/OpenAMP/open-amp/lib/include",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/system/Middlewares/OpenAMP/libmetal/lib/include",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/system/Middlewares/OpenAMP/virtual_driver",
+                "/home/charles/.platformio/packages/framework-cmsis/CMSIS/Core/Include",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/system/Drivers/CMSIS/Device/ST/STM32F3xx/Include",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/system/Drivers/CMSIS/Device/ST/STM32F3xx/Source/Templates/gcc",
+                "/home/charles/.platformio/packages/framework-cmsis/CMSIS/DSP/Include",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/cores/arduino",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/variants/NUCLEO_F303K8",
+                "/home/charles/Documents/Associatif/Clubelek/Robotique/Coupe de france/module-tirette/.pio/libdeps/nucleo_f303k8/LiquidCrystal/src",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/CMSIS_DSP/src",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/EEPROM/src",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/IWatchdog/src",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/Keyboard/src",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/Mouse/src",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/Servo/src",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/SoftwareSerial/src",
+                "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/SrcWrapper/src",
                 "/home/charles/.platformio/packages/tool-unity",
                 ""
             ],
@@ -28,32 +54,63 @@
                     "/home/charles/Documents/Associatif/Clubelek/Robotique/Coupe de france/module-tirette/include",
                     "/home/charles/Documents/Associatif/Clubelek/Robotique/Coupe de france/module-tirette/src",
                     "/home/charles/Documents/Associatif/Clubelek/Robotique/Coupe de france/module-tirette/lib/ros_lib",
-                    "/home/charles/.platformio/packages/framework-arduino-avr/libraries/SPI/src",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/SPI/src",
                     "/home/charles/Documents/Associatif/Clubelek/Robotique/Coupe de france/module-tirette/lib/LiquidCrystal_I2C-1.1.3",
-                    "/home/charles/.platformio/packages/framework-arduino-avr/libraries/Wire/src",
-                    "/home/charles/.platformio/packages/framework-arduino-avr/cores/arduino",
-                    "/home/charles/.platformio/packages/framework-arduino-avr/variants/standard",
-                    "/home/charles/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src",
-                    "/home/charles/.platformio/packages/framework-arduino-avr/libraries/HID/src",
-                    "/home/charles/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/Wire/src",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/cores/arduino/avr",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/cores/arduino/stm32",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/cores/arduino/stm32/LL",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/cores/arduino/stm32/usb",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/cores/arduino/stm32/OpenAMP",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/cores/arduino/stm32/usb/hid",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/cores/arduino/stm32/usb/cdc",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/system/Drivers/STM32F3xx_HAL_Driver/Inc",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/system/Drivers/STM32F3xx_HAL_Driver/Src",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/system/STM32F3xx",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/system/Middlewares/OpenAMP",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/system/Middlewares/OpenAMP/open-amp/lib/include",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/system/Middlewares/OpenAMP/libmetal/lib/include",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/system/Middlewares/OpenAMP/virtual_driver",
+                    "/home/charles/.platformio/packages/framework-cmsis/CMSIS/Core/Include",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/system/Drivers/CMSIS/Device/ST/STM32F3xx/Include",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/system/Drivers/CMSIS/Device/ST/STM32F3xx/Source/Templates/gcc",
+                    "/home/charles/.platformio/packages/framework-cmsis/CMSIS/DSP/Include",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/cores/arduino",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/variants/NUCLEO_F303K8",
+                    "/home/charles/Documents/Associatif/Clubelek/Robotique/Coupe de france/module-tirette/.pio/libdeps/nucleo_f303k8/LiquidCrystal/src",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/CMSIS_DSP/src",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/EEPROM/src",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/IWatchdog/src",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/Keyboard/src",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/Mouse/src",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/Servo/src",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/SoftwareSerial/src",
+                    "/home/charles/.platformio/packages/framework-arduinoststm32/libraries/SrcWrapper/src",
                     "/home/charles/.platformio/packages/tool-unity",
                     ""
                 ]
             },
             "defines": [
-                "PLATFORMIO=50004",
-                "ARDUINO_AVR_UNO",
-                "F_CPU=16000000L",
-                "ARDUINO_ARCH_AVR",
+                "PLATFORMIO=50100",
+                "STM32F303x8",
+                "STM32F3xx",
                 "ARDUINO=10808",
-                "__AVR_ATmega328P__",
+                "ARDUINO_ARCH_STM32",
+                "ARDUINO_NUCLEO_F303K8",
+                "BOARD_NAME=\"NUCLEO_F303K8\"",
+                "HAL_UART_MODULE_ENABLED",
                 ""
             ],
             "cStandard": "c11",
-            "cppStandard": "c++11",
-            "compilerPath": "/home/charles/.platformio/packages/toolchain-atmelavr/bin/avr-gcc",
+            "cppStandard": "c++14",
+            "compilerPath": "/home/charles/.platformio/packages/toolchain-gccarmnoneeabi/bin/arm-none-eabi-gcc",
             "compilerArgs": [
-                "-mmcu=atmega328p",
+                "-mfpu=fpv4-sp-d16",
+                "-mfloat-abi=hard",
+                "-mcpu=cortex-m4",
+                "-mthumb",
                 ""
             ]
         }
diff --git a/.vscode/launch.json b/.vscode/launch.json
index b3441db..1af128f 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -12,12 +12,13 @@
             "type": "platformio-debug",
             "request": "launch",
             "name": "PIO Debug",
-            "executable": "/home/charles/Documents/Associatif/Clubelek/Robotique/Coupe de france/module-tirette/.pio/build/arduino_uno/firmware.elf",
-            "projectEnvName": "arduino_uno",
-            "toolchainBinDir": "/home/charles/.platformio/packages/toolchain-atmelavr/bin",
+            "executable": "/home/charles/Documents/Associatif/Clubelek/Robotique/Coupe de france/module-tirette/.pio/build/nucleo_f303k8/firmware.elf",
+            "projectEnvName": "nucleo_f303k8",
+            "toolchainBinDir": "/home/charles/.platformio/packages/toolchain-gccarmnoneeabi/bin",
+            "svdPath": "/home/charles/.platformio/platforms/ststm32/misc/svd/STM32F30x.svd",
             "preLaunchTask": {
                 "type": "PlatformIO",
-                "task": "Pre-Debug (arduino_uno)"
+                "task": "Pre-Debug (nucleo_f303k8)"
             },
             "internalConsoleOptions": "openOnSessionStart"
         },
@@ -25,9 +26,10 @@
             "type": "platformio-debug",
             "request": "launch",
             "name": "PIO Debug (skip Pre-Debug)",
-            "executable": "/home/charles/Documents/Associatif/Clubelek/Robotique/Coupe de france/module-tirette/.pio/build/arduino_uno/firmware.elf",
-            "projectEnvName": "arduino_uno",
-            "toolchainBinDir": "/home/charles/.platformio/packages/toolchain-atmelavr/bin",
+            "executable": "/home/charles/Documents/Associatif/Clubelek/Robotique/Coupe de france/module-tirette/.pio/build/nucleo_f303k8/firmware.elf",
+            "projectEnvName": "nucleo_f303k8",
+            "toolchainBinDir": "/home/charles/.platformio/packages/toolchain-gccarmnoneeabi/bin",
+            "svdPath": "/home/charles/.platformio/platforms/ststm32/misc/svd/STM32F30x.svd",
             "internalConsoleOptions": "openOnSessionStart"
         }
     ]
diff --git a/platformio.ini b/platformio.ini
index 9976ad8..5a25535 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -16,6 +16,11 @@ platform = atmelavr
 framework = arduino
 board = uno
 
+[env:arduino_nano]
+platform = atmelavr
+framework = arduino
+board = nanoatmega168
+
 [env:nucleo_f303k8]
 platform = ststm32
 framework = arduino
diff --git a/src/main.cpp b/src/main.cpp
index a9e9c5d..1e4bf30 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -4,22 +4,33 @@
 #include <std_msgs/Bool.h>
 #include <std_msgs/Int32.h>
 
-#define TEAM_YELLOW 0x0
-#define TEAM_BLUE 0x1
-
-#define PULL_CORD_IN 0x0
-#define PULL_CORD_OUT 0x1
-
+// Pin declaration
+const int pullCordPin = D8;
+const int switchPin = D9;
+const int buttonPin = D10;
 
 // Current team (either TEAM_YELLOW or TEAM_BLUE)
-bool team = TEAM_YELLOW;
-
-// Pull cord status (either PULL_CORD_IN if the pull cord is still in place, or PULL_CORD_OUT if it has been removed)
-bool pullCordStatus = false;
+bool team;
+bool buttonStatus;
 
 // Last received estimated score
 int estimatedScore = 0;
 
+
+
+/**
+ * Tirette (avec gestion du rebond)
+ */
+
+int pullCordState;             // the current reading from the input pin
+int lastPullCordState = LOW;   // the previous reading from the input pin
+
+unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
+unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
+
+
+
+
 ros::NodeHandle nh;
 
 std_msgs::Bool teamMsg;
@@ -35,11 +46,55 @@ ros::Subscriber<std_msgs::Int32> estimatedScoreSub("estimated_score", &estimated
 
 // LCD screen
 LiquidCrystal_I2C lcd(0x27, 16, 2);
-//LiquidCrystal_I2C lcd(0x3F, 16, 2);
+
+
+void refreshScreen() {
+  lcd.clear();
+
+  lcd.setCursor(0, 0);
+  lcd.print("Score=");
+  lcd.setCursor(6, 0);
+  lcd.print(estimatedScore);
+
+  lcd.setCursor(0, 1);
+
+  if (team) {
+    lcd.print("Team=JAUNE");
+  } else {
+    lcd.print("Team=BLEU");
+  }
+
+  lcd.setCursor(12, 1);
+  
+  if (pullCordState) {
+    lcd.print("GO");
+  } else {
+    lcd.print("STOP");
+  }
+
+  lcd.setCursor(13, 0);
+  
+  if (buttonStatus) {
+    lcd.print("ON");
+  } else {
+    lcd.print("OFF");
+  }
+
+}
 
 void setup()
 {
+  pinMode(pullCordPin, INPUT_PULLUP);
+  pinMode(switchPin, INPUT_PULLUP);
+  pinMode(buttonPin, INPUT_PULLUP);
+
   lcd.init();
+  lcd.backlight();
+
+  team = digitalRead(switchPin);
+  buttonStatus = digitalRead(buttonPin);
+
+  refreshScreen();
 
   nh.initNode();
   nh.advertise(teamStatusPub);
@@ -51,24 +106,60 @@ void setup()
 // Appelé quand le score est mis à jour (message de score reçu sur l'Arduino depuis la raspi)
 void estimatedScoreCallback(const std_msgs::Int32& estimatedScoreMsg) {
   estimatedScore = estimatedScoreMsg.data;
-  // TODO Mettre à jour le score sur l'affichage LCD
-  // led.
+  refreshScreen();
 }
 
 void loop()
 {
-  //TODO lire les status des boutons etc pour mettre à jour les valeurs
 
+  // DEBUT PARTIE TIRETTE
+
+  int pullCordReading = digitalRead(pullCordPin);
+
+  // If the switch changed, due to noise or pressing:
+  if (pullCordReading != lastPullCordState) {
+    // reset the debouncing timer
+    lastDebounceTime = millis();
+  }
+
+  if ((millis() - lastDebounceTime) > debounceDelay) {
+    // whatever the reading is at, it's been there for longer than the debounce
+    // delay, so take it as the actual current state:
+
+    // if the button state has changed:
+    if (pullCordReading != pullCordState) {
+      pullCordState = pullCordReading;
+      refreshScreen();
+    }
+  }
+
+  lastPullCordState = pullCordReading;
+
+
+
+  // FIN PARTIE TIRETTE
+
+
+  if(!digitalRead(buttonPin) != buttonStatus) {
+    // Appui bouton
+    buttonStatus = !digitalRead(buttonPin);
+    refreshScreen();
+  }
+
+  if(digitalRead(switchPin) != team) {
+    //Changement d'équipe
+    team = digitalRead(switchPin);
+    refreshScreen();
+  }
+  
   teamMsg.data = team;
   teamStatusPub.publish(&teamMsg);
 
-  pullCordStatusMsg.data = pullCordStatus;
+  pullCordStatusMsg.data = pullCordState;
   pullCordStatusPub.publish(&pullCordStatusMsg);
 
-  buttonStatusMsg.data = false;
+  buttonStatusMsg.data = buttonStatus;
   buttonStatusPub.publish(&buttonStatusMsg);
 
   nh.spinOnce();
-
-  delay(1);
 }
\ No newline at end of file
-- 
GitLab