Selaa lähdekoodia

initial project setup for Arduino

sfreundel 4 vuotta sitten
vanhempi
sitoutus
7bd2725115
6 muutettua tiedostoa jossa 536 lisäystä ja 0 poistoa
  1. 2 0
      .gitignore
  2. 39 0
      include/README
  3. 46 0
      lib/README
  4. 21 0
      platformio.ini
  5. 417 0
      src/main.cpp
  6. 11 0
      test/README

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+.pio
+.vscode

+ 39 - 0
include/README

@@ -0,0 +1,39 @@
+
+This directory is intended for project header files.
+
+A header file is a file containing C declarations and macro definitions
+to be shared between several project source files. You request the use of a
+header file in your project source file (C, C++, etc) located in `src` folder
+by including it, with the C preprocessing directive `#include'.
+
+```src/main.c
+
+#include "header.h"
+
+int main (void)
+{
+ ...
+}
+```
+
+Including a header file produces the same results as copying the header file
+into each source file that needs it. Such copying would be time-consuming
+and error-prone. With a header file, the related declarations appear
+in only one place. If they need to be changed, they can be changed in one
+place, and programs that include the header file will automatically use the
+new version when next recompiled. The header file eliminates the labor of
+finding and changing all the copies as well as the risk that a failure to
+find one copy will result in inconsistencies within a program.
+
+In C, the usual convention is to give header files names that end with `.h'.
+It is most portable to use only letters, digits, dashes, and underscores in
+header file names, and at most one dot.
+
+Read more about using header files in official GCC documentation:
+
+* Include Syntax
+* Include Operation
+* Once-Only Headers
+* Computed Includes
+
+https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

+ 46 - 0
lib/README

@@ -0,0 +1,46 @@
+
+This directory is intended for project specific (private) libraries.
+PlatformIO will compile them to static libraries and link into executable file.
+
+The source code of each library should be placed in a an own separate directory
+("lib/your_library_name/[here are source files]").
+
+For example, see a structure of the following two libraries `Foo` and `Bar`:
+
+|--lib
+|  |
+|  |--Bar
+|  |  |--docs
+|  |  |--examples
+|  |  |--src
+|  |     |- Bar.c
+|  |     |- Bar.h
+|  |  |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
+|  |
+|  |--Foo
+|  |  |- Foo.c
+|  |  |- Foo.h
+|  |
+|  |- README --> THIS FILE
+|
+|- platformio.ini
+|--src
+   |- main.c
+
+and a contents of `src/main.c`:
+```
+#include <Foo.h>
+#include <Bar.h>
+
+int main (void)
+{
+  ...
+}
+
+```
+
+PlatformIO Library Dependency Finder will find automatically dependent
+libraries scanning project source files.
+
+More information about PlatformIO Library Dependency Finder
+- https://docs.platformio.org/page/librarymanager/ldf.html

+ 21 - 0
platformio.ini

@@ -0,0 +1,21 @@
+; PlatformIO Project Configuration File
+;
+;   Build options: build flags, source filter
+;   Upload options: custom upload port, speed and extra flags
+;   Library options: dependencies, extra library storages
+;   Advanced options: extra scripting
+;
+; Please visit documentation for the other options and examples
+; https://docs.platformio.org/page/projectconf.html
+
+[env:env1]
+platform = atmelavr
+board = nanoatmega328
+framework = arduino
+upload_port = /dev/ttyUSB*
+monitor_speed = 115200
+
+lib_deps = 
+	ivanseidel/Thread@0.0.0-alpha+sha.1a4e504c5f
+	marcoschwartz/LiquidCrystal_I2C@^1.1.4
+	sui77/rc-switch@^2.6.4

+ 417 - 0
src/main.cpp

@@ -0,0 +1,417 @@
+// *****************************************************************************************
+// ************************************************************
+// **************************************
+// *
+// * Header Files
+// * 
+// *
+
+#define NewRemoteRcv   0
+#if NewRemoteRcv
+  #include <NewRemoteReceiver.h>
+#else
+  #include <RCSwitch.h>
+#endif
+
+#include <Thread.h>
+#include <Wire.h>
+#include <LiquidCrystal_I2C.h>
+
+LiquidCrystal_I2C lcd(0x27, 16, 2); // display type 16x2
+
+
+#include <avr/sleep.h>//this AVR library contains the methods that controls the sleep modes
+#include <avr/power.h>
+#include <avr/wdt.h>
+
+#define _THREADS  0 // 0 - Disable Threads; 1 - Enable Threads
+#define _InterruptLevel 0
+#define _InterruptPin   2 //Pin we are going to use to wake up the Arduino
+
+// *****************************************************************************************
+// ************************************************************
+// **************************************
+// *
+// * Debugging Level 
+// * 0 - Turn off Debugging Messages
+// * 0x0001 - General Level 
+// * 0x0010 - Thread Level
+// * 0x0100 - RF SNIFFER Messages
+ 
+#define DEBUGLEVEL  1 // Print Messages
+#define BAUDRATE  115200 // Debug Port Baudrate
+
+#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
+#define WAIT_SERIAL_DEBUG 0
+
+#define _DEBUG(level,msg, ...) do { \
+          if ((DEBUGLEVEL & level) && (level != 0))\
+              Serial.print((msg), ##__VA_ARGS__);\
+          } while(0)
+
+#define _DEBUGLN(level,msg, ...) do { \
+          if ((DEBUGLEVEL & level) && (level != 0))\
+             Serial.println((msg), ##__VA_ARGS__);\
+          } while(0)
+
+int freeRam () {
+  extern int __heap_start, *__brkval;
+  int v;
+  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
+}
+// *****************************************************************************************
+// ************************************************************
+// **************************************
+// *
+// * Global Variables
+// * 
+// *
+
+bool BoardRunning = false;
+
+Thread myThreadLed = Thread();
+Thread myThreadDisplay = Thread();
+
+#if _THREADS
+Thread myThreadRecv = Thread();
+Thread myThreadSend = Thread();
+Thread myThreadSendAliveMsg = Thread();
+#endif
+
+#if NewRemoteRcv
+  static unsigned int msgCounter = 0; 
+#else
+  RCSwitch mySwitch = RCSwitch();
+#endif
+
+unsigned long previousMillis = 0;
+const long interval = 1000; // delay in milliseconds
+
+unsigned long switchReceivedValue = 0;
+
+
+// *****************************************************************************************
+// ************************************************************
+// **************************************
+// *
+// * Functions Declaration and Implementation
+// * 
+// *
+
+// *
+// * Thread - Toggle LED
+// * 
+void threadLed(){
+  static bool ledStatus = false;
+  ledStatus = !ledStatus;
+
+  digitalWrite(LED_BUILTIN, ledStatus); //We use the led on pin 13 to indecate when Arduino is running
+
+  _DEBUG(2,"LED: ");
+  _DEBUGLN(2,millis());
+  switchReceivedValue = 0;
+}
+
+void threadDisplay(){
+  static bool displayBackgroundLight = false;
+  displayBackgroundLight = !displayBackgroundLight;
+
+  lcd.setCursor(0,0); 
+  lcd.print("AZ-Delivery");
+  lcd.setCursor(0,1);
+  lcd.print("Enjoy - Arduino");
+
+  if (displayBackgroundLight)
+     lcd.backlight(); // turn on LCD backlight 
+  else
+     lcd.noBacklight(); // turn off LCD backlight 
+
+  _DEBUG(2,"Display: ");
+  _DEBUGLN(2,millis());
+  switchReceivedValue = 0;
+}
+
+#if _THREADS
+
+// *
+// * Thread - Read Button
+// * 
+void threadButton(){
+  static bool buttonStatus = false;
+
+  _DEBUG(2,"Button: ");
+  _DEBUGLN(2,millis());
+}
+
+// *
+// * Thread - Receive Messages
+// * 
+void threadRecv(){
+  static bool recvStatus = false;
+
+  _DEBUG(2,"Recv: ");
+  _DEBUGLN(2,millis());
+}
+
+// *
+// * Thread - Send Messages
+// * 
+void threadSend(){
+  static bool sendStatus = false;
+
+  _DEBUG(2,"Send: ");
+  _DEBUGLN(2,millis());
+}
+
+// *
+// * Thread - Send Alive Message
+// * 
+void threadSendAliveMsg(){
+  static char msg = 'o';
+  static int  count = 0;
+
+  if (msg == 'o') 
+    msg = '-';
+  else 
+    msg = 'o';
+    
+  count++;
+  if(!(count % 60)){
+    static int iHours = 0;
+    _DEBUG(1,"\n");
+    _DEBUG(1,iHours);
+    _DEBUG(1, " hours: ");
+    iHours++;
+  }
+  _DEBUG(1,msg);
+}
+#endif
+// *
+// * Function SetupWdtSleep
+// *
+// *    Time  |   Macro   |  WDP0  WDP1  WDP2  WDP3  
+// *    16ms    WDTO_16MS     0     0     0     0 
+// *    32ms    WDTO_30MS     1     0     0     0
+// *    64ms    WDTO_60MS     0     1     0     0
+// *   125ms    WDTO_120MS    1     1     0     0
+// *   250ms    WDTO_250MS    0     0     1     0
+// *   500ms    WDTO_500MS    1     0     1     0
+// *     1s     WDTO_1S       0     1     1     0
+// *     2s     WDTO_2S       1     1     1     0
+// *     4s     WDTO_4S       0     0     0     1
+// *     8s     WDTO_8S       1     0     0     1
+// *
+// *
+// *
+void SetupWdtSleep() {
+  // Setup Watchdog Timers
+  MCUSR   &= ~(1<<WDRF); // clear WDT reset 
+  WDTCSR  |= (1<<WDCE) | (1<<WDE);   // Set WDCE, Prescaler
+  WDTCSR  =  WDTO_1S;
+  WDTCSR  |= 1<<WDIE; // Enable Interrupt
+
+  Serial.println("Init Watchdog");
+}
+
+// *
+// * Function Going-To-Sleep
+// * 
+void Going_To_Sleep(){
+  sleep_enable(); //Enabling sleep mode
+  set_sleep_mode(SLEEP_MODE_PWR_DOWN);//Setting sleep mode (Deep Sleep for power saving)
+#if NewRemoteRcv
+#else
+  mySwitch.disableReceive(); // Disable RF Receiver
+  sleep_mode();// activating sleep mode
+  mySwitch.enableReceive(_InterruptLevel); // Enable RF Receiver
+#endif  
+}
+
+// *
+// * Function ISR Wake-Up
+// * 
+ISR (WDT_vect)
+{
+
+}
+
+
+
+// *****************************************************************************************
+// ************************************************************
+// **************************************
+// *
+// * startup Code 
+// * 
+// *
+
+void setup() {
+  // put your setup code here, to run once:
+  if(DEBUGLEVEL){
+     Serial.begin(BAUDRATE); 
+     _DEBUGLN(1, "*************************************************");
+     _DEBUG(1, "Board initialized successfully: ");
+     _DEBUGLN(1, __FILENAME__); 
+     _DEBUG(1, "Build date: "); _DEBUGLN(1, __DATE__);
+  }
+
+  pinMode(LED_BUILTIN,OUTPUT);//We use the led on pin 13 to indecate when Arduino is A sleep
+  pinMode(_InterruptPin,INPUT_PULLUP);//Set pin d2 to input using the buildin pullup resistor
+  // digitalWrite(LED_BUILTIN,HIGH);//turning LED on
+
+  // Initialize LCD 
+  lcd.init();
+  // lcd.backlight(); // turn on LCD backlight 
+  // lcd.noBacklight(); // turn off LCD backlight 
+
+  //
+  // Initialize Threads
+  //
+  myThreadLed.onRun(threadLed);
+  myThreadLed.setInterval(1500);
+
+  myThreadDisplay.onRun(threadDisplay);
+  myThreadDisplay.setInterval(1500);
+
+#if _THREADS
+
+  myThreadRecv.onRun(threadRecv);
+  myThreadRecv.setInterval(1700);
+
+  myThreadSend.onRun(threadSend);
+  myThreadSend.setInterval(1800);
+
+  myThreadSendAliveMsg.onRun(threadSendAliveMsg);
+  myThreadSendAliveMsg.setInterval(60000);
+#endif
+
+#if NewRemoteRcv
+  // Initialize receiver on interrupt 0 (= digital pin 2) for arduino uno, calls the callback "showCode"
+  // after 1 identical codes have been received in a row. (thus, keep the button pressed
+  // for a moment), on esp8266 use on interrupt 5 = digital pin 1
+  //
+  // See the interrupt-parameter of attachInterrupt for possible values (and pins)
+  // to connect the receiver.
+
+  // if you don't see codes try to reset your board after upload
+  NewRemoteReceiver::init(0, 2, showCode);
+  Serial.println("Receiver initialized");   
+
+#else
+  // RFControl::startReceiving(0);
+  mySwitch.enableReceive(_InterruptLevel); // receives Data based on Interrupt
+  // attachInterrupt(0, wake_up, CHANGE);//attaching an interrupt to pin d2
+#endif
+  
+  Serial.println("14CORE | RF Sniffer Test Code for Receiver");
+  Serial.println("RF Sniffer Initializing......");
+  delay(2000);
+  Serial.println("Starting.....................");
+  delay(2000);    
+ // SetupWdtSleep();
+}
+
+// *****************************************************************************************
+// ************************************************************
+// **************************************
+// *
+// * loop Code 
+// * 
+// *
+
+void loop() {
+  // put your main code here, to run repeatedly:
+  unsigned long currentMillis = millis();
+  
+  if (!BoardRunning){
+     _DEBUGLN(1, "Entering loop()");
+     _DEBUG(1, "Free RAM: ");
+     _DEBUGLN(1, freeRam());
+     _DEBUGLN(1, "*************************************************");
+     BoardRunning = true;     
+     delay(1000);
+  }
+
+ if(myThreadDisplay.shouldRun())
+     myThreadDisplay.run(); 
+
+  // if(myThreadLed.shouldRun())
+  //   myThreadLed.run(); 
+
+#if _THREADS
+
+  if(myThreadRecv.shouldRun())
+     myThreadRecv.run(); 
+
+  if(myThreadSend.shouldRun())
+     myThreadSend.run(); 
+
+  if(myThreadSendAliveMsg.shouldRun())
+     myThreadSendAliveMsg.run();
+
+#endif
+
+#if NewRemoteRcv
+
+#else
+  //
+  // 
+  //
+  if (mySwitch.available()) {
+
+    int value = mySwitch.getReceivedValue();
+    if (value == 0) {
+      Serial.println("Unknown encoding");
+    } else {
+      // digitalWrite(LED_BUILTIN,HIGH);// turning LED on  
+      _DEBUG(4,millis());
+      _DEBUG(4,"\t");
+      _DEBUG(4,"Received 0x");
+      _DEBUG(4,mySwitch.getReceivedValue(),HEX);
+      _DEBUG(4," / ");
+      _DEBUG(4,mySwitch.getReceivedBitlength());
+      _DEBUG(4," Bit / Protocol: ");
+      _DEBUG(4,mySwitch.getReceivedProtocol());
+      _DEBUG(4," / Delay: ");
+      _DEBUGLN(4,mySwitch.getReceivedDelay());
+      // output(mySwitch.getReceivedValue(), mySwitch.getReceivedBitlength(), mySwitch.getReceivedDelay(), mySwitch.getReceivedRawdata(),mySwitch.getReceivedProtocol());      
+      // decoder(mySwitch.getReceivedValue(), mySwitch.getReceivedProtocol());     
+    }
+    mySwitch.resetAvailable();
+    delay(150);
+  } 
+#endif
+
+ if(currentMillis - previousMillis >= 200)
+ {
+   // digitalWrite(LED_BUILTIN, LOW);// turning LED off  
+   // _DEBUGLN(4,"...going to sleep");
+   // Going_To_Sleep();
+   previousMillis = millis();
+ }  
+} // loop
+
+#if NewRemoteRcv
+
+// Callback function is called only when a valid code is received.
+void showCode(unsigned int period, unsigned long address, unsigned long groupBit, unsigned long unit, unsigned long switchType) {
+
+  Serial.print("Counter: "); 
+  Serial.println(msgCounter);
+
+  // Print the received code.
+  Serial.print("Code: ");
+  Serial.print(address);
+  Serial.print(" Period: ");
+  Serial.println(period);
+  Serial.print(" unit: ");
+  Serial.println(unit);
+  Serial.print(" groupBit: ");
+  Serial.println(groupBit);
+  Serial.print(" switchType: ");
+  Serial.println(switchType);
+
+  Serial.println("----------------------------------------------------");
+  msgCounter++;
+}
+#endif

+ 11 - 0
test/README

@@ -0,0 +1,11 @@
+
+This directory is intended for PlatformIO Unit Testing and project tests.
+
+Unit Testing is a software testing method by which individual units of
+source code, sets of one or more MCU program modules together with associated
+control data, usage procedures, and operating procedures, are tested to
+determine whether they are fit for use. Unit testing finds problems early
+in the development cycle.
+
+More information about PlatformIO Unit Testing:
+- https://docs.platformio.org/page/plus/unit-testing.html