-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathApplication.h
223 lines (194 loc) · 5.76 KB
/
Application.h
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* Application.h
*
* The MIT License
*
* Copyright (c) 2017-2018 Tomoaki Yamaguchi tomoaki@tomy-tech.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef APPLICATION_H_
#define APPLICATION_H_
#if ARDUINO >= 100
#include <Arduino.h>
#include <AppDefine.h>
#else
#if ARDUINO < 100
#include "WProgram.h"
#endif
#endif
#include <inttypes.h>
#include <stdio.h>
#include <avr/pgmspace.h>
#include <avr/power.h>
#include <SoftwareSerial.h>
void int0D2(void);
void int1D3(void);
void setupDevice(void);
void sleep(void);
void wakeup(void);
void resetArduino(void);
void EnableInt0(void);
void EnableInt1(void);
void DebugPrint(const __FlashStringHelper *format, ...);
void ConsolePrint(const __FlashStringHelper *format, ...);
void DebugPrint(const char* format, ...);
void ConsolePrint(const char* format, ...);
void ConsoleBegin(unsigned long baud);
void ConsoleBegin(unsigned long baud, uint8_t rxpin, uint8_t txpin);
void DisableConsole(void);
void DisableDebug(void);
void SetUTC(uint16_t ddmmyy, uint16_t hhmmss);
namespace tomyApplication
{
#define ARDUINO_LED_PIN 13
#define LOG_BUF_LEN 128
/*
* Debug define
*/
//#define SHOW_TASK_LIST
//#define LORA_DEBUG
/*
* Sleep mode define
*/
#define SLEEP_MODE SLEEP_MODE_PWR_DOWN
//#define SLEEP_MODE SLEEP_MODE_STANDBY
//#define SLEEP_MODE SLEEP_MODE_PWR_SAVE
/*======================================
MACROs for the Appication
=======================================*/
#define TASK_LIST TaskList_t theTaskList[]
#define TASK(...) {__VA_ARGS__}
#define END_OF_TASK_LIST {0, 0, 0}
#define ReRun(...) theApplication->rerun(__VA_ARGS__)
#define LedOn() theApplication->indicator(true)
#define LedOff() theApplication->indicator(false)
#define disableInterrupt() theApplication->disableInterrupts()
#define enableInterrupt() theApplication->enableInterrupts()
#define elapseSeconds() theApplication->getElapseTime()
/*======================================
Type definitions
=======================================*/
/* TaskList type */
typedef struct
{
void (*callback)(void);
uint32_t start;
uint32_t interval;
} TaskList_t;
/* Interrupt Status */
enum INT_stat_t
{
INT_INIT, INT_INT0, INT_INT1
};
/*========================================
Class Application
=======================================*/
class TaskManager;
class Application
{
public:
Application(void);
~Application();
void initialize( void);
void run(void);
void rerun(void (*_callbackPtr)(), uint32_t second);
void indicator(bool onoff);
void setWdtInterval(uint8_t second);
uint32_t getElapseTime(void);
private:
void systemSleep(void);
void checkInt(uint8_t pin);
void disableInterrupts(void);
void enableInterrupts(void);
TaskManager * _taskMgr;
bool _sleepMode;
bool _int0enable;
bool _int1enable;
};
/*============================================
Class TaskManager
============================================*/
class TaskEvent;
class TaskManager
{
friend class TaskEvent;
public:
TaskManager(void);
~TaskManager(void);
void initialize(void);
void addTask(void (*)(), uint32_t start, uint32_t interval);
void execute(void);
void printTaskEvents(void);
private:
void startEvent(TaskEvent* event);
void insertEventHead(TaskEvent* event);
void insertEvent(TaskEvent* event);
void eraseEvent(TaskEvent* event);
bool isExist(TaskEvent* event);
TaskEvent* _head;
};
/*============================================
Class TaskEvent
============================================*/
class TaskEvent
{
friend class TaskManager;
public:
TaskEvent(TaskManager* mgr);
~TaskEvent(void);
void setIntervalTime(uint32_t value);
void setEventCallback(void (*callback)());
void setRemainTime(uint32_t sec);
void execute(void);
private:
uint32_t _remainTime;
uint32_t _interval;
uint32_t _startTime;
uint32_t _count;
uint8_t _isRunning;
void (*_callbackPtr)();
TaskEvent *_next;
static TaskManager* _taskMgr;
};
/*============================================
Class SerialLog
============================================*/
class SerialLog
{
public:
SerialLog(void);
~SerialLog(void);
void out(bool console, uint8_t len, const char* fmt, va_list args);
void out(bool console, uint8_t len, const __FlashStringHelper* fmt, va_list args);
void begin(unsigned long baud);
void begin(unsigned long baud, uint8_t rxpin, uint8_t txpin);
void disableDebug(void);
void disableConsole(void);
void savePower(void);
void flush(void);
private:
void print(char* buf);
SoftwareSerial* _serial;
bool _serialFlg;
bool _consoleFlg;
bool _debugFlg;
};
} /* tomyApplication */
#endif /* APPLICATION_H_ */