WS2812B Code Cleanup, Configuration via config.h

This commit is contained in:
seiichiro 2021-03-09 17:25:35 +01:00
parent ee120ed342
commit 62e7f0ab54
3 changed files with 50 additions and 44 deletions

View file

@ -13,15 +13,17 @@ platform = atmelmegaavr
board = ATtiny3216 board = ATtiny3216
framework = arduino framework = arduino
# Board Config ## Board Config ##
# You might want to set f_cpu to 5MHz (5000000L) to allow operation at lower Battery Voltage - Use "Burn Fuses" after changing f_cpu
# Be aware that some Functions (like WS2812B LED Support) will not work at 5 HMz
board_build.f_cpu = 8000000L board_build.f_cpu = 8000000L
board_hardware.oscillator = internal board_hardware.oscillator = internal
board_hardware.bod = disabled board_hardware.bod = disabled
# Debug Port Config ## Debug Port Config ##
monitor_speed = 115200 monitor_speed = 115200
# LMIC Config via Build Flags ## LMIC Config via Build Flags ##
build_flags = build_flags =
-D CFG_eu868 -D CFG_eu868
-D CFG_sx1276_radio -D CFG_sx1276_radio
@ -29,7 +31,7 @@ build_flags =
-D DISABLE_BEACONS -D DISABLE_BEACONS
-D ARDUINO_LMIC_PROJECT_CONFIG_H_SUPPRESS -D ARDUINO_LMIC_PROJECT_CONFIG_H_SUPPRESS
# Programmer Config (MicroUPDI) ## Programmer Config (MicroUPDI) ##
upload_port = usb upload_port = usb
upload_protocol = xplainedmini_updi upload_protocol = xplainedmini_updi
upload_flags = upload_flags =
@ -37,5 +39,6 @@ upload_flags =
-P$UPLOAD_PORT -P$UPLOAD_PORT
-c$UPLOAD_PROTOCOL -c$UPLOAD_PROTOCOL
## External Libraries ##
lib_deps = lib_deps =
mcci-catena/MCCI LoRaWAN LMIC library @ ^3.3.0 mcci-catena/MCCI LoRaWAN LMIC library @ ^3.3.0

View file

@ -4,6 +4,15 @@
// ATTNode v3 Onboard LED is on PIN_PA7 // ATTNode v3 Onboard LED is on PIN_PA7
#define LED_PIN PIN_PA7 #define LED_PIN PIN_PA7
// Enable WS2812B RGB LED Support for the CO2 Addon Board
// * First LED shows CO2-Level (green: <1000, yellow: 1000-1800, red: >=1800)
// * Second LED shows LoRa Status (yellow: Joining, green 1s: Joined, green 100ms: Sending, blue 250ms: Received Downlink)
// WS2812B_BRIGHT can be set to the desired brightness value for the LEDs (0=off, 255=brightest)
// Uncomment the 3 following lines to get the default behaviour
// #define WS2812B_PIN PIN_PC1
// #define WS2812B_NUM 2
// #define WS2812B_BRIGHT 32
// Enable Serial Debugging. Parameters for the Serial Port are 115200 // Enable Serial Debugging. Parameters for the Serial Port are 115200
// Please be aware that the SG112A/B CO2 Sensor uses the HW-UART, so // Please be aware that the SG112A/B CO2 Sensor uses the HW-UART, so
// Serial Debug Output is not available with this Sensor. // Serial Debug Output is not available with this Sensor.

View file

@ -31,14 +31,20 @@ void blink(uint8_t num) {
#define BLINK_LED(COUNT) #define BLINK_LED(COUNT)
#endif #endif
// WS2812B // WS2812B RGB LEDs on the CO2 Addon Board
#include <tinyNeoPixel_Static.h> // Defines the Macro Function WS2812B_SETLED so we don't need #ifdefs everywhere
#ifdef WS2812B_PIN
#define WS_NUM_PIXELS 2 #include <tinyNeoPixel_Static.h>
byte pixels[WS2812B_NUM * 3];
byte pixels[WS_NUM_PIXELS * 3]; tinyNeoPixel leds = tinyNeoPixel(WS2812B_NUM, WS2812B_PIN, NEO_GRB, pixels);
tinyNeoPixel leds = tinyNeoPixel(WS_NUM_PIXELS, PIN_PC1, NEO_GRB, pixels); #define WS2812B_SETLED(led,r,g,b) leds.setPixelColor(led,r,g,b); leds.show()
#define WS2812B_BLINK(led,r,g,b,ms) leds.setPixelColor(led,r,g,b); leds.show(); delay(ms); leds.setPixelColor(led,0,0,0); leds.show()
#else
#define WS2812B_SETLED(led,r,g,b)
#define WS2812B_BLINK(led,r,g,b,ms)
#endif
// Create the Sensor Objects
#if defined HAS_NO_SENSOR #if defined HAS_NO_SENSOR
struct lora_data { struct lora_data {
uint8_t bat; uint8_t bat;
@ -83,7 +89,7 @@ const lmic_pinmap lmic_pins = {
// List of unused Pins - will be disabled for Power Saving // List of unused Pins - will be disabled for Power Saving
#if defined DEBUG || defined HAS_SG112A || defined HAS_MHZ19C #if defined DEBUG || defined HAS_SG112A || defined HAS_MHZ19C
const int disabledPins[] = {PIN_PB5, PIN_PB4, PIN_PB1, PIN_PB0, PIN_PC3, PIN_PC2, PIN_PC0}; const int disabledPins[] = {PIN_PB5, PIN_PB4, PIN_PB1, PIN_PB0, PIN_PC3, PIN_PC2, PIN_PC1, PIN_PC0};
#else #else
const int disabledPins[] = {PIN_PB5, PIN_PB4, PIN_PB3, PIN_PB2, PIN_PB1, PIN_PB0, PIN_PC3, PIN_PC2, PIN_PC1, PIN_PC0}; const int disabledPins[] = {PIN_PB5, PIN_PB4, PIN_PB3, PIN_PB2, PIN_PB1, PIN_PB0, PIN_PC3, PIN_PC2, PIN_PC1, PIN_PC0};
#endif #endif
@ -117,21 +123,13 @@ void onEvent(ev_t ev) {
case EV_JOINED: case EV_JOINED:
// Disable LinkCheck // Disable LinkCheck
LMIC_setLinkCheckMode(0); LMIC_setLinkCheckMode(0);
leds.setPixelColor(1, leds.Color(0,127,0)); WS2812B_BLINK(1,0,127,0,1000);
leds.show();
delay(1000);
leds.setPixelColor(1, leds.Color(0,0,0));
leds.show();
DEBUG_PRINTLN("OTAA Join Succeeded"); DEBUG_PRINTLN("OTAA Join Succeeded");
break; break;
case EV_TXCOMPLETE: case EV_TXCOMPLETE:
// Check for Downlink // Check for Downlink
DEBUG_PRINTLN("LoRa Packet Sent"); DEBUG_PRINTLN("LoRa Packet Sent");
leds.setPixelColor(1, leds.Color(0,127,0)); WS2812B_BLINK(1,0,127,0,1000);
leds.show();
delay(100);
leds.setPixelColor(1, leds.Color(0,0,0));
leds.show();
if ((int)LMIC.dataLen == 2) { if ((int)LMIC.dataLen == 2) {
// We got a Packet with the right size - lets assemble it into a uint16_t // We got a Packet with the right size - lets assemble it into a uint16_t
DEBUG_PRINTLN("Received Downlink") DEBUG_PRINTLN("Received Downlink")
@ -140,11 +138,7 @@ void onEvent(ev_t ev) {
DEBUG_PRINTLN(tmpslp); DEBUG_PRINTLN(tmpslp);
sleep_time = tmpslp; sleep_time = tmpslp;
EEPROM.put(ADDR_SLP, tmpslp); EEPROM.put(ADDR_SLP, tmpslp);
leds.setPixelColor(1, leds.Color(0,0,127)); WS2812B_BLINK(1,0,0,127,250);
leds.show();
delay(250);
leds.setPixelColor(1, leds.Color(0,0,0));
leds.show();
} }
// Got to sleep for specified Time // Got to sleep for specified Time
@ -196,25 +190,24 @@ void do_send(osjob_t* j) {
DEBUG_PRINTLN("LoRa-Packet Queued"); DEBUG_PRINTLN("LoRa-Packet Queued");
LMIC_setTxData2(1, (unsigned char *)&data, sizeof(data), 0); LMIC_setTxData2(1, (unsigned char *)&data, sizeof(data), 0);
// ppm level #if defined WS2812B_PIN && (defined HAS_SG112A || defined HAS_MHZ19C)
// CO2 PPM Levels and LED Colors
// < 1000 ppm green // < 1000 ppm green
// < 1800 ppm yellow // < 1800 ppm yellow
// > 1000 ppm red // > 1000 ppm red
if (data.ppm > 0 && data.ppm <= 1000) { if (data.ppm > 0 && data.ppm <= 1000) {
leds.setPixelColor(0, leds.Color(0,127,0)); WS2812B_SETLED(0,0,127,0);
leds.show();
} else if (data.ppm > 1000 && data.ppm <= 1800) { } else if (data.ppm > 1000 && data.ppm <= 1800) {
leds.setPixelColor(0, leds.Color(127,127,0)); WS2812B_SETLED(0,127,127,0);
leds.show();
} else if (data.ppm > 1800) { } else if (data.ppm > 1800) {
leds.setPixelColor(0, leds.Color(127,0,0)); WS2812B_SETLED(0,127,0,0);
leds.show();
} else { } else {
leds.setPixelColor(0, leds.Color(0,0,0)); WS2812B_SETLED(0,0,0,0);
leds.show();
} }
#endif #endif // WS2812B
#endif // #infdef HAS_NO_SENSOR
} }
@ -229,13 +222,15 @@ void setup()
Wire.begin(); Wire.begin();
SPI.begin(); SPI.begin();
pinMode(PIN_PC1, OUTPUT);
leds.setBrightness(64);
// Disable unused Pins (for power saving) // Disable unused Pins (for power saving)
for (int i = 0; i < (sizeof(disabledPins) / sizeof(disabledPins[0])) - 1; i++) for (int i = 0; i < (sizeof(disabledPins) / sizeof(disabledPins[0])) - 1; i++)
pinMode(disabledPins[i], INPUT_PULLUP); pinMode(disabledPins[i], INPUT_PULLUP);
#ifdef WS2812B_PIN
pinMode(WS2812B_PIN, OUTPUT);
leds.setBrightness(WS2812B_BRIGHT);
#endif
// Set RTC // Set RTC
while (RTC.STATUS > 0) {} while (RTC.STATUS > 0) {}
RTC.CLKSEL = RTC_CLKSEL_INT1K_gc; RTC.CLKSEL = RTC_CLKSEL_INT1K_gc;
@ -271,8 +266,7 @@ void setup()
DEBUG_PRINTLN("Setup Finished"); DEBUG_PRINTLN("Setup Finished");
// Schedule First Send (Triggers OTAA Join as well) // Schedule First Send (Triggers OTAA Join as well)
leds.setPixelColor(1, leds.Color(127, 127, 0)); WS2812B_SETLED(1,127,127,0);
leds.show();
do_send(&sendjob); do_send(&sendjob);
} }