From 0eae83866b63801c4059e68d1743ca16a7512ccf Mon Sep 17 00:00:00 2001
From: Karol Hennessy <revkarol@gmail.com>
Date: Thu, 30 Apr 2020 17:12:36 +0200
Subject: [PATCH] fix to valve controller for exhale valve

---
 arduino/hev_prototype_v1/src/ValvesController.cpp | 8 +++++++-
 arduino/hev_prototype_v1/src/ValvesController.h   | 3 +--
 arduino/hev_prototype_v1/src/main.cpp             | 6 ++----
 3 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/arduino/hev_prototype_v1/src/ValvesController.cpp b/arduino/hev_prototype_v1/src/ValvesController.cpp
index 380536bd..016b0b27 100644
--- a/arduino/hev_prototype_v1/src/ValvesController.cpp
+++ b/arduino/hev_prototype_v1/src/ValvesController.cpp
@@ -23,6 +23,8 @@ ValvesController::ValvesController()
     _purge.proportional = false;
     _purge.state = VALVE_STATE::CLOSED;
 
+    _pin_to_chan[pin_valve_inhale] = pwm_chan_inhale;
+    _pin_to_chan[pin_valve_exhale] = pwm_chan_exhale;
 }
 
 ValvesController::~ValvesController()
@@ -45,7 +47,11 @@ void ValvesController::setPWMValve(int pin, float frac_open)
 
 #ifdef CHIP_ESP32
     int duty_cycle = calcValveDutyCycle(pwm_resolution, frac_open);
-    int chan = pin_to_chan[pin];
+    int chan = _pin_to_chan[pin];
+    //if (pin == pin_valve_exhale)
+    //    chan = pwm_chan_exhale;
+    //else if (pin == pin_valve_inhale)
+    //    chan = pwm_chan_inhale;
     ledcWrite(chan, duty_cycle);
 #else
     int duty_cycle = pow(2, pwm_resolution) * frac_open;
diff --git a/arduino/hev_prototype_v1/src/ValvesController.h b/arduino/hev_prototype_v1/src/ValvesController.h
index aea18515..3cab0a3c 100644
--- a/arduino/hev_prototype_v1/src/ValvesController.h
+++ b/arduino/hev_prototype_v1/src/ValvesController.h
@@ -15,8 +15,6 @@ enum VALVE_STATE : bool
     OPEN = HIGH
 };
 
-static int pin_to_chan[50];  // too lazy to create a proper hashmap for 2 variables; 50 pins is probably fine
-static int chan_to_pin[50];  
 
 class ValvesController
 {
@@ -38,6 +36,7 @@ private:
     valve _inhale;
     valve _exhale;
     valve _purge;
+    uint8_t _pin_to_chan[50]; 
 
 };
 
diff --git a/arduino/hev_prototype_v1/src/main.cpp b/arduino/hev_prototype_v1/src/main.cpp
index 05e78765..99920f9b 100644
--- a/arduino/hev_prototype_v1/src/main.cpp
+++ b/arduino/hev_prototype_v1/src/main.cpp
@@ -5,6 +5,7 @@
 #endif
 // #include <MemoryFree.h>
 #include <Wire.h>
+#include <SPI.h>
 #include <Adafruit_MCP9808.h>
 #include <INA.h>
 #include "CommsControl.h"
@@ -41,8 +42,6 @@ void setup()
     ledcAttachPin(pin_buzzer, 3);  
     ledcAttachPin(pin_valve_inhale , pwm_chan_inhale);  
     ledcAttachPin(pin_valve_exhale , pwm_chan_exhale);  
-    pin_to_chan[pin_valve_inhale] = pwm_chan_inhale;
-    pin_to_chan[pin_valve_exhale] = pwm_chan_exhale;
 
 // map<int,int> pin_to_chan; // = { pin_valve_inhale : pwm_chan_inhale , pin_valve_exhale : pwm_chan_exhale};
 #else
@@ -54,7 +53,6 @@ void setup()
     pinMode(pin_valve_air_in, OUTPUT);
     pinMode(pin_valve_o2_in, OUTPUT);
     pinMode(pin_valve_purge, OUTPUT);
-    pinMode(pin_valve_atmosphere, OUTPUT);
 
     pinMode(pin_pressure_air_supply, INPUT);
     pinMode(pin_pressure_air_regulated, INPUT);
@@ -72,7 +70,7 @@ void setup()
     pinMode(pin_led_yellow, OUTPUT);
     pinMode(pin_led_red, OUTPUT);
 
-    pinMode(pin_buzzer, OUTPUT);
+    //pinMode(pin_buzzer, OUTPUT);
     pinMode(pin_button_0, INPUT);
 
     comms.beginSerial();
-- 
GitLab