Browse Source

in progress

led
wiebel 8 years ago
parent
commit
6d52344012
  1. 191
      src/CANNode.cpp
  2. 30
      src/CANNode.h

191
src/CANNode.cpp

@ -12,9 +12,14 @@
#include "CANNode.h"
// Configuration
#define NODE_ID 0x01
// Misc
#define N_EVENTS 64 // size of events array
#define N_SWITCHES 64 // size of switch array
#define N_OUTPUTS 64
#define N_ACTIONS 64
#define DEBUG 0 // 1 for noisy serial
#define LED 17
#define RELAY1 0
@ -52,15 +57,72 @@
#define DS2406_BUF_LEN 10
// Definitions
static OW_switch_t switches[N_SWITCHES] PROGMEM=
{
{ 1, { 0x12, 0x5b, 0x27, 0x50, 0x0, 0x0, 0x0, 0x26 }, { 0x01, 0x02 } }
static uint8_t node_id PROGMEM= { NODE_ID };
static OW_switch_t switches[N_SWITCHES] PROGMEM={
{ 1, { 0x12, 0x5b, 0x27, 0x50, 0x0, 0x0, 0x0, 0x26 }, { 1, 9 } }
};
static uint8_t switches_state[N_SWITCHES];
static outputs_t outputs[N_OUTPUTS] PROGMEM={
{ GPIO, 0},
{ GPIO, 1},
{ GPIO, 23},
{ GPIO, 22},
{ GPIO, 17},
{ GPIO, 16},
{ GPIO, 9},
{ GPIO, 10},
{ NOP, 0xFF}
};
static uint8_t outputs_state[N_OUTPUTS];
// ID:
// 3 bit Prio, 2bit TYPE, 8bit DST, 8bit SRC, 8bit CMD/SEQ
// Type: 0: CMD, 1: FIRST, 2: CONT, 3: LAST
// CMD:
static event_t tx_events[N_EVENTS] PROGMEM={
{ 1, 0x03, 0xff, 0x03, 0x01},
{ 2, 0x03, 0xff, 0x03, 0x02},
{ 3, 0x03, 0xff, 0x03, 0x03},
{ 4, 0x03, 0xff, 0x03, 0x04},
{ 5, 0x03, 0xff, 0x03, 0x05},
{ 6, 0x03, 0xff, 0x03, 0x06},
{ 7, 0x03, 0xff, 0x03, 0x07},
{ 8, 0x03, 0xff, 0x03, 0x08},
{ 9, 0x03, 0xff, 0x03, 0x01},
{ 9, 0x03, 0xff, 0x03, 0x02},
{ 9, 0x03, 0xff, 0x03, 0x03},
{ 9, 0x03, 0xff, 0x03, 0x04},
{ 9, 0x03, 0xff, 0x03, 0x05},
{ 9, 0x03, 0xff, 0x03, 0x06},
{ 9, 0x03, 0xff, 0x03, 0x07},
{ 9, 0x03, 0xff, 0x03, 0x08},
static tx_event_t tx_events[N_EVENTS];
// { 0x01, 0x0CFF0103, 1, { 0xDE, 0xAD } },
// { 0x01, 0x0102DEAD, 2, { 0xDE, 0xAD } },
// { 0x02, 0x0204BEEF, 4, { 0xDE, 0xAD, 0xBE, 0xEF } }
};
static action_t action_map[N_ACTIONS] PROGMEM={
{1, 0};
{2, 1};
{3, 2};
{4, 3};
{5, 4};
{6, 5};
{7, 6};
{8, 7};
{9, 0};{9, 1};{9, 2};{9, 3};{9, 4};{9, 5};{9, 6};{9, 7};
}
// tx_events[0].id =0x01;
// tx_events[0].telegram.id = 0x0102DEAD;
// tx_events[0].telegram.len = 2;
// tx_events[0].telegram.buf[0] = 0xDE;
// tx_events[0].telegram.buf[1] = 0xAD;
// tx_events[1].id =0x02;
// tx_events[1].telegram.id = 0x0204BEEF;
// tx_events[1].telegram.len = 4;
// tx_events[1].telegram.buf[0] = 0xDE;
// tx_events[1].telegram.buf[1] = 0xAD;
// Initialisation
// Misc
@ -86,6 +148,7 @@ uint8_t buffer[DS2406_BUF_LEN];
uint8_t readout,trig_event,event_idx,tmp;
OneWire OW_1(OW_pin);
telegram_comp_t mesg_comp;
static CAN_message_t txmsg,rxmsg;
static uint8_t hex[17] = "0123456789abcdef";
@ -139,37 +202,42 @@ uint8_t toggle_Pin(uint8_t pin){
return digitalRead(pin);
}
uint32_t forgeid(event_t event){
return (event.prio<<26)+(event.dst<<16)+(NODE_ID<<8)+event.cmd;
}
telegram_comp_t parse_CAN (CAN_telegram_t mesg){
telegram_comp_t tmp;
tmp.prio=mesg.id>>26;
tmp.frametype=(mesg.id>>24) & 0x03;
tmp.dst=(mesg.id>>16) & 0xFF;
tmp.src=(mesg.id>>8) & 0xFF;
tmp.cmd=mesg.id & 0xFF;
tmp.length=mesg.length;
tmp.buff=mesg.buf;
return tmp;
}
void take_action (event_type type, uint8_t tag ){
for (uint_t i = 0; action_mapaction_map[i].tag != 0 ; i++) {
if ( action_mapaction_map[i].tag == tag ) {
switch ( type ) {
case OFF:
digitalWrite(outputs[action_mapaction_map[i].outputs_idx].address,LOW);
break;
case ON:
digitalWrite(outputs[action_mapaction_map[i].outputs_idx].address,HIGH);
break;
case TOGGLE:
toggle_Pin(outputs[action_mapaction_map[i].outputs_idx].address);
break;
}
}
}
// -------------------------------------------------------------
void setup(void)
{
// Misc
// Most clumsy way later to be put in eeprom or s/th
// switches[0].id = 1;
// switches[0].addr[0] = 0x12,
// switches[0].addr[1] = 0x5b;
// switches[0].addr[2] = 0x27;
// switches[0].addr[3] = 0x50;
// switches[0].addr[4] = 0x0;
// switches[0].addr[5] = 0x0;
// switches[0].addr[6] = 0x0;
// switches[0].addr[7] = 0x26;
// switches[0].event_id[0] = 0x01;
// switches[0].event_id[1] = 0x02;
tx_events[0].id =0x01;
tx_events[0].telegram.id = 0x0102DEAD;
tx_events[0].telegram.len = 2;
tx_events[0].telegram.buf[0] = 0xDE;
tx_events[0].telegram.buf[1] = 0xAD;
tx_events[1].id =0x02;
tx_events[1].telegram.id = 0x0204BEEF;
tx_events[1].telegram.len = 4;
tx_events[1].telegram.buf[0] = 0xDE;
tx_events[1].telegram.buf[1] = 0xAD;
tx_events[1].telegram.buf[2] = 0xBE;
tx_events[1].telegram.buf[3] = 0xEF;
//{ 0x02040608, 2, { 0xbe, 0xef }}};
Serial.begin(115200);
pinMode(led, OUTPUT);
@ -182,6 +250,12 @@ void setup(void)
txmsg.ext = 1;
txmsg.timeout = 100;
// outputs
for (size_t i = 0; outputs[i].type != NOP; i++) {
if (outputs[i].type == GPIO || outputs[i].type == PWM){
pinMode(outputs[i].address, OUTPUT);
}
}
delay(100);
Serial.println(F("Hello Teensy 3.2 CANode awakes."));
@ -220,14 +294,14 @@ void loop(void)
if (action[0]) {
Serial.print("pioA toggled");
digitalWrite(led, !digitalRead(led));
trig_event = switches[0].event_id[0];
trig_event = switches[0].event_tag[0];
// might need queuing
event_idx = 0;
}
if (action[1]) {
Serial.print("pioB toggled");
digitalWrite(led, !digitalRead(led));
trig_event= switches[0].event_id[1];
trig_event= switches[0].event_tag[1];
event_idx = 0;
}
}
@ -240,14 +314,15 @@ void loop(void)
}
}
if ( txmsg.len == 0 && trig_event != 0 ) {
if ( tx_events[event_idx].id == trig_event ) {
txmsg.id = tx_events[event_idx].telegram.id;
txmsg.len = tx_events[event_idx].telegram.len;
for (uint i=0;i<txmsg.len;i++){
txmsg.buf[i] = tx_events[event_idx].telegram.buf[i];
}
}
if ( tx_events[event_idx].id == 0) { trig_event = 0; }
if ( tx_events[event_idx].tag == trig_event ) {
txmsg.id = forgeid(tx_events[event_idx]);
txmsg.len = 1;
txmsg.buf[0]= tx_events[event_idx].target_id;
// for (uint i=0;i<txmsg.len;i++){
// txmsg.buf[i] = tx_events[event_idx].telegram.buf[i];
// }
}
if ( tx_events[event_idx].tag == 0) { trig_event = 0; }
event_idx++;
}
// if not time-delayed, read CAN messages and print 1st byte
@ -273,14 +348,34 @@ void loop(void)
Serial.print(rxmsg.buf[i],HEX);
}
Serial.print("\n");
if (rxmsg.id ==0x0102DEAD) {
state=toggle_Pin(led);
txmsg.id=0xDEADBEEF;
txmsg.len=2;
txmsg.buf[0]=led;
txmsg.buf[1]=state;
mesg_comp= parse_CAN(rxmesg)
if (mesg_comp.dst == 0xFF || mesg_comp.dst == NODE_ID ) {
switch (mesg_comp.cmd) {
// OFF
case 0x00 :
take_action(OFF, mesg_comp.buf[0]);
break;
// ON
case 0x01 :
take_action(ON, mesg_comp.buf[0]);
break;
// VALUE
case 0x02 :
break;
// TOGGLE
case 0x03:
take_action(TOGGLE, mesg_comp.buf[0]);
break;
// if (rxmsg.id == 0x0102DEAD) {
// state=toggle_Pin(led);
// txmsg.id=0xDEADBEEF;
// txmsg.len=2;
// txmsg.buf[0]=led;
// txmsg.buf[1]=state;
}
//#endif
}
rxCount = 0;
}
txTimer = 100;//milliseconds

30
src/CANNode.h

@ -1,6 +1,10 @@
#include <Arduino.h>
enum out_type {gpio, pwm, ow, i2c, spi, ws2811, dmx};
enum out_type { GPIO, PWM, OW, I2C, SPI, WS2811, DMX };
enum event_type { LOCAL, SEND};
enum action_type { OFF, ON, TOGGLE, VALUE };
enum telegram_type { ALERT, EVENT, NOTIFY, INFO };
typedef struct CAN_telegram_t {
uint32_t id;
@ -11,14 +15,30 @@ typedef struct CAN_telegram_t {
typedef struct OW_switch_t {
uint8_t nick;
uint8_t addr[8];
uint8_t event_id[2];
uint8_t event_tag[2];
} OW_switch_t;
typedef struct tx_event_t {
uint8_t id;
CAN_telegram_t telegram;
typedef struct event_t {
uint8_t tag;
uint8_t prio;
uint8_t dst;
uint8_t cmd;
uint8_t target_id;
// uint8_t data[2];
// CAN_telegram_t telegram;
} event_t;
typedef struct telegram_dict_t {
CAN_telegram_t telegram;
uint8_t tag;
} telegram_dict_ttelegram_dict_t;
typedef struct action_t {
uint8_t id;
uint8_t tag;
uint8_t outputs_id;
} action_t;
typedef struct outputs_t {
uint8_t id;
out_type type;

Loading…
Cancel
Save