|
@@ -0,0 +1,82 @@
|
|
|
+/*
|
|
|
+
|
|
|
+ Home Dynamic System v3
|
|
|
+
|
|
|
+ 5-way wall switches
|
|
|
+ Hardware reference design
|
|
|
+
|
|
|
+ Author: tobychui
|
|
|
+*/
|
|
|
+
|
|
|
+#include "painlessMesh.h"
|
|
|
+
|
|
|
+//Mesh settings
|
|
|
+#define MESH_PREFIX "homedynamicv3"
|
|
|
+#define MESH_PASSWORD "verySecurePassword"
|
|
|
+#define MESH_PORT 9640
|
|
|
+
|
|
|
+// Output shift register pints
|
|
|
+const int SRCLK_PIN = D7;
|
|
|
+const int RCLK_PIN = D1;
|
|
|
+const int SER_PIN = D2;
|
|
|
+
|
|
|
+// Input button pins (The last one must be A0 pin)
|
|
|
+bool buttonStates[5] = {false, false, false, false, false};
|
|
|
+const int buttonPins[] = {D4, D6, D5, D0, A0};
|
|
|
+const int numButtons = 5;
|
|
|
+
|
|
|
+// Exception status led
|
|
|
+
|
|
|
+//Runtimes
|
|
|
+Scheduler deviceScheduler; // to control your personal task
|
|
|
+painlessMesh mesh;
|
|
|
+
|
|
|
+// Function definations
|
|
|
+void updateButtonStates();
|
|
|
+
|
|
|
+
|
|
|
+//System Tasks
|
|
|
+void buttonPressCallbacks();
|
|
|
+void sendMessage();
|
|
|
+Task taskButtons(10, TASK_FOREVER, &buttonPressCallbacks);
|
|
|
+Task taskSendMessage( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage );
|
|
|
+
|
|
|
+void setup() {
|
|
|
+ //Enable debug Serial
|
|
|
+ Serial.begin(115200);
|
|
|
+
|
|
|
+ // Initialize shift register pins
|
|
|
+ pinMode(SRCLK_PIN, OUTPUT);
|
|
|
+ pinMode(RCLK_PIN, OUTPUT);
|
|
|
+ pinMode(SER_PIN, OUTPUT);
|
|
|
+
|
|
|
+ // Initialize button pins
|
|
|
+ for (int i = 0; i < numButtons; ++i) {
|
|
|
+ pinMode(buttonPins[i], INPUT_PULLUP);
|
|
|
+ }
|
|
|
+
|
|
|
+ //Reset the state of all LEDs
|
|
|
+ resetOutputs();
|
|
|
+
|
|
|
+ //Start mesh networking
|
|
|
+ mesh.setDebugMsgTypes( ERROR | STARTUP ); // set before init() so that you can see startup messages
|
|
|
+
|
|
|
+ mesh.init( MESH_PREFIX, MESH_PASSWORD, &deviceScheduler, MESH_PORT );
|
|
|
+ mesh.onReceive(&receivedCallback);
|
|
|
+ mesh.onNewConnection(&newConnectionCallback);
|
|
|
+ mesh.onChangedConnections(&changedConnectionCallback);
|
|
|
+ mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
|
|
|
+
|
|
|
+ //Add tasks to scheduler
|
|
|
+ deviceScheduler.addTask(taskSendMessage);
|
|
|
+ deviceScheduler.addTask(taskButtons);
|
|
|
+
|
|
|
+ //Enable tasks
|
|
|
+ taskSendMessage.enable();
|
|
|
+ taskButtons.enable();
|
|
|
+}
|
|
|
+
|
|
|
+void loop() {
|
|
|
+ // put your main code here, to run repeatedly:
|
|
|
+ mesh.update();
|
|
|
+}
|