-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSmartController.ino
More file actions
144 lines (115 loc) · 3.43 KB
/
SmartController.ino
File metadata and controls
144 lines (115 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//#define UNIT_TEST_ENABLE //toggle unit testing
/**
* This is the firmware of Xlight SmartController based on Photon/P1 MCU.
* There are two Protocol Domains:
* 1. MySensor Protocol: Radio network and BLE communication
* 2. MQTT Protocol: LAN & WAN
*
* Created by Baoshi Sun <bs.sun@datatellit.com>
* Copyright (C) 2015-2016 DTIT
* Full contributor list:
*
* Documentation:
* Support Forum:
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*******************************
*
* REVISION HISTORY
* Version 1.0 - Created by Baoshi Sun <bs.sun@datatellit.com>
*
* DESCRIPTION
* 1. Setup program skeleton
* 2. Define major interfaces
* 3. Define basic data structures
* 4. Define fundamental status machine
* 5. Define primary messages
* 6. Define cloud variables and Functions
*
* ToDo:
* 1. Include MySensor lib
* 2. Include MQTT lib
**/
#ifdef UNIT_TEST_ENABLE
#include "test.ino"
#else
//------------------------------------------------------------------
// Include dependency packages below
//------------------------------------------------------------------
#include "application.h"
#include "xlxConfig.h"
#include "xlSmartController.h"
#include "xlxSerialConsole.h"
#include "SparkIntervalTimer.h"
//------------------------------------------------------------------
// Program Body Begins Here
//------------------------------------------------------------------
// SINGLETONS:
// theSys: SmartControllerClass
// theConfig: ConfigClass
// theConsole: SerialConsoleClass
// theLog: LoggerClass
// theRadio: RF24ServerClass
// Define hardware IntervalTimer
IntervalTimer sysTimer;
void SysteTimerCB()
{
static UC fastTick = 0; // must be static
// Change Status Indicator according to system status
// e.g: fast blink, slow blink, breath, etc
// ToDo:
// ToDo: MIC input (tone detection)
// High speed non-block process
if (++fastTick > RTE_TICK_FASTPROCESS) {
fastTick = 0;
theSys.FastProcess();
}
}
void setup()
{
// System Initialization
theSys.Init();
// Load Configuration
theConfig.LoadConfig();
// Initialize Pins
theSys.InitPins();
// Start system timer: callback every n * 0.5ms using hmSec timescale
//Use TIMER6 to retain PWM capabilities on all pins
sysTimer.begin(SysteTimerCB, RTE_DELAY_SYSTIMER, hmSec, TIMER6);
// Initialization Radio Interfaces
theSys.InitRadio();
// Initialization network Interfaces
theSys.InitNetwork();
// Initiaze Cloud Variables & Functions
theSys.InitCloudObj();
// Initialize Sensors
theSys.InitSensors();
// Initialize Serial Console
theConsole.Init();
// System Starts
theSys.Start();
// Wait the system started
while( Time.now() < 2000 ) {
Particle.process();
}
}
// Notes: approximate RTE_DELAY_SELFCHECK ms per loop
/// If you need more accurate and faster timer, do it with sysTimer
void loop()
{
static UC tick = 0;
// Process commands
IF_MAINLOOP_TIMER( theSys.ProcessCommands(), "ProcessCommands" );
// Collect data
IF_MAINLOOP_TIMER( theSys.CollectData(tick++), "CollectData" );
// Act on new Rules in Rules chain
IF_MAINLOOP_TIMER( theSys.ReadNewRules(), "ReadNewRules" );
// ToDo: transfer data
// ToDo: status synchronize
// Self-test & alarm trigger, also insert delay between each loop
IF_MAINLOOP_TIMER( theSys.SelfCheck(RTE_DELAY_SELFCHECK), "SelfCheck" );
}
#endif