Browse Source

Added switches code

Toby Chui 6 months ago
parent
commit
2d93d89f72

+ 82 - 0
hardware/hds_5-way-switch/firmware/5xswitches/5xswitches.ino

@@ -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();
+}

+ 52 - 0
hardware/hds_5-way-switch/firmware/5xswitches/buttons.ino

@@ -0,0 +1,52 @@
+void buttonPressCallbacks(){
+  updateButtonStates();
+  updateShiftRegister();
+}
+
+void updateButtonStates() {
+  // Read the state of each button and update the boolean array
+  for (int i = 0; i < numButtons-1; ++i) {
+    buttonStates[i] = digitalRead(buttonPins[i]) == LOW;
+  }
+
+  //Read the analog emulated pins
+  int a0Valu = analogRead(A0);
+  buttonStates[4] = (a0Valu < 1000);
+}
+
+// Function to reset all outputs to 0
+void resetOutputs() {
+  // Shift in 8 zeros (all outputs off)
+  for (int i = 0; i < 8; ++i) {
+    digitalWrite(SER_PIN, LOW);
+    digitalWrite(SRCLK_PIN, HIGH);
+    delay(1); // Optional: Adjust delay if needed
+    digitalWrite(SRCLK_PIN, LOW);
+  }
+  // Latch the data to the output pins
+  digitalWrite(RCLK_PIN, HIGH);
+  delay(1); // Optional: Adjust delay if needed
+  digitalWrite(RCLK_PIN, LOW);
+}
+
+void updateShiftRegister() {
+  // Prepare the data to be shifted into the shift register
+  byte data = 0b00000000; // Clear the bits
+
+  for (int i=0; i < numButtons; i++){
+    data |= buttonStates[i] << (7 - i);
+  }
+
+  // Shift the data into the shift register
+  for (int i = 7; i >= 0; --i) {
+    digitalWrite(SER_PIN, (data >> i) & 0b1);
+    digitalWrite(SRCLK_PIN, HIGH);
+    delay(1); // Optional: Adjust delay if needed
+    digitalWrite(SRCLK_PIN, LOW);
+  }
+
+  // Latch the data to the output pins of the shift register
+  digitalWrite(RCLK_PIN, HIGH);
+  delay(1); // Optional: Adjust delay if needed
+  digitalWrite(RCLK_PIN, LOW);
+}

+ 27 - 0
hardware/hds_5-way-switch/firmware/5xswitches/mesh.ino

@@ -0,0 +1,27 @@
+
+void sendMessage() {
+  String msg = "Hi from node1";
+  msg += mesh.getNodeId();
+  mesh.sendBroadcast( msg );
+  taskSendMessage.setInterval( random( TASK_SECOND * 1, TASK_SECOND * 5 ));
+}
+
+
+
+
+// Painless Mesh callbacks
+void receivedCallback( uint32_t from, String &msg ) {
+  Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str());
+}
+
+void newConnectionCallback(uint32_t nodeId) {
+    Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
+}
+
+void changedConnectionCallback() {
+  Serial.printf("Changed connections\n");
+}
+
+void nodeTimeAdjustedCallback(int32_t offset) {
+    Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset);
+}

BIN
hardware/hds_5-way-switch/pcb/Gerber_HomeDynamic 5-way switch.zip


File diff suppressed because it is too large
+ 49 - 0
hardware/hds_5-way-switch/pcb/PCB_HomeDynamic 5-way switch_2023-10-26.json


Some files were not shown because too many files changed in this diff