Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Public const rtcWr As Int = 1 'write to rtc memory
Public const rtcRd As Int = 2 'read from rtc memory
Public const rtcGetTime As Int = 3 'get RTC clock periods
Public const rtcGetPeriod As Int = 4 'get RTC clock period
Public Const DSleep As Int = 5
Public varLocation As Int
Public VarLong As Long
Public rtcTimeLong As Long
Public rtcPeriodLong As Long
Dim esp As ESP8266
End Sub
Private Sub AppStart
Serial1.Initialize(9600) '115200)
Delay(2000) 'so that logs work!
Log("AppStart")
RTC(rtcRd,0,1)
Log("Read: ", VarLong)
VarLong = VarLong + 1
RTC(rtcWr, VarLong, 1)
Log("Written: ", VarLong)
RTC(rtcGetPeriod, 0, 0)
Log("Period: ", rtcPeriodLong)
RTC(rtcGetTime, 0, 0)
Log("get rtc time: ", rtcTimeLong)
' esp.restart 'rtc timer persists, rtc memory persists
RTC(DSleep, 3000, 0) 'rtc timer resets, rtc memory persists
End Sub
'there are 128 x 4byte user memory locations for storage (at = 0 to 127)
'val holds the value to write or is ignored for the other 4 cases or is the deepsleep period in mS
'action is 0 - 3 for the 4 rtc functions available
Public Sub RTC(Action As Int, Val As ULong, At As ULong)
varLocation = 64 + (At * 4)
Select Action
Case rtcWr
If (varLocation < 64) Or (varLocation > 191) Then Return
VarLong = Val
RunNative("rtc_Write", Action)
Case rtcRd
If (varLocation < 64) Or (varLocation > 191) Then Return
RunNative("rtc_Read", Action)
Case rtcGetTime
RunNative("rtc_GetTime", Action)
Case rtcGetPeriod
RunNative("rtc_GetPeriod", Action)
Case DSleep
RunNative("deepSleep", Val * 1000)
Case Else
End Select
End Sub
#if C
//#define RTCMEMORYSTART 64
//#define RTCMEMORYLEN 128
#include "user_interface.h" // this is for the RTC memory read/write functions
Long varLong;
ulong varlocation;
bool rtc_Write(B4R::Object* u){
varLong = b4r_main::_varlong;
varlocation = b4r_main::_varlocation;
system_rtc_mem_write(varlocation, &varLong, sizeof(varLong));
yield();
}
bool rtc_Read(B4R::Object* u){
varlocation = b4r_main::_varlocation;
system_rtc_mem_read(varlocation, &varLong, sizeof(varLong));
b4r_main::_varlong = varLong;
yield();
}
bool rtc_GetTime(B4R::Object* u){
b4r_main::_rtctimelong = system_get_rtc_time();
yield();
}
bool rtc_GetPeriod(B4R::Object* u){
b4r_main::_rtcperiodlong = system_rtc_clock_cali_proc();
yield();
}
void deepSleep(B4R::Object* o) {
ESP.deepSleep(o->toULong());
}
#end if