Added Plasma2040 example that displays data from the BME68x

pull/194/head
ZodiusInfuser 2021-08-20 18:17:39 +01:00
rodzic c4ea05a763
commit b72e0a1bb9
4 zmienionych plików z 184 dodań i 1 usunięć

Wyświetl plik

@ -33,7 +33,7 @@ namespace pimoroni {
}
bool BME68X::configure(uint8_t filter, uint8_t odr, uint8_t os_humidity, uint8_t os_pressure, uint8_t os_temp) {
int8_t result;
int8_t result = 0;
conf.filter = filter;
conf.odr = odr;

Wyświetl plik

@ -1,3 +1,4 @@
include(plasma2040_bme68x.cmake)
include(plasma2040_rotary.cmake)
include(plasma2040_rainbow.cmake)
include(plasma2040_stacker.cmake)

Wyświetl plik

@ -0,0 +1,17 @@
set(OUTPUT_NAME plasma2040_bme68x)
add_executable(${OUTPUT_NAME} plasma2040_bme68x.cpp)
target_link_libraries(${OUTPUT_NAME}
pico_stdlib
plasma2040
bme68x
hardware_i2c
pimoroni_i2c
rgbled
button
)
# enable usb output
pico_enable_stdio_usb(${OUTPUT_NAME} 1)
pico_add_extra_outputs(${OUTPUT_NAME})

Wyświetl plik

@ -0,0 +1,165 @@
#include <stdio.h>
#include <math.h>
#include <cstdint>
#include "pico/stdlib.h"
#include "plasma2040.hpp"
#include "common/pimoroni_common.hpp"
#include "bme68x.hpp"
#include "rgbled.hpp"
#include "button.hpp"
using namespace pimoroni;
using namespace plasma;
// Set how many LEDs you have
const uint N_LEDS = 30;
// How many times the LEDs will be updated per second
const uint UPDATES = 60;
constexpr float TEMPERATURE_C_MIN = 20.0f;
constexpr float TEMPERATURE_C_MAX = 35.0f;
constexpr float PRESSURE_PA_MIN = 87000.0f;
constexpr float PRESSURE_PA_MAX = 108500.0f;
constexpr float HUMIDITY_MIN = 0.0f;
constexpr float HUMIDITY_MAX = 100.0f;
// Pick *one* LED type by uncommenting the relevant line below:
// APA102-style LEDs with Data/Clock lines. AKA DotStar
APA102 led_strip(N_LEDS, pio0, 0, plasma2040::DAT, plasma2040::CLK);
// WS28X-style LEDs with a single signal line. AKA NeoPixel
//WS2812 led_strip(N_LEDS, pio0, 0, plasma2040::DAT);
Button button_a(plasma2040::BUTTON_A);
Button button_b(plasma2040::BUTTON_B);
RGBLED led(plasma2040::LED_R, plasma2040::LED_G, plasma2040::LED_B);
I2C i2c(BOARD::PICO_EXPLORER);
BME68X bme68x(&i2c);
enum DisplayMode {
ALL,
TEMPERATURE,
PRESSURE,
HUMIDITY
};
float map(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void colour_gauge(float percent, uint start_led, uint end_led, float start_hue, float end_hue) {
if(end_led > start_led) {
uint range = end_led - start_led;
float light_pixels = percent * range;
for(uint i = 0; i < range; i++) {
float h = map(i, 0.0f, (float)range - 1, start_hue, end_hue);
uint i2 = i + 1;
if(i2 <= light_pixels) {
led_strip.set_hsv(i + start_led, h, 1.0f, 1.0f);
}
else if(i <= light_pixels) {
float scale = map(light_pixels, (float)i, (float)i2, 0.0f, 1.0f);
led_strip.set_hsv(i + start_led, h, 1.0f, scale);
}
else {
led_strip.set_hsv(i + start_led, 0.0f, 0.0f, 0.0f);
}
}
}
}
int main() {
stdio_init_all();
led_strip.start(UPDATES);
bool bme_detected = bme68x.init();
uint first_third = led_strip.num_leds / 3;
uint second_third = (led_strip.num_leds * 2) / 3;
float t = 0.0f;
DisplayMode mode = DisplayMode::ALL;
while(true) {
if(bme_detected) {
bme68x_data data;
auto result = bme68x.read_forced(&data);
(void)result;
printf("%.2f, %.2f, %.2f\n", data.temperature, data.pressure, data.humidity);
switch(mode) {
case DisplayMode::ALL:
t = map(data.temperature, TEMPERATURE_C_MIN, TEMPERATURE_C_MAX, 0.0f, 1.0f);
colour_gauge(t, 0, first_third, 0.667f, 1.0f);
t = map(data.pressure, PRESSURE_PA_MIN, PRESSURE_PA_MAX, 0.0f, 1.0f);
colour_gauge(t, first_third, second_third, 0.333f, 0.0f);
t = map(data.humidity, HUMIDITY_MIN, HUMIDITY_MAX, 0.0f, 1.0f);
colour_gauge(t, second_third, led_strip.num_leds, 0.333f, 0.667f);
break;
case DisplayMode::TEMPERATURE:
t = map(data.temperature, TEMPERATURE_C_MIN, TEMPERATURE_C_MAX, 0.0f, 1.0f);
colour_gauge(t, 0, led_strip.num_leds, 0.667f, 1.0f);
break;
case DisplayMode::PRESSURE:
t = map(data.pressure, PRESSURE_PA_MIN, PRESSURE_PA_MAX, 0.0f, 1.0f);
colour_gauge(t, 0, led_strip.num_leds, 0.333f, 0.0f);
break;
case DisplayMode::HUMIDITY:
t = map(data.humidity, HUMIDITY_MIN, HUMIDITY_MAX, 0.0f, 1.0f);
colour_gauge(t, 0, led_strip.num_leds, 0.333f, 0.667f);
break;
}
}
bool a_pressed = button_a.read();
bool b_pressed = button_b.read();
switch(mode) {
case DisplayMode::ALL:
led.set_rgb(127, 127, 127);
if(a_pressed) mode = DisplayMode::TEMPERATURE;
else if(b_pressed) mode = DisplayMode::HUMIDITY;
break;
case DisplayMode::TEMPERATURE:
led.set_rgb(255, 0, 255);
if(a_pressed) mode = DisplayMode::PRESSURE;
else if(b_pressed) mode = DisplayMode::ALL;
break;
case DisplayMode::PRESSURE:
led.set_rgb(255, 255, 0);
if(a_pressed) mode = DisplayMode::HUMIDITY;
else if(b_pressed) mode = DisplayMode::TEMPERATURE;
break;
case DisplayMode::HUMIDITY:
led.set_rgb(0, 255, 255);
if(a_pressed) mode = DisplayMode::ALL;
else if(b_pressed) mode = DisplayMode::PRESSURE;
break;
}
}
}