From 584c263eaa9b66824645d19e03dd32f4d0b44446 Mon Sep 17 00:00:00 2001 From: mwaiblinger Date: Thu, 28 Mar 2019 12:47:54 +0100 Subject: [PATCH 1/2] more tools --- tools/data2obj.py | 41 +++++++++++++++++++++++++++++++++++ tools/obj2data.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100755 tools/data2obj.py create mode 100755 tools/obj2data.py diff --git a/tools/data2obj.py b/tools/data2obj.py new file mode 100755 index 0000000..14821ac --- /dev/null +++ b/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)) diff --git a/tools/obj2data.py b/tools/obj2data.py new file mode 100755 index 0000000..d5ba35d --- /dev/null +++ b/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)) From 6a76239f40629ade93b9889c41ca75d8942cdf70 Mon Sep 17 00:00:00 2001 From: mwaiblinger Date: Wed, 3 Apr 2019 00:55:09 +0200 Subject: [PATCH 2/2] much better can bus writing --- src/CANNode.cpp | 82 +++++++++++++++++++++++++++--------------------- src/Node_3_def.h | 16 +++++----- 2 files changed, 55 insertions(+), 43 deletions(-) diff --git a/src/CANNode.cpp b/src/CANNode.cpp index 9fe03c6..0a77007 100644 --- a/src/CANNode.cpp +++ b/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; + Serial.print(F("Sending to CAN ID: ")); + Serial.print(txmsg.id); + Serial.print(F("DATA: ")); + Serial.println(txmsg.buf[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,10 +329,14 @@ void loop(void) } // if ((switches_state[s_idx] != readout) && (readout != 255 )) { if (switches_state[s_idx] != readout) { - tmp = readout ^ switches_state[s_idx]; - switches_state[s_idx] = readout; - action[0] = tmp & 0x08; - action[1] = tmp & 0x04; + 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 "); @@ -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