Skip to content

Commit dfcd3d4

Browse files
committed
Simplify gateway code (handles interrupts internally) and remove external dependencies. Ledmode is now always "enabled".
Remove unused libraries. Add default CS/CE pin to Config.h Add new variebles for voltage and current. Add new internal command sent to controller when gateway is ready and online
1 parent 8a6b092 commit dfcd3d4

File tree

21 files changed

+52
-2496
lines changed

21 files changed

+52
-2496
lines changed

SerialGateway/SerialGateway.ino

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* Copyright (C) 2013 Henrik Ekblad <[email protected]>
33
*
44
* This program is free software; you can redistribute it and/or
@@ -22,16 +22,14 @@
2222
*/
2323

2424
#include <SPI.h>
25-
#include <MsTimer2.h>
26-
#include <PinChangeInt.h>
2725
#include <MyGateway.h>
2826
#include <stdarg.h>
2927

3028
#define INCLUSION_MODE_TIME 1 // Number of minutes inclusion mode is enabled
3129
#define INCLUSION_MODE_PIN 3 // Digital pin used for inclusion mode button
3230

3331

34-
MyGateway gw(9, 10, INCLUSION_MODE_TIME, INCLUSION_MODE_PIN, 6, 5, 4);
32+
MyGateway gw(DEFAULT_CE_PIN, DEFAULT_CS_PIN, INCLUSION_MODE_TIME, INCLUSION_MODE_PIN, 6, 5, 4);
3533

3634
char inputString[MAX_RECEIVE_LENGTH] = ""; // A string to hold incoming commands from serial/ethernet interface
3735
int inputPos = 0;
@@ -40,34 +38,15 @@ boolean commandComplete = false; // whether the string is complete
4038
void setup()
4139
{
4240
gw.begin();
43-
44-
// C++ classes and interrupts really sucks. Need to attach interrupt
45-
// outside thw Gateway class due to language shortcomings! Gah!
46-
47-
if (gw.isLedMode()) {
48-
// Add led timer interrupt
49-
MsTimer2::set(300, ledTimersInterrupt);
50-
MsTimer2::start();
51-
// Add interrupt for inclustion button to pin
52-
PCintPort::attachInterrupt(INCLUSION_MODE_PIN, startInclusionInterrupt, RISING);
53-
}
5441
}
5542

5643
void loop()
5744
{
5845
gw.processRadioMessage();
5946
checkSerialInput();
60-
6147
}
6248

6349

64-
void startInclusionInterrupt() {
65-
gw.startInclusionInterrupt();
66-
}
67-
68-
void ledTimersInterrupt() {
69-
gw.ledTimersInterrupt();
70-
}
7150

7251
void checkSerialInput() {
7352
if (commandComplete) {

libraries/MsTimer2/examples/FlashLed/FlashLed.pde

Lines changed: 0 additions & 42 deletions
This file was deleted.

libraries/MsTimer2/keywords.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.

libraries/MySensors/MyConfig.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#define RF24_PA_LEVEL_GW RF24_PA_LEVEL //Gateway PA Level, defaults to Sensor net PA Level. Tune here if using an amplified nRF2401+ in your gateway.
1111
#define BASE_RADIO_ID ((uint64_t)0xA8A8E1FC00LL) // This is also act as base value for sensor nodeId addresses. Change this (or channel) if you have more than one sensor network.
1212

13+
// MySensors online examples defaults
14+
#define DEFAULT_CE_PIN 9
15+
#define DEFAULT_CS_PIN 10
16+
1317

1418
/***
1519
* Enable/Disable debug logging

libraries/MySensors/MyGateway.cpp

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,29 @@
1010
*/
1111

1212
#include "MyGateway.h"
13+
#include "utility/MsTimer2.h"
14+
#include "utility/PinChangeInt.h"
1315

1416

15-
MyGateway::MyGateway(uint8_t _cepin, uint8_t _cspin, uint8_t _inclusion_time) : MySensor(_cepin, _cspin) {
16-
ledMode = false;
17-
inclusionTime = _inclusion_time;
18-
}
17+
uint8_t pinRx;
18+
uint8_t pinTx;
19+
uint8_t pinEr;
20+
boolean buttonTriggeredInclusion;
21+
volatile uint8_t countRx;
22+
volatile uint8_t countTx;
23+
volatile uint8_t countErr;
24+
boolean inclusionMode; // Keeps track on inclusion mode
25+
1926

2027
MyGateway::MyGateway(uint8_t _cepin, uint8_t _cspin, uint8_t _inclusion_time, uint8_t _inclusion_pin, uint8_t _rx, uint8_t _tx, uint8_t _er) : MySensor(_cepin, _cspin) {
21-
ledMode = true;
2228
pinInclusion = _inclusion_pin;
2329
inclusionTime = _inclusion_time;
2430
pinRx = _rx;
2531
pinTx = _tx;
2632
pinEr = _er;
2733
}
2834

35+
2936
void MyGateway::begin(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate, void (*inDataCallback)(char *)) {
3037
Serial.begin(BAUD_RATE);
3138
repeaterMode = true;
@@ -47,47 +54,48 @@ void MyGateway::begin(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e da
4754
countTx = 0;
4855
countErr = 0;
4956

50-
if (ledMode) {
51-
// Setup led pins
52-
pinMode(pinRx, OUTPUT);
53-
pinMode(pinTx, OUTPUT);
54-
pinMode(pinEr, OUTPUT);
55-
digitalWrite(pinRx, LOW);
56-
digitalWrite(pinTx, LOW);
57-
digitalWrite(pinEr, LOW);
57+
// Setup led pins
58+
pinMode(pinRx, OUTPUT);
59+
pinMode(pinTx, OUTPUT);
60+
pinMode(pinEr, OUTPUT);
61+
digitalWrite(pinRx, LOW);
62+
digitalWrite(pinTx, LOW);
63+
digitalWrite(pinEr, LOW);
5864

59-
// Setup digital in that triggers inclusion mode
60-
pinMode(pinInclusion, INPUT);
61-
digitalWrite(pinInclusion, HIGH);
65+
// Setup digital in that triggers inclusion mode
66+
pinMode(pinInclusion, INPUT);
67+
digitalWrite(pinInclusion, HIGH);
6268

63-
// Set initial state of leds
64-
digitalWrite(pinRx, HIGH);
65-
digitalWrite(pinTx, HIGH);
66-
digitalWrite(pinEr, HIGH);
69+
// Set initial state of leds
70+
digitalWrite(pinRx, HIGH);
71+
digitalWrite(pinTx, HIGH);
72+
digitalWrite(pinEr, HIGH);
6773

68-
}
6974

7075
// Start up the radio library
7176
setupRadio(paLevel, channel, dataRate);
7277
RF24::openReadingPipe(WRITE_PIPE, BASE_RADIO_ID);
7378
RF24::openReadingPipe(CURRENT_NODE_PIPE, BASE_RADIO_ID);
7479
RF24::startListening();
7580

76-
// Send startup log message on serial
77-
serial(PSTR("0;0;%d;0;%d;Arduino startup complete.\n"), C_INTERNAL, I_LOG_MESSAGE);
78-
79-
}
81+
// Add led timer interrupt
82+
MsTimer2::set(300, ledTimersInterrupt);
83+
MsTimer2::start();
8084

85+
// Add interrupt for inclusion button to pin
86+
PCintPort::attachInterrupt(pinInclusion, startInclusionInterrupt, RISING);
8187

82-
boolean MyGateway::isLedMode() {
83-
return ledMode;
88+
// Send startup log message on serial
89+
serial(PSTR("0;0;%d;0;%d;Gateway startup complete.\n"), C_INTERNAL, I_GATEWAY_READY);
8490
}
8591

86-
void MyGateway::startInclusionInterrupt() {
92+
93+
void startInclusionInterrupt() {
8794
buttonTriggeredInclusion = true;
8895
}
8996

9097

98+
9199
void MyGateway::checkButtonTriggeredInclusion() {
92100
if (buttonTriggeredInclusion) {
93101
// Ok, someone pressed the inclusion button on the gateway
@@ -243,7 +251,7 @@ void MyGateway::serial(MyMessage &msg) {
243251
}
244252

245253

246-
void MyGateway::ledTimersInterrupt() {
254+
void ledTimersInterrupt() {
247255
if(countRx && countRx != 255) {
248256
// switch led on
249257
digitalWrite(pinRx, LOW);

libraries/MySensors/MyGateway.h

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
#include "MySensor.h"
1616

17-
#define MAX_RECEIVE_LENGTH 100 // Max buffersize needed for messages coming from vera
18-
#define MAX_SEND_LENGTH 120 // Max buffersize needed for messages coming from vera
17+
#define MAX_RECEIVE_LENGTH 100 // Max buffersize needed for messages coming from controller
18+
#define MAX_SEND_LENGTH 120 // Max buffersize needed for messages destined for controller
1919

2020
class MyGateway : public MySensor
2121
{
@@ -36,43 +36,27 @@ class MyGateway : public MySensor
3636
* @param _er Digital pin for error led
3737
*
3838
*/
39-
MyGateway(uint8_t _cepin=9, uint8_t _cspin=10, uint8_t _inclusion_time = 1);
40-
MyGateway(uint8_t _cepin, uint8_t _cspin, uint8_t _inclusion_time, uint8_t _inclusion_pin, uint8_t _rx, uint8_t _tx, uint8_t _er);
39+
MyGateway(uint8_t _cepin=DEFAULT_CE_PIN, uint8_t _cspin=DEFAULT_CS_PIN, uint8_t _inclusion_time = 1, uint8_t _inclusion_pin = 3, uint8_t _rx=6, uint8_t _tx=5, uint8_t _er=4);
4140

4241
/* Use this and pass a function that should be called when you want to process commands that arrive from radio network */
4342
void begin(rf24_pa_dbm_e paLevel=RF24_PA_LEVEL_GW, uint8_t channel=RF24_CHANNEL, rf24_datarate_e dataRate=RF24_DATARATE, void (*dataCallback)(char *)=NULL);
4443

4544
void processRadioMessage();
4645
void parseAndSend(char *inputString);
47-
boolean isLedMode();
48-
void ledTimersInterrupt();
49-
void startInclusionInterrupt();
5046

5147
private:
5248
char convBuf[MAX_PAYLOAD*2+1];
5349
char serialBuffer[MAX_SEND_LENGTH]; // Buffer for building string when sending data to vera
5450
unsigned long inclusionStartTime;
55-
boolean inclusionMode; // Keeps track on inclusion mode
56-
boolean buttonTriggeredInclusion;
57-
volatile uint8_t countRx;
58-
volatile uint8_t countTx;
59-
volatile uint8_t countErr;
60-
boolean ledMode;
6151
boolean useWriteCallback;
6252
void (*dataCallback)(char *);
63-
64-
6553
uint8_t pinInclusion;
6654
uint8_t inclusionTime;
67-
uint8_t pinRx;
68-
uint8_t pinTx;
69-
uint8_t pinEr;
7055

7156
uint8_t h2i(char c);
7257

7358
void serial(const char *fmt, ... );
7459
void serial(MyMessage &msg);
75-
void interruptStartInclusion();
7660
void checkButtonTriggeredInclusion();
7761
void setInclusionMode(boolean newMode);
7862
void checkInclusionFinished();
@@ -82,6 +66,7 @@ class MyGateway : public MySensor
8266
void errBlink(uint8_t cnt);
8367
};
8468

85-
69+
void ledTimersInterrupt();
70+
void startInclusionInterrupt();
8671

8772
#endif

libraries/MySensors/MyMessage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ typedef enum {
3939
V_IMPEDANCE, V_ARMED, V_TRIPPED, V_WATT, V_KWH, V_SCENE_ON, V_SCENE_OFF,
4040
V_HEATER, V_HEATER_SW, V_LIGHT_LEVEL, V_VAR1, V_VAR2, V_VAR3, V_VAR4, V_VAR5,
4141
V_UP, V_DOWN, V_STOP, V_IR_SEND, V_IR_RECEIVE, V_FLOW, V_VOLUME, V_LOCK_STATUS,
42-
V_DUST_LEVEL
42+
V_DUST_LEVEL, V_VOLTAGE, V_CURRENT
4343
} data;
4444

4545
// Type of internal messages (for internal messages)
4646
typedef enum {
4747
I_BATTERY_LEVEL, I_TIME, I_VERSION, I_ID_REQUEST, I_ID_RESPONSE,
4848
I_INCLUSION_MODE, I_CONFIG, I_FIND_PARENT, I_FIND_PARENT_RESPONSE,
4949
I_LOG_MESSAGE, I_CHILDREN, I_SKETCH_NAME, I_SKETCH_VERSION,
50-
I_REBOOT
50+
I_REBOOT, I_GATEWAY_READY
5151
} internal;
5252

5353
// Type of sensor (for presentation message)

libraries/MySensors/MySensor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ class MySensor : public RF24
8888
*
8989
* Creates a new instance of Sensor class.
9090
*
91-
* @param _cepin The pin attached to RF24 Chip Enable on the RF module (defualt 9)
91+
* @param _cepin The pin attached to RF24 Chip Enable on the RF module (default 9)
9292
* @param _cspin The pin attached to RF24 Chip Select (default 10)
9393
*/
94-
MySensor(uint8_t _cepin=9, uint8_t _cspin=10);
94+
MySensor(uint8_t _cepin=DEFAULT_CE_PIN, uint8_t _cspin=DEFAULT_CS_PIN);
9595

9696
/**
9797
* Begin operation of the MySensors library

0 commit comments

Comments
 (0)