Browse Source

Merge branch 'master' of git.wiebel.org:OSS/CANNode

master
root 6 years ago
parent
commit
896580a239
  1. 82
      src/CANNode.cpp
  2. 16
      src/Node_3_def.h
  3. 41
      tools/data2obj.py
  4. 55
      tools/obj2data.py

82
src/CANNode.cpp

@ -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;
} // }
} }

16
src/Node_3_def.h

@ -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},

41
tools/data2obj.py

@ -0,0 +1,41 @@
#! /usr/bin/env python
import sys
msg_id=int(str(sys.argv[1]),0) & 0x1FFFFFFF
msg_prio=msg_id>>26
msg_type=(msg_id>>24) & 0x03
msg_dst=(msg_id>>16) & 0xFF
msg_src=(msg_id>>8) & 0xFF
msg_cmd=msg_id & 0xFF
#print('id: {0:b}'.format(msg_id))
print('id: {0:#010x} {0:#031b}'.format(msg_id ,msg_id))
#print("id:", hex(msg_id), format(40,str(bin(msg_id))))
print('prio: {0:#03x} {0:#04b}'.format(msg_prio))
print('type: {0:#03x} {0:#04b}'.format(msg_type))
print('dst: {0:#04x} {0:#010b}'.format(msg_dst))
print('src: {0:#04x} {0:#010b}'.format(msg_src))
print('cmd: {0:#04x} {0:#010b}'.format(msg_cmd))
nmsg_prio=msg_id>>26
nmsg_type=(msg_id>>24) & 0x03
nmsg_src=(msg_id>>18) & 0x3F
nmsg_dst=(msg_id>>12) & 0x3F
nmsg_targ=(msg_id>>6) & 0x3F
nmsg_cmd=msg_id & 0x1F
print('New schema')
#print('id: {0:b}'.format(msg_id))
print('id: {0:#010x} {0:#031b}'.format(msg_id))
#print("id:", hex(msg_id), format(40,str(bin(msg_id))))
print('prio: {0:#04x} {0:#04b}'.format(nmsg_prio))
print('type: {0:#04x} {0:#04b}'.format(nmsg_type))
print('src: {0:#04x} {0:#08b}'.format(nmsg_src))
print('dst: {0:#04x} {0:#08b}'.format(nmsg_dst))
print('target: {0:#04x} {0:#08b}'.format(nmsg_targ))
print('cmd: {0:#04x} {0:#07b}'.format(nmsg_cmd))

55
tools/obj2data.py

@ -0,0 +1,55 @@
#! /usr/bin/env python
import sys
import json
#stuff = json.loads(sys.argv[1])
#data = json.load(sys.stdin)
#print(json.dumps(sys.stdin, sort_keys=True, indent=4))
###print str(sys.argv[1])
#print(stuff['foo'])
#print(json.dumps(data, indent=4))
#msg_id=int(str(sys.argv[1]),0)
#msg_prio=msg_id>>26
#msg_type=(msg_id>>24) & 0x03
#msg_dst=(msg_id>>16) & 0xFF
#msg_src=(msg_id>>8) & 0xFF
#msg_cmd=msg_id & 0xFF
#print "id:", hex(msg_id), bin(msg_id)
#print "prio:", hex(msg_prio), bin(msg_prio)
#print "type:", hex(msg_type), bin(msg_type)
#print "dst:", hex(msg_dst), bin(msg_dst)
#print "src:", hex(msg_src), bin(msg_src)
#print "cmd:", hex(msg_cmd), bin(msg_cmd)
import sys
import simplejson as json
def main(args):
try:
inputFile = open(args[1])
input = json.load(inputFile)
inputFile.close()
except IndexError:
usage()
return False
if len(args) < 3:
print json.dumps(input, sort_keys = False, indent = 10)
else:
outputFile = open(args[2], "w")
json.dump(input, outputFile, sort_keys = False, indent = 4)
outputFile.close()
return True
def usage():
print __doc__
if __name__ == "__main__":
sys.exit(not main(sys.argv))
Loading…
Cancel
Save