much better can bus writing
This commit is contained in:
parent
584c263eaa
commit
6a76239f40
@ -14,7 +14,9 @@
|
|||||||
|
|
||||||
// For Node definition:
|
// 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
|
// Metro ticks in ms
|
||||||
#define METRO_CAN_tick 1
|
#define METRO_CAN_tick 1
|
||||||
@ -66,12 +68,13 @@ FlexCAN CANbus(CAN_speed);
|
|||||||
uint8_t addr[8];
|
uint8_t addr[8];
|
||||||
uint8_t data[8];
|
uint8_t data[8];
|
||||||
uint8_t buffer[DS2406_BUF_LEN];
|
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);
|
OneWire OW_1(OW_pin);
|
||||||
|
|
||||||
telegram_comp_t mesg_comp;
|
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";
|
static uint8_t hex[17] = "0123456789abcdef";
|
||||||
|
|
||||||
int txCount,rxCount;
|
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){
|
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.id = forgeid(prio, dst, cmd, type);
|
||||||
txmsg.len = data_size;
|
txmsg.len = data_size;
|
||||||
for (uint8_t i = 0; i < txmsg.len; i++) { txmsg.buf[i] = data[i]; }
|
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){
|
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.id = forgeid(prio, dst, cmd, type);
|
||||||
//txmsg.len = sizeof(data);
|
|
||||||
txmsg.len = 1;
|
txmsg.len = 1;
|
||||||
txmsg.buf[0] = data;
|
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++)
|
for (uint8_t e_idx = 0; tx_events[e_idx].tag != 0; e_idx++)
|
||||||
if ( tx_events[e_idx].tag == trig_event ) {
|
if ( tx_events[e_idx].tag == trig_event ) {
|
||||||
|
txmsg.ext = 1;
|
||||||
|
txmsg.timeout = 100;
|
||||||
txmsg.id = event2id(tx_events[e_idx]);
|
txmsg.id = event2id(tx_events[e_idx]);
|
||||||
txmsg.len = 1;
|
txmsg.len = 1;
|
||||||
// txmsg.timeout = 100;
|
|
||||||
txmsg.buf[0]= tx_events[e_idx].data;
|
txmsg.buf[0]= tx_events[e_idx].data;
|
||||||
CANbus.write(txmsg);
|
CANbus.write(txmsg);
|
||||||
Serial.print(F("Sending to CAN ID: "));
|
Serial.print(F("Sending to CAN ID: "));
|
||||||
Serial.print(txmsg.id);
|
Serial.print(txmsg.id);
|
||||||
Serial.print(F("DATA: "));
|
Serial.print(F("DATA: "));
|
||||||
Serial.println(txmsg.buf[0]);
|
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);
|
bool new_state = digitalRead(outputs[action_map[i].outputs_idx].address);
|
||||||
if ( old_state == new_state) {
|
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;
|
data[1]=new_state ^ outputs[action_map[i].outputs_idx].invert;
|
||||||
CAN_send(NOTIFY, 0x0, STATE_OUT, data, 2);
|
CAN_send(NOTIFY, 0x0, STATE_OUT, data, 2);
|
||||||
}
|
}
|
||||||
else {
|
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;
|
data[1]=new_state ^ outputs[action_map[i].outputs_idx].invert;
|
||||||
CAN_send(NOTIFY, 0x0, NEWSTATE_OUT, data, 2);
|
CAN_send(NOTIFY, 0x0, NEWSTATE_OUT, data, 2);
|
||||||
}
|
}
|
||||||
@ -231,8 +245,6 @@ void setup(void)
|
|||||||
pinMode(CAN_RS_PIN,OUTPUT);
|
pinMode(CAN_RS_PIN,OUTPUT);
|
||||||
digitalWrite(CAN_RS_PIN,0);
|
digitalWrite(CAN_RS_PIN,0);
|
||||||
CANbus.begin();
|
CANbus.begin();
|
||||||
txmsg.ext = 1;
|
|
||||||
txmsg.timeout = 100;
|
|
||||||
|
|
||||||
// outputs
|
// outputs
|
||||||
for (size_t i = 0; outputs[i].type != NOP; i++) {
|
for (size_t i = 0; outputs[i].type != NOP; i++) {
|
||||||
@ -317,10 +329,14 @@ void loop(void)
|
|||||||
}
|
}
|
||||||
// if ((switches_state[s_idx] != readout) && (readout != 255 )) {
|
// if ((switches_state[s_idx] != readout) && (readout != 255 )) {
|
||||||
if (switches_state[s_idx] != readout) {
|
if (switches_state[s_idx] != readout) {
|
||||||
tmp = readout ^ switches_state[s_idx];
|
delay(1);
|
||||||
switches_state[s_idx] = readout;
|
recheck = read_DS2406(switches[s_idx].addr);
|
||||||
action[0] = tmp & 0x08;
|
if (readout == recheck) {
|
||||||
action[1] = tmp & 0x04;
|
tmp = readout ^ switches_state[s_idx];
|
||||||
|
switches_state[s_idx] = readout;
|
||||||
|
action[0] = tmp & 0x08;
|
||||||
|
action[1] = tmp & 0x04;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (action[0]) {
|
if (action[0]) {
|
||||||
Serial.print("pioA of switch ");
|
Serial.print("pioA of switch ");
|
||||||
@ -330,14 +346,12 @@ void loop(void)
|
|||||||
data[0]=switches[s_idx].nick;
|
data[0]=switches[s_idx].nick;
|
||||||
data[1]=0;
|
data[1]=0;
|
||||||
CAN_send(NOTIFY, 0x0, STATE_IN, data, 2);
|
CAN_send(NOTIFY, 0x0, STATE_IN, data, 2);
|
||||||
CANbus.write(txmsg);
|
|
||||||
send_event(switches[s_idx].event_tag[0]);
|
send_event(switches[s_idx].event_tag[0]);
|
||||||
} else {
|
} else {
|
||||||
Serial.println(F(" is now ON"));
|
Serial.println(F(" is now ON"));
|
||||||
data[0]=switches[s_idx].nick;
|
data[0]=switches[s_idx].nick;
|
||||||
data[1]=1;
|
data[1]=1;
|
||||||
CAN_send(NOTIFY, 0x0, STATE_IN, data, 2);
|
CAN_send(NOTIFY, 0x0, STATE_IN, data, 2);
|
||||||
CANbus.write(txmsg);
|
|
||||||
send_event(switches[s_idx].event_tag[1]);
|
send_event(switches[s_idx].event_tag[1]);
|
||||||
}
|
}
|
||||||
action[0] = 0;
|
action[0] = 0;
|
||||||
@ -350,14 +364,12 @@ void loop(void)
|
|||||||
data[0]=switches[s_idx].nick;
|
data[0]=switches[s_idx].nick;
|
||||||
data[1]=2;
|
data[1]=2;
|
||||||
CAN_send(NOTIFY, 0x0, STATE_IN, data, 2);
|
CAN_send(NOTIFY, 0x0, STATE_IN, data, 2);
|
||||||
CANbus.write(txmsg);
|
|
||||||
send_event(switches[s_idx].event_tag[2]);
|
send_event(switches[s_idx].event_tag[2]);
|
||||||
} else {
|
} else {
|
||||||
Serial.println(F(" is now ON"));
|
Serial.println(F(" is now ON"));
|
||||||
data[0]=switches[s_idx].nick;
|
data[0]=switches[s_idx].nick;
|
||||||
data[1]=3;
|
data[1]=3;
|
||||||
CAN_send(NOTIFY, 0x0, STATE_IN, data, 2);
|
CAN_send(NOTIFY, 0x0, STATE_IN, data, 2);
|
||||||
CANbus.write(txmsg);
|
|
||||||
send_event(switches[s_idx].event_tag[3]);
|
send_event(switches[s_idx].event_tag[3]);
|
||||||
}
|
}
|
||||||
action[1] = 0;
|
action[1] = 0;
|
||||||
@ -406,18 +418,18 @@ void loop(void)
|
|||||||
}
|
}
|
||||||
rxCount = 0;
|
rxCount = 0;
|
||||||
}
|
}
|
||||||
txTimer = 100;//milliseconds
|
// txTimer = 10;//milliseconds
|
||||||
if (txmsg.len != 0){
|
// if (txmsg.len != 0){
|
||||||
Serial.print("PUT=");
|
// Serial.print("PUT=");
|
||||||
Serial.print(txmsg.id,HEX);
|
// Serial.print(txmsg.id,HEX);
|
||||||
for (uint8_t i=0; i<txmsg.len; i++){
|
// for (uint8_t i=0; i<txmsg.len; i++){
|
||||||
Serial.print(":");
|
// Serial.print(":");
|
||||||
Serial.print(txmsg.buf[i],HEX);
|
// Serial.print(txmsg.buf[i],HEX);
|
||||||
}
|
// }
|
||||||
Serial.print("\n");
|
// Serial.print("\n");
|
||||||
CANbus.write(txmsg);
|
// CANbus.write(txmsg);
|
||||||
txmsg.buf[0]++;
|
// txmsg.buf[0]++;
|
||||||
txmsg.len = 0;
|
// txmsg.len = 0;
|
||||||
}
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,8 +19,8 @@
|
|||||||
#define RELAY4 22
|
#define RELAY4 22
|
||||||
#define RELAY5 17
|
#define RELAY5 17
|
||||||
#define RELAY6 16
|
#define RELAY6 16
|
||||||
#define RELAY7 9
|
#define RELAY7 19
|
||||||
#define RELAY8 10
|
#define RELAY8 18
|
||||||
|
|
||||||
// OneWire
|
// OneWire
|
||||||
#define OW_pin 14
|
#define OW_pin 14
|
||||||
@ -47,8 +47,8 @@ static outputs_t outputs[N_OUTPUTS] PROGMEM={
|
|||||||
{ GPIO, 22, 0, true }, // 3
|
{ GPIO, 22, 0, true }, // 3
|
||||||
{ GPIO, 17, 0, true }, // 4
|
{ GPIO, 17, 0, true }, // 4
|
||||||
{ GPIO, 16, 0, true }, // 5
|
{ GPIO, 16, 0, true }, // 5
|
||||||
{ GPIO, 9, 255, true }, // 6
|
{ GPIO, 19, 0, true }, // 6
|
||||||
{ GPIO, 10, 0, true }, // 7
|
{ GPIO, 18, 0, true }, // 7
|
||||||
{ NOP, 0xFF, 0, 0 }
|
{ NOP, 0xFF, 0, 0 }
|
||||||
};
|
};
|
||||||
static uint8_t outputs_state[N_OUTPUTS];
|
static uint8_t outputs_state[N_OUTPUTS];
|
||||||
@ -69,10 +69,10 @@ static event_t tx_events[N_EVENTS] PROGMEM={
|
|||||||
{ 8, 0x03, 0x03, TOGGLE, 0x08},
|
{ 8, 0x03, 0x03, TOGGLE, 0x08},
|
||||||
{ 11, 0x03, 0x01, ON, 0x01},
|
{ 11, 0x03, 0x01, ON, 0x01},
|
||||||
{ 12, 0x03, 0x01, ON, 0x02},
|
{ 12, 0x03, 0x01, ON, 0x02},
|
||||||
{ 210, 0x03, 0xff, OFF, 0x04},
|
{ 210, 0x03, 0x03, OFF, 0x04},
|
||||||
{ 211, 0x03, 0xff, ON, 0x04},
|
{ 211, 0x03, 0x03, ON, 0x04},
|
||||||
{ 220, 0x03, 0xff, OFF, 0x05},
|
{ 220, 0x03, 0x03, OFF, 0x05},
|
||||||
{ 221, 0x03, 0xff, ON, 0x05},
|
{ 221, 0x03, 0x03, ON, 0x05},
|
||||||
{ 255, 0x03, 0xff, OFF, 0x09},
|
{ 255, 0x03, 0xff, OFF, 0x09},
|
||||||
{ 254, 0x03, 0xff, ON, 0x09},
|
{ 254, 0x03, 0xff, ON, 0x09},
|
||||||
{ 10, 0x03, 0xff, TOGGLE, 0x01},
|
{ 10, 0x03, 0xff, TOGGLE, 0x01},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user