Browse Source

much better can bus writing

I_sense
mwaiblinger 5 years ago
parent
commit
6a76239f40
  1. 66
      src/CANNode.cpp
  2. 16
      src/Node_3_def.h

66
src/CANNode.cpp

@ -14,7 +14,9 @@
// For Node definition:
#include "Node_2_def.h"
//#include "Node_1_def.h" // Dachstuhl
#include "Node_2_def.h" // Werkstatt
//#include "Node_3_def.h" // Keller
// Metro ticks in ms
#define METRO_CAN_tick 1
@ -66,12 +68,13 @@ FlexCAN CANbus(CAN_speed);
uint8_t addr[8];
uint8_t data[8];
uint8_t buffer[DS2406_BUF_LEN];
uint8_t readout,trig_event,event_idx,tmp;
uint8_t readout,recheck,trig_event,event_idx,tmp;
OneWire OW_1(OW_pin);
telegram_comp_t mesg_comp;
static CAN_message_t txmsg,rxmsg;
static CAN_message_t global_txmsg,rxmsg;
CAN_message_t txmsg;
static uint8_t hex[17] = "0123456789abcdef";
int txCount,rxCount;
@ -134,16 +137,25 @@ uint32_t forgeid(uint8_t prio, uint8_t dst, uint8_t cmd, uint8_t type=0){
}
int CAN_send(uint8_t prio, uint8_t dst, uint8_t cmd, uint8_t* data, uint8_t data_size, uint8_t type=0){
txmsg.ext = 1;
txmsg.timeout = 100;
txmsg.id = forgeid(prio, dst, cmd, type);
txmsg.len = data_size;
for (uint8_t i = 0; i < txmsg.len; i++) { txmsg.buf[i] = data[i]; }
CANbus.write(txmsg);
// txmsg.buf[0]++;
// txmsg.len = 0;
}
int CAN_send(uint8_t prio, uint8_t dst, uint8_t cmd, uint8_t data, uint8_t type=0){
txmsg.ext = 1;
txmsg.timeout = 100;
txmsg.id = forgeid(prio, dst, cmd, type);
//txmsg.len = sizeof(data);
txmsg.len = 1;
txmsg.buf[0] = data;
CANbus.write(txmsg);
// txmsg.buf[0]++;
// txmsg.len = 0;
}
@ -164,16 +176,18 @@ void send_event (uint8_t trig_event){
for (uint8_t e_idx = 0; tx_events[e_idx].tag != 0; e_idx++)
if ( tx_events[e_idx].tag == trig_event ) {
txmsg.ext = 1;
txmsg.timeout = 100;
txmsg.id = event2id(tx_events[e_idx]);
txmsg.len = 1;
// txmsg.timeout = 100;
txmsg.buf[0]= tx_events[e_idx].data;
CANbus.write(txmsg);
Serial.print(F("Sending to CAN ID: "));
Serial.print(txmsg.id);
Serial.print(F("DATA: "));
Serial.println(txmsg.buf[0]);
txmsg.len = 0;
// txmsg.buf[0]++;
// txmsg.len = 0;
}
}
@ -205,12 +219,12 @@ for (uint8_t i = 0; action_map[i].tag != 0 ; i++) {
}
bool new_state = digitalRead(outputs[action_map[i].outputs_idx].address);
if ( old_state == new_state) {
data[0]=outputs[action_map[i].outputs_idx].address;
data[0]=action_map[i].outputs_idx;
data[1]=new_state ^ outputs[action_map[i].outputs_idx].invert;
CAN_send(NOTIFY, 0x0, STATE_OUT, data, 2);
}
else {
data[0]=outputs[action_map[i].outputs_idx].address;
data[0]=action_map[i].outputs_idx;
data[1]=new_state ^ outputs[action_map[i].outputs_idx].invert;
CAN_send(NOTIFY, 0x0, NEWSTATE_OUT, data, 2);
}
@ -231,8 +245,6 @@ void setup(void)
pinMode(CAN_RS_PIN,OUTPUT);
digitalWrite(CAN_RS_PIN,0);
CANbus.begin();
txmsg.ext = 1;
txmsg.timeout = 100;
// outputs
for (size_t i = 0; outputs[i].type != NOP; i++) {
@ -317,11 +329,15 @@ void loop(void)
}
// if ((switches_state[s_idx] != readout) && (readout != 255 )) {
if (switches_state[s_idx] != readout) {
delay(1);
recheck = read_DS2406(switches[s_idx].addr);
if (readout == recheck) {
tmp = readout ^ switches_state[s_idx];
switches_state[s_idx] = readout;
action[0] = tmp & 0x08;
action[1] = tmp & 0x04;
}
}
if (action[0]) {
Serial.print("pioA of switch ");
Serial.print(switches[s_idx].nick);
@ -330,14 +346,12 @@ void loop(void)
data[0]=switches[s_idx].nick;
data[1]=0;
CAN_send(NOTIFY, 0x0, STATE_IN, data, 2);
CANbus.write(txmsg);
send_event(switches[s_idx].event_tag[0]);
} else {
Serial.println(F(" is now ON"));
data[0]=switches[s_idx].nick;
data[1]=1;
CAN_send(NOTIFY, 0x0, STATE_IN, data, 2);
CANbus.write(txmsg);
send_event(switches[s_idx].event_tag[1]);
}
action[0] = 0;
@ -350,14 +364,12 @@ void loop(void)
data[0]=switches[s_idx].nick;
data[1]=2;
CAN_send(NOTIFY, 0x0, STATE_IN, data, 2);
CANbus.write(txmsg);
send_event(switches[s_idx].event_tag[2]);
} else {
Serial.println(F(" is now ON"));
data[0]=switches[s_idx].nick;
data[1]=3;
CAN_send(NOTIFY, 0x0, STATE_IN, data, 2);
CANbus.write(txmsg);
send_event(switches[s_idx].event_tag[3]);
}
action[1] = 0;
@ -406,18 +418,18 @@ void loop(void)
}
rxCount = 0;
}
txTimer = 100;//milliseconds
if (txmsg.len != 0){
Serial.print("PUT=");
Serial.print(txmsg.id,HEX);
for (uint8_t i=0; i<txmsg.len; i++){
Serial.print(":");
Serial.print(txmsg.buf[i],HEX);
}
Serial.print("\n");
CANbus.write(txmsg);
txmsg.buf[0]++;
txmsg.len = 0;
}
// txTimer = 10;//milliseconds
// if (txmsg.len != 0){
// Serial.print("PUT=");
// Serial.print(txmsg.id,HEX);
// for (uint8_t i=0; i<txmsg.len; i++){
// Serial.print(":");
// Serial.print(txmsg.buf[i],HEX);
// }
// Serial.print("\n");
// CANbus.write(txmsg);
// txmsg.buf[0]++;
// txmsg.len = 0;
// }
}

16
src/Node_3_def.h

@ -19,8 +19,8 @@
#define RELAY4 22
#define RELAY5 17
#define RELAY6 16
#define RELAY7 9
#define RELAY8 10
#define RELAY7 19
#define RELAY8 18
// OneWire
#define OW_pin 14
@ -47,8 +47,8 @@ static outputs_t outputs[N_OUTPUTS] PROGMEM={
{ GPIO, 22, 0, true }, // 3
{ GPIO, 17, 0, true }, // 4
{ GPIO, 16, 0, true }, // 5
{ GPIO, 9, 255, true }, // 6
{ GPIO, 10, 0, true }, // 7
{ GPIO, 19, 0, true }, // 6
{ GPIO, 18, 0, true }, // 7
{ NOP, 0xFF, 0, 0 }
};
static uint8_t outputs_state[N_OUTPUTS];
@ -69,10 +69,10 @@ static event_t tx_events[N_EVENTS] PROGMEM={
{ 8, 0x03, 0x03, TOGGLE, 0x08},
{ 11, 0x03, 0x01, ON, 0x01},
{ 12, 0x03, 0x01, ON, 0x02},
{ 210, 0x03, 0xff, OFF, 0x04},
{ 211, 0x03, 0xff, ON, 0x04},
{ 220, 0x03, 0xff, OFF, 0x05},
{ 221, 0x03, 0xff, ON, 0x05},
{ 210, 0x03, 0x03, OFF, 0x04},
{ 211, 0x03, 0x03, ON, 0x04},
{ 220, 0x03, 0x03, OFF, 0x05},
{ 221, 0x03, 0x03, ON, 0x05},
{ 255, 0x03, 0xff, OFF, 0x09},
{ 254, 0x03, 0xff, ON, 0x09},
{ 10, 0x03, 0xff, TOGGLE, 0x01},

Loading…
Cancel
Save