Pārlūkot izejas kodu

Working on two boards.

Fred Damstra 6 gadi atpakaļ
vecāks
revīzija
90e8617d53
5 mainītis faili ar 54 papildinājumiem un 19 dzēšanām
  1. 11 7
      CRV2.ino
  2. 4 2
      crv2_esp_now.cpp
  3. 22 4
      led_ring.cpp
  4. 14 5
      led_ring.h
  5. 3 1
      neighbors.cpp

+ 11 - 7
CRV2.ino

@@ -9,7 +9,7 @@
 #include "credentials.h"
 #include "neighbors.h"
 #include "ota.h"
-#include "lcd.h"
+#include "led_ring.h"
 
 
 Neighbor_List NList;
@@ -20,10 +20,11 @@ void setup() {
   Serial.println("Here's what I know about myself:");
   Serial.print("  ESP_NOW_MAX_DATA_LEN = "); Serial.println(ESP_NOW_MAX_DATA_LEN);
 
-  // Initialize LCDs first to turn of pwm
-  Serial.println("Initializing LCDs...");
-  lcd_setup();
-
+  // Initialize leds first to turn of pwm
+  Serial.println("Initializing LEDs...");
+  led_setup();
+  led_setall(255, 0, 0); // Start at red
+  
   Serial.println("Initializing Buzzer...");
   buzzer_setup();  
   beep(); delay(200); beep(); delay(200); beep(); delay(200);
@@ -54,6 +55,7 @@ void setup() {
 
   // Initializing broadcast
   initBroadcast();
+  led_setall(0, 0, 255); // Blue after initialization
 }
 
 void loop() {
@@ -72,12 +74,14 @@ void loop() {
   }
   
   if(NList.ready()) {
-   
+    led_setall(0, 255, 0); // Green if we're good   
+  } else {
+    led_setall(0, 0, 255); // Blue for determining
   }
 
   //buzz(); delay(200);
   //buzz(); delay(200);
   //buzz(); delay(200);
 
-  //lcd_examples(); // testing
+  //led_examples(); // testing
 }

+ 4 - 2
crv2_esp_now.cpp

@@ -22,7 +22,8 @@ bool send_encapsulated(const uint8_t type, const uint8_t *data, const size_t len
   buffer[4] = PROTOCOL_VERSION;
   buffer[5] = type;
   buffer[6] = len;
-  memcpy(buffer+6, data, len);
+  //Serial.print("send_encapsulated len = "); Serial.println(buffer[6]);
+  memcpy(buffer+7, data, len);
   return send_broadcast(buffer, len + 7);
 }
 
@@ -41,10 +42,11 @@ void process_message(receive_buffer_t& incoming) {
   }
   uint8_t type = buffer[5];
   uint8_t length = buffer[6];
+  //Serial.print("Processing message; Type = "); Serial.print(type); Serial.print("; Length = "); Serial.println(length);
   switch(type) {
     case 1:
       //Serial.print("Processing neighbor discovery from peer "); Serial.println((char *)incoming.mac_addr);
-      NList.process_discovery(incoming.mac_addr, incoming.data+6, length);
+      NList.process_discovery(incoming.mac_addr, incoming.data+7, length);
       break;
     default:
       Serial.print("ERROR: Unsupported transmission type "); 

+ 22 - 4
lcd.cpp → led_ring.cpp

@@ -1,4 +1,4 @@
-#include "lcd.h"
+#include "led_ring.h"
 
 #include <Adafruit_NeoPixel.h>
 #ifdef __AVR__
@@ -13,14 +13,18 @@
 //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
 //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
 //   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
-Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, LCD_PIN, NEO_GRB + NEO_KHZ800);
+Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, LED_PIN, NEO_GRB + NEO_KHZ800);
 
 // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
 // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
 // and minimize distance between Arduino and first pixel.  Avoid connecting
 // on a live circuit...if you must, connect GND first.
 
-void lcd_setup() {
+uint32_t red = strip.Color(255, 0, 0);
+uint32_t green = strip.Color(0, 255, 0);
+uint32_t blue = strip.Color(0, 0, 255);
+
+void led_setup() {
   // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
   #if defined (__AVR_ATtiny85__)
     if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
@@ -29,10 +33,24 @@ void lcd_setup() {
 
   strip.begin();
   strip.setBrightness(100);
+  delay(1); // Annoying flickering issues due to timing. I hate to use delay().
   strip.show(); // Initialize all pixels to 'off'
 }
 
-void lcd_examples() {
+void led_setall(uint32_t color) {
+  for(uint16_t i=0; i<strip.numPixels(); i++) {
+    strip.setPixelColor(i, color); 
+  }
+  delay(1);
+  strip.show();
+}
+
+void led_setall(uint8_t red, uint8_t green, uint8_t blue) {
+  led_setall(strip.Color(red, green, blue));
+}
+
+
+void led_examples() {
   // Some example procedures showing how to display to the pixels:
   colorWipe(strip.Color(255, 0, 0), 50); // Red
   colorWipe(strip.Color(0, 255, 0), 50); // Green

+ 14 - 5
lcd.h → led_ring.h

@@ -1,12 +1,21 @@
-#ifndef LCD_H
-#define LCD_H
+#ifndef LED_RING_H
+#define LED_RING_H
 
 #include <Arduino.h>
+#include <Adafruit_NeoPixel.h>
+#ifdef __AVR__
+  #include <avr/power.h>
+#endif
+
+extern Adafruit_NeoPixel strip;
+
+#define LED_PIN 5
 
-#define LCD_PIN 5
+void led_setup();
+void led_setall(uint32_t color);
+void led_setall(uint8_t red, uint8_t green, uint8_t blue);
 
-void lcd_setup();
-void lcd_examples();
+void led_examples();
 
 void colorWipe(uint32_t c, uint8_t wait); // Fill the dots one after the other with a color
 void rainbow(uint8_t wait);

+ 3 - 1
neighbors.cpp

@@ -89,7 +89,8 @@ bool Neighbor_List::process_discovery(const uint8_t neighbor[19], const uint8_t
   for (int i = 0; i < neighbors_len - 1 && i < MAX_NEIGHBORS; i++) {
     if (list[i].id != neighbors_list[i]) {
       Serial.print("List mismatch with neighbor "); Serial.print((char *)neighbor); Serial.print("; Index = "); Serial.println(i);
-      Serial.print("Mine: "); print_id(list[i].id); Serial.print("; Theirs: "); print_id(neighbors_list[i]); Serial.println("");
+      //Serial.print("Mine: "); print_id(list[i].id); Serial.print("; Theirs: "); print_id(neighbors_list[i]); Serial.println("");
+      //Serial.print("Neighbors Length: "); Serial.print(neighbors_len); Serial.print("; Byte length: "); Serial.print(len); Serial.print("; Size of 64: "); Serial.println(sizeof(uint64_t));
       this->set_agreement(neighbor_id, false);
       return false;
     }
@@ -117,6 +118,7 @@ void Neighbor_List::beacon(void) {
       }
     }
     raw_ids[i] = 0xFFFFFFFFFFFFFFFF; /* Last ID is not zero */
+    //Serial.print("Sending list. Num neighbors: "); Serial.print(num_neighbors); Serial.print("; Size: "); Serial.println(sizeof(uint64_t) * (i + 1));
 
     if (!send_encapsulated(1, (uint8_t *)raw_ids, sizeof(uint64_t) * (i + 1))) {
       Serial.println("ERROR Sending Encapsulated!");