|
@ -9,11 +9,12 @@ |
|
|
#include <Metro.h> |
|
|
#include <Metro.h> |
|
|
#include <FlexCAN.h> |
|
|
#include <FlexCAN.h> |
|
|
#include <OneWire.h> |
|
|
#include <OneWire.h> |
|
|
|
|
|
#include <FastLED.h> |
|
|
#include "CANNode.h" |
|
|
#include "CANNode.h" |
|
|
|
|
|
|
|
|
// For Node definition:
|
|
|
// For Node definition:
|
|
|
|
|
|
|
|
|
#include "Node_3_def.h" |
|
|
#include "Node_2_def.h" |
|
|
|
|
|
|
|
|
// Metro ticks in ms
|
|
|
// Metro ticks in ms
|
|
|
#define METRO_CAN_tick 1 |
|
|
#define METRO_CAN_tick 1 |
|
@ -50,9 +51,12 @@ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t led = LED; |
|
|
uint8_t led = ONBOARD_LED; |
|
|
uint8_t state; |
|
|
uint8_t state; |
|
|
uint8_t pin_state; |
|
|
uint8_t pin_state; |
|
|
|
|
|
uint8_t led_current_value; |
|
|
|
|
|
uint8_t led_last_value; |
|
|
|
|
|
int serial_in; |
|
|
bool action[2]; |
|
|
bool action[2]; |
|
|
// Metro
|
|
|
// Metro
|
|
|
Metro METRO_CAN = Metro(METRO_CAN_tick); |
|
|
Metro METRO_CAN = Metro(METRO_CAN_tick); |
|
@ -69,6 +73,8 @@ OneWire OW_1(OW_pin); |
|
|
|
|
|
|
|
|
telegram_comp_t mesg_comp; |
|
|
telegram_comp_t mesg_comp; |
|
|
|
|
|
|
|
|
|
|
|
// event info = { 1, 0x03, 0x01, OFF, 0x01};
|
|
|
|
|
|
|
|
|
static CAN_message_t txmsg,rxmsg; |
|
|
static CAN_message_t txmsg,rxmsg; |
|
|
static uint8_t hex[17] = "0123456789abcdef"; |
|
|
static uint8_t hex[17] = "0123456789abcdef"; |
|
|
|
|
|
|
|
@ -76,6 +82,9 @@ int txCount,rxCount; |
|
|
unsigned int txTimer,rxTimer; |
|
|
unsigned int txTimer,rxTimer; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CRGB leds[NUM_LEDS]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Functions ------------------------------^-------------------------
|
|
|
// Functions ------------------------------^-------------------------
|
|
|
void PrintBytes(uint8_t* addr, uint8_t count, bool newline=0) { |
|
|
void PrintBytes(uint8_t* addr, uint8_t count, bool newline=0) { |
|
|
for (uint8_t i = 0; i < count; i++) { |
|
|
for (uint8_t i = 0; i < count; i++) { |
|
@ -122,7 +131,7 @@ uint8_t toggle_Pin(uint8_t pin){ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
uint32_t forgeid(event_t event){ |
|
|
uint32_t forgeid(event_t event){ |
|
|
return (event.prio<<26)+(event.dst<<16)+(NODE_ID<<8)+event.cmd; |
|
|
return (event.prio<<26)+(event.dst<<18)+(NODE_ID<<8)+event.cmd; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
telegram_comp_t parse_CAN(CAN_message_t mesg){ |
|
|
telegram_comp_t parse_CAN(CAN_message_t mesg){ |
|
@ -154,35 +163,94 @@ void send_event (uint8_t trig_event){ |
|
|
txmsg.len = 0; |
|
|
txmsg.len = 0; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
void switch_leds (uint8_t value) { |
|
|
|
|
|
led_last_value = led_current_value; |
|
|
|
|
|
// FastLED.setTemperature( TEMP );
|
|
|
|
|
|
// FastLED.setBrightness( BRIGHTNESS );
|
|
|
|
|
|
memset8( leds, value, NUM_LEDS * sizeof(CRGB)); |
|
|
|
|
|
led_current_value = value; |
|
|
|
|
|
FastLED.show(); |
|
|
|
|
|
} |
|
|
|
|
|
void toggle_leds ( void ) { |
|
|
|
|
|
if ( led_current_value != 0) { |
|
|
|
|
|
switch_leds(0); |
|
|
|
|
|
} else { |
|
|
|
|
|
if ( led_current_value == led_last_value){ |
|
|
|
|
|
switch_leds(255); |
|
|
|
|
|
} else { |
|
|
|
|
|
switch_leds(led_last_value); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
void take_action (action_type type, uint8_t tag ){ |
|
|
void take_action (action_type type, uint8_t tag ){ |
|
|
for (uint8_t i = 0; action_map[i].tag != 0 ; i++) { |
|
|
for (uint8_t i = 0; action_map[i].tag != 0 ; i++) { |
|
|
if ( action_map[i].tag == tag ) { |
|
|
if ( action_map[i].tag == tag ) { |
|
|
switch ( type ) { |
|
|
switch ( type ) { |
|
|
case OFF: |
|
|
case OFF: |
|
|
|
|
|
switch ( outputs[action_map[i].outputs_idx].type ){ |
|
|
|
|
|
case GPIO: |
|
|
digitalWrite(outputs[action_map[i].outputs_idx].address,LOW ^ outputs[action_map[i].outputs_idx].invert); |
|
|
digitalWrite(outputs[action_map[i].outputs_idx].address,LOW ^ outputs[action_map[i].outputs_idx].invert); |
|
|
|
|
|
break; |
|
|
|
|
|
case LED: |
|
|
|
|
|
switch_leds(0); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
Serial.print(F("Switching OFF Output: ")); |
|
|
Serial.print(F("Switching OFF Output: ")); |
|
|
Serial.println(action_map[i].outputs_idx); |
|
|
Serial.println(action_map[i].outputs_idx); |
|
|
break; |
|
|
break; |
|
|
case ON: |
|
|
case ON: |
|
|
|
|
|
switch ( outputs[action_map[i].outputs_idx].type ){ |
|
|
|
|
|
case GPIO: |
|
|
|
|
|
digitalWrite(outputs[action_map[i].outputs_idx].address,HIGH ^ outputs[action_map[i].outputs_idx].invert); |
|
|
|
|
|
break; |
|
|
|
|
|
case LED: |
|
|
|
|
|
switch_leds(255); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
digitalWrite(outputs[action_map[i].outputs_idx].address, HIGH ^ outputs[action_map[i].outputs_idx].invert); |
|
|
digitalWrite(outputs[action_map[i].outputs_idx].address, HIGH ^ outputs[action_map[i].outputs_idx].invert); |
|
|
Serial.print(F("Switching ON Output: ")); |
|
|
Serial.print(F("Switching ON Output: ")); |
|
|
Serial.println(action_map[i].outputs_idx); |
|
|
Serial.println(action_map[i].outputs_idx); |
|
|
break; |
|
|
break; |
|
|
case TOGGLE: |
|
|
case TOGGLE: |
|
|
pin_state = toggle_Pin(outputs[action_map[i].outputs_idx].address); |
|
|
|
|
|
Serial.print(F("Toggeling Output: ")); |
|
|
Serial.print(F("Toggeling Output: ")); |
|
|
Serial.print(action_map[i].outputs_idx); |
|
|
Serial.print(action_map[i].outputs_idx); |
|
|
Serial.print(F(" to new state: ")); |
|
|
Serial.print(F(" to new state: ")); |
|
|
|
|
|
switch ( outputs[action_map[i].outputs_idx].type ){ |
|
|
|
|
|
case GPIO: |
|
|
|
|
|
pin_state = toggle_Pin(outputs[action_map[i].outputs_idx].address); |
|
|
Serial.println(pin_state ^ outputs[action_map[i].outputs_idx].invert); |
|
|
Serial.println(pin_state ^ outputs[action_map[i].outputs_idx].invert); |
|
|
break; |
|
|
break; |
|
|
|
|
|
case LED: |
|
|
|
|
|
toggle_leds(); |
|
|
|
|
|
Serial.println(led_current_value); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
break; |
|
|
case VALUE: |
|
|
case VALUE: |
|
|
Serial.println(F("TBD")); |
|
|
switch ( outputs[action_map[i].outputs_idx].type ){ |
|
|
|
|
|
case GPIO: |
|
|
|
|
|
if (VALUE == 0) { |
|
|
|
|
|
digitalWrite(outputs[action_map[i].outputs_idx].address,LOW ^ outputs[action_map[i].outputs_idx].invert); |
|
|
|
|
|
} else { |
|
|
|
|
|
digitalWrite(outputs[action_map[i].outputs_idx].address, HIGH ^ outputs[action_map[i].outputs_idx].invert); |
|
|
|
|
|
} |
|
|
|
|
|
break; |
|
|
|
|
|
case LED: |
|
|
|
|
|
switch_leds(VALUE); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
Serial.print(F("Setting Output: ")); |
|
|
|
|
|
Serial.print(action_map[i].outputs_idx); |
|
|
|
|
|
Serial.print(F(" to value: ")); |
|
|
|
|
|
Serial.println(VALUE); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
// -------------------------------------------------------------
|
|
|
void setup(void) |
|
|
void setup(void) |
|
|
{ |
|
|
{ |
|
@ -198,6 +266,11 @@ void setup(void) |
|
|
CANbus.begin(); |
|
|
CANbus.begin(); |
|
|
txmsg.ext = 1; |
|
|
txmsg.ext = 1; |
|
|
txmsg.timeout = 100; |
|
|
txmsg.timeout = 100; |
|
|
|
|
|
// FastLED
|
|
|
|
|
|
//delay(3000); // sanity delay
|
|
|
|
|
|
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); |
|
|
|
|
|
FastLED.setBrightness( BRIGHTNESS ); |
|
|
|
|
|
FastLED.setTemperature( TEMP ); |
|
|
|
|
|
|
|
|
// outputs
|
|
|
// outputs
|
|
|
for (size_t i = 0; outputs[i].type != NOP; i++) { |
|
|
for (size_t i = 0; outputs[i].type != NOP; i++) { |
|
@ -366,5 +439,24 @@ void loop(void) |
|
|
txmsg.buf[0]++; |
|
|
txmsg.buf[0]++; |
|
|
txmsg.len = 0; |
|
|
txmsg.len = 0; |
|
|
} |
|
|
} |
|
|
|
|
|
if (Serial.available() > 0) { |
|
|
|
|
|
// read the incoming byte:
|
|
|
|
|
|
serial_in = Serial.read(); |
|
|
|
|
|
switch (serial_in) { |
|
|
|
|
|
case 0x31 : take_action(TOGGLE, 1); break; |
|
|
|
|
|
case 0x32 : take_action(TOGGLE, 2); break; |
|
|
|
|
|
case 0x33 : take_action(TOGGLE, 3); break; |
|
|
|
|
|
case 0x34 : take_action(TOGGLE, 4); break; |
|
|
|
|
|
case 0x35 : take_action(TOGGLE, 5); break; |
|
|
|
|
|
case 0x36 : take_action(TOGGLE, 6); break; |
|
|
|
|
|
case 0x37 : take_action(TOGGLE, 7); break; |
|
|
|
|
|
case 0x38 : take_action(TOGGLE, 8); break; |
|
|
|
|
|
case 0x39 : take_action(TOGGLE, 10); break; |
|
|
|
|
|
// case 0x38 : digitalWrite(13, 1); break;
|
|
|
|
|
|
// case 0x39 : digitalWrite(13, 0); break;
|
|
|
|
|
|
} |
|
|
|
|
|
// say what you got:
|
|
|
|
|
|
Serial.print("I received: "); |
|
|
|
|
|
Serial.println(serial_in, HEX); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|