From 869ea45c68da648eb2830ba8d44d28053604c83a Mon Sep 17 00:00:00 2001
From: Raphael Nestler <raphael.nestler@gmail.com>
Date: Fri, 23 Aug 2019 12:40:10 +0200
Subject: [PATCH] Allow switching between sensors

---
 sensor-plot/src/main.rs | 65 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 59 insertions(+), 6 deletions(-)

diff --git a/sensor-plot/src/main.rs b/sensor-plot/src/main.rs
index 993aa53..ca9969a 100644
--- a/sensor-plot/src/main.rs
+++ b/sensor-plot/src/main.rs
@@ -34,6 +34,10 @@ impl PlotBuffer {
         }
     }
 
+    pub fn clear(&mut self) {
+        *self = PlotBuffer::new();
+    }
+
     pub fn plot(&self, display: &Display) {
         let min = self.get_min() as f32;
         let max = self.get_max() as f32;
@@ -57,23 +61,72 @@ impl PlotBuffer {
     }
 }
 
+#[derive(Clone, Copy)]
+enum Sensor {
+    Temperature,
+    Humidity,
+    Pressure,
+    GasResistance,
+}
+
+impl Sensor {
+    pub fn new() -> Self {
+        Sensor::Temperature
+    }
+
+    pub fn left(self) -> Self {
+        match self {
+            Self::Temperature => Self::Humidity,
+            Self::Humidity => Self::Pressure,
+            Self::Pressure => Self::GasResistance,
+            Self::GasResistance => Self::Temperature,
+        }
+    }
+
+    pub fn right(self) -> Self {
+        match self {
+            Self::Temperature => Self::GasResistance,
+            Self::Humidity => Self::Temperature,
+            Self::Pressure => Self::Humidity,
+            Self::GasResistance => Self::Pressure,
+        }
+    }
+}
+
 main!(main);
 fn main() {
     writeln!(UART, "Hello from Rust\r").unwrap();
 
-    let mut light_plot = PlotBuffer::new();
+    let mut plot_buffer = PlotBuffer::new();
 
     let environment_sensors = BME680::start();
     let display = Display::open();
+    let mut sensor = Sensor::new();
+    let mut old_b = Buttons::read();
 
     loop {
-        let sample = environment_sensors
-            .read()
-            .map_or(0, |v| (v.humidity * 1000.0) as i32);
-        light_plot.add_sample(sample);
+        let b = Buttons::read();
+
+        if b.left_bottom() && !old_b.left_bottom() {
+            sensor = sensor.left();
+            plot_buffer.clear();
+        }
+        if b.right_bottom() && !old_b.right_bottom() {
+            sensor = sensor.right();
+            plot_buffer.clear();
+        }
+        old_b = b;
+
+        let sample = environment_sensors.read().map_or(0, |v| match sensor {
+            Sensor::Temperature => (v.temperature * 1000.0) as i32,
+            Sensor::Humidity => (v.humidity * 1000.0) as i32,
+            Sensor::Pressure => (v.pressure * 1000.0) as i32,
+            Sensor::GasResistance => (v.gas_resistance * 1000.0) as i32,
+        });
+        plot_buffer.add_sample(sample);
 
         display.clear(Color::black());
-        light_plot.plot(&display);
+        plot_buffer.plot(&display);
         display.update();
     }
 }
-- 
GitLab