Loom 4 v4.5
Arduino library for Internet of Things Rapid Prototyping in environmental sensing
Loading...
Searching...
No Matches
Loom_Hypnos.h
1#pragma once
2
3
4#include <OPEnS_RTC.h>
5#include <ArduinoLowPower.h>
6#include <map>
7#include <tuple>
8
9#include "Arduino.h"
10#include "Module.h"
11
12#include "Hardware/Loom_Hypnos/SDManager.h"
13#include "Loom_Manager.h"
14
15// Used to pass along the user defined interrupt callback
16using InterruptCallbackFunction = void (*)();
17
21enum HYPNOS_VERSION{
22 V3_2 = 10,
23 V3_3 = 11,
24 ADALOGGER = 4
25};
26
30enum TIME_ZONE{
31 WAT = 1,
32 AT = 2,
33 AST = 4,
34 EST = 5,
35 CST = 6,
36 MST = 7,
37 PST = 8,
38 AKST = 9,
39 HST = 9,
40 SST = 11,
41 GMT = 0,
42 BST = -1,
43 CET = -1,
44 EET = -2,
45 EEST = -3,
46 BRT = -3,
47 ZP4 = -4,
48 ZP5 = -5,
49 ZP6 = -6,
50 ZP7 = -7,
51 AWST = -8,
52 ACST = -9, // Half an hour off so its -9.5
53 AEST = -10
54
55};
56
60enum HypnosInterruptType{
61 SLEEP,
62 OTHER
63};
64
70class Loom_Hypnos : public Module{
71 protected:
72
73 /* These aren't used with the Hypnos */
74 void measure() override {};
75
76 void initialize() override {};
77
78 void power_up() override {};
79 void power_down() override {};
80
81 // We want to use the package method to add the timestamp to the JSON
82 void package() override;
83 public:
84
85 volatile bool shouldPowerUp = true;
86
95 Loom_Hypnos(Manager& man, HYPNOS_VERSION version, TIME_ZONE zone, bool use_custom_time = false, bool useSD = true);
96
100 ~Loom_Hypnos();
101
102 /* Power Control Functionality */
103
111 void enable(bool enable33 = true, bool emable5 = true);
112
117 void disable();
118
119 /* SD Functionality */
120
124 bool logToSD();
125
126 /* Sleep Functionality */
127
135 bool registerInterrupt(InterruptCallbackFunction isrFunc = nullptr, int interruptPin = 12, HypnosInterruptType interruptType = SLEEP, int triggerState = LOW);
136
141 void wakeup();
142
147 bool reattachRTCInterrupt(int interruptPin = 12);
148
153 void setInterruptDuration(const TimeSpan duration);
154
159 void sleep(bool waitForSerial = false);
160
164 DateTime getCurrentTime();
165
169 void set_custom_time();
170
177 TimeSpan getSleepIntervalFromSD(const char* fileName);
178
183 void getTimeZoneFromSD(const char* fileName);
184
189 char* readFile(const char* fileName) { return sdMan->readFile(fileName); };
190
194 const char* getDefaultFilename(){ return sdMan->getDefaultFilename(); };
195
199 SDManager* getSDManager() { return sdMan; };
200
204 void setLogName(const char* name) { sdMan->setLogName(name); };
205
206 /* Return initialization state of the RTC */
207 bool isRTCInitialized() { return RTC_initialized; };
208
209 private:
210
211 Manager* manInst = nullptr; // Instance of the manager
212 SDManager* sdMan = nullptr; // SD Manager
213
214 int sd_chip_select; // Pin that the SD card will use to communicate with the Hypnos
215 bool enableSD; // Specifies whether or not the SD card should be enabled on the Hypnos
216
217 int batch_size;
218
219 /* Real-Time Clock Settings */
220
221 RTC_DS3231 RTC_DS; // Real time clock reference
222 bool RTC_initialized = false; // Did the RTC initialize correctly?
223
224 bool custom_time = false; // Set the RTC to a user specified time
225
226 // Map the given pin to an interrupt call back
227 // 0th - ISR
228 // 1st - Interrupt Trigger
229 // 2nd - Interrupt Type (SLEEP or OTHER)
230 std::map<int, std::tuple<InterruptCallbackFunction, int, HypnosInterruptType>> pinToInterrupt;
231
232
233 void initializeRTC(); // Initialize RTC
234
235 void createTimezoneMap(); // Map Timezone Strings to Timezone enum
236 std::map<const char*, TIME_ZONE> timezoneMap; // String to Timezone enum
237
238 DateTime get_utc_time(); // Convert the local time to UTC, accounts for daylight savings zones
239 TIME_ZONE timezone; // Timezone the RTC was set to
240
241 void dateTime_toString(DateTime time, char array[21]); // Convert a DateTime object to our desired format
242
243 DateTime time; // UTC time
244 DateTime localTime; // Local time
245
246 /* Sleep functionality */
247
248 void pre_sleep(); // Called just before the hypnos enters sleep, this disconnects the power rails and the serial bus
249 void post_sleep(bool waitForSerial); // Called just after the hypnos wakes up, this reconnects the power rails and the serial bus
250
251
252
253};
Definition: Loom_Hypnos.h:70
const char * getDefaultFilename()
Definition: Loom_Hypnos.h:194
void setInterruptDuration(const TimeSpan duration)
Definition: Loom_Hypnos.cpp:346
SDManager * getSDManager()
Definition: Loom_Hypnos.h:199
void getTimeZoneFromSD(const char *fileName)
Definition: Loom_Hypnos.cpp:459
bool registerInterrupt(InterruptCallbackFunction isrFunc=nullptr, int interruptPin=12, HypnosInterruptType interruptType=SLEEP, int triggerState=LOW)
Definition: Loom_Hypnos.cpp:101
void wakeup()
Definition: Loom_Hypnos.cpp:169
void enable(bool enable33=true, bool emable5=true)
Definition: Loom_Hypnos.cpp:55
DateTime getCurrentTime()
Definition: Loom_Hypnos.cpp:246
void set_custom_time()
Definition: Loom_Hypnos.cpp:265
void disable()
Definition: Loom_Hypnos.cpp:80
char * readFile(const char *fileName)
Definition: Loom_Hypnos.h:189
TimeSpan getSleepIntervalFromSD(const char *fileName)
Definition: Loom_Hypnos.cpp:432
bool reattachRTCInterrupt(int interruptPin=12)
Definition: Loom_Hypnos.cpp:143
bool logToSD()
Definition: Loom_Hypnos.cpp:514
~Loom_Hypnos()
Definition: Loom_Hypnos.cpp:29
void setLogName(const char *name)
Definition: Loom_Hypnos.h:204
void sleep(bool waitForSerial=false)
Definition: Loom_Hypnos.cpp:368
Definition: Loom_Manager.h:18
Definition: Module.h:30
Definition: SDManager.h:15
void setLogName(const char *name)
Definition: SDManager.h:107
char * readFile(const char *fileName)
Definition: SDManager.cpp:283
const char * getDefaultFilename()
Definition: SDManager.h:73