Browse Source

More talking to CAN (state change, new TWids)

wiebel 7 months ago
parent
commit
f63b37a9de
2 changed files with 50 additions and 6 deletions
  1. 48
    4
      src/CANNode.cpp
  2. 2
    2
      src/CANNode.h

+ 48
- 4
src/CANNode.cpp View File

@@ -3,9 +3,10 @@
3 3
 // -------------------------------------------------------------
4 4
 // CANNode for Teensy 3.1/3.2
5 5
 // GPLv2
6
-// by wiebel (c) 2015-2018 
6
+// by wiebel (c) 2015-2018
7 7
 // github_push_test_01
8 8
 
9
+#include <vector>
9 10
 #include <Metro.h>
10 11
 #include <FlexCAN.h>
11 12
 #include <OneWire.h>
@@ -121,10 +122,31 @@ uint8_t toggle_Pin(uint8_t pin){
121 122
   return digitalRead(pin);
122 123
 }
123 124
 
124
-uint32_t forgeid(event_t event){
125
+uint32_t event2id(event_t event){
125 126
   return (event.prio<<26)+(event.dst<<16)+(NODE_ID<<8)+event.cmd;
126 127
 }
127 128
 
129
+uint32_t forgeid(uint8_t prio, uint8_t dst, uint8_t cmd, uint8_t type=0){
130
+  // CMD:
131
+  // Type: 0: SINGLE, 1: FIRST, 2: CONT, 3: LAST
132
+  return (prio<<26)+(type<<24)+(dst<<16)+(NODE_ID<<8)+cmd;
133
+}
134
+int CAN_send(uint8_t prio, uint8_t dst, uint8_t cmd, uint8_t data[], uint8_t type=0){
135
+  txmsg.id = forgeid(prio, dst, cmd, type);
136
+  txmsg.len = sizeof(data);
137
+  for (uint8_t i = 0; i < txmsg.len; i++) { txmsg.buf[i] = data[i]; }
138
+  CANbus.write(txmsg);
139
+}
140
+
141
+int CAN_send(uint8_t prio, uint8_t dst, uint8_t cmd, uint8_t data, uint8_t type=0){
142
+  txmsg.id = forgeid(prio, dst, cmd, type);
143
+  //txmsg.len = sizeof(data);
144
+  txmsg.len = 1;
145
+  txmsg.buf[0] = data;
146
+  CANbus.write(txmsg);
147
+}
148
+
149
+
128 150
 telegram_comp_t parse_CAN(CAN_message_t mesg){
129 151
   telegram_comp_t tmp;
130 152
   tmp.prio=mesg.id>>26;
@@ -142,7 +164,7 @@ void send_event (uint8_t trig_event){
142 164
 
143 165
   for (uint8_t e_idx = 0; tx_events[e_idx].tag != 0; e_idx++)
144 166
     if ( tx_events[e_idx].tag == trig_event ) {
145
-      txmsg.id = forgeid(tx_events[e_idx]);
167
+      txmsg.id = event2id(tx_events[e_idx]);
146 168
       txmsg.len = 1;
147 169
 //      txmsg.timeout = 100;
148 170
       txmsg.buf[0]= tx_events[e_idx].data;
@@ -154,9 +176,11 @@ void send_event (uint8_t trig_event){
154 176
       txmsg.len = 0;
155 177
     }
156 178
   }
157
-void take_action (action_type type, uint8_t tag ){
179
+
180
+void take_action (cmd_type type, uint8_t tag ){
158 181
 for (uint8_t i = 0; action_map[i].tag != 0 ; i++) {
159 182
   if ( action_map[i].tag == tag ) {
183
+    bool old_state = digitalRead(outputs[action_map[i].outputs_idx].address);
160 184
     switch ( type ) {
161 185
       case OFF:
162 186
         digitalWrite(outputs[action_map[i].outputs_idx].address,LOW ^ outputs[action_map[i].outputs_idx].invert);
@@ -179,6 +203,23 @@ for (uint8_t i = 0; action_map[i].tag != 0 ; i++) {
179 203
         Serial.println(F("TBD"));
180 204
         break;
181 205
       }
206
+      bool new_state = digitalRead(outputs[action_map[i].outputs_idx].address);
207
+      if ( old_state == new_state) {
208
+        switch ( new_state ) {
209
+          case 0:
210
+            CAN_send(NOTIFY, 0xFF, OFF, action_map[i].outputs_idx);
211
+          case 1:
212
+            CAN_send(NOTIFY, 0xFF, ON, action_map[i].outputs_idx);
213
+        }
214
+      }
215
+      else {
216
+        switch ( new_state ) {
217
+          case 0:
218
+            CAN_send(NOTIFY, 0xFF, T_OFF, action_map[i].outputs_idx);
219
+          case 1:
220
+            CAN_send(NOTIFY, 0xFF, T_ON, action_map[i].outputs_idx);
221
+        }
222
+      }
182 223
     }
183 224
   }
184 225
 }
@@ -252,8 +293,11 @@ void loop(void)
252 293
             }
253 294
           }
254 295
         if (new_owd) {
296
+          // send new ID to all
297
+          CAN_send(NOTIFY, 0xFF, NEW_TWID, addr);
255 298
           Serial.print("Found a device: ");
256 299
           print_OW_Device(addr);
300
+
257 301
           Serial.println();
258 302
         }
259 303
 

+ 2
- 2
src/CANNode.h View File

@@ -4,8 +4,8 @@
4 4
 enum out_type { GPIO, PWM, OW, I2C, SPI, WS2811, DMX, NOP };
5 5
 enum event_type { LOCAL, SEND};
6 6
 
7
-enum action_type { OFF, ON, VALUE, TOGGLE };
8
-enum telegram_type { ALERT, EVENT, NOTIFY, INFO };
7
+enum cmd_type { OFF, ON, VALUE, TOGGLE, T_OFF, T_ON, BIN_DUMP, NEW_TWID };
8
+enum telegram_prio { ALERT, EVENT, NOTIFY, INFO };
9 9
 
10 10
 typedef struct CAN_telegram_t {
11 11
   uint32_t id;