' Preamble Option Base 0 ' Operational (live) log starts at element 0 Const LogTop=272 ' with length 6, we can squeeze 273 (0-272) into flash - if you are doing one entry per hour that is over 10 days ' but don't save to flash at that rate, a MicroMite would only last ~2 years! So balance it nicely Dim OpLog$(LogTop) Length 6 ' dimension the log array Var Restore ' pull any existing log into the operational array
' anywhere in your code... example to log a temperature LogEvent LogEnc$("U",GetTemp()) ' to make an entry in the log
Var Save OpLog$() ' save the array to flash - don't do this too often, try to save ' the log only at key points so as not to wear out the flash
' To dump the log For x=0 to LogTop If OpLog$(x)<>"" Then Print "@"+LogDec$(OpLog$(x)) Next
@02-04-2018 00:00:00 Temp 9C@02-04-2018 00:00:00 Sensor triggers in last hour=2@02-04-2018 01:00:00 Temp 7C@02-04-2018 02:00:00 Temp 7C@02-04-2018 02:00:00 Sensor triggers in last hour=11@02-04-2018 03:00:00 Temp 6C@02-04-2018 03:00:00 Sensor triggers in last hour=1@02-04-2018 04:00:00 Temp 6C@02-04-2018 05:00:00 Temp 7C@02-04-2018 05:31:00 Dawn. Temp 7C@02-04-2018 06:00:00 Temp 8C@02-04-2018 07:00:00 Temp 8C@02-04-2018 08:00:00 Temp 9C@02-04-2018 09:00:00 Temp 9C@02-04-2018 10:00:00 Temp 10C@02-04-2018 11:00:00 Temp 11C@02-04-2018 12:00:00 Temp 12C
' The Subs & Functions ' add an item to the event log - only manages and adds the log entry to the array, doesn't save to flash Sub LogEvent(x$) Local Integer n For n=1 To LogTop OpLog$(n-1)=OpLog$(n) Next OpLog$(LogTop)=x$ End Sub ' encode a log string Function LogEnc$(t$,n As Integer) Local Integer m Local z$,a$ a$=Hex$(UnixTime(Now())) For m=1 To 8 Step 2 z$=z$+Chr$(Val("&h"+Mid$(a$,m,2))) Next LogEnc$=z$+Chr$(n And 255)+Ucase$(t$) End Function ' decode a log string Function LogDec$(a$) Local l$,v$ Local Integer m For m=1 to 4 l$=l$+Hex$(Asc(Mid$(a$,m,1)),2) Next l$=HumanTime(Val("&h"+l$))+" " v$=Str$(SgnX(7,Asc(Mid$(a$,5)))) Select Case Mid$(a$,6,1) ' have whatever you want here in the case statements Case "O" LogDec$=l$+"System Started. Watchdog event="+v$ Case "U" LogDec$=l$+"Dawn. Temp "+v$+"C" Case "D" LogDec$=l$+"Dusk. Temp "+v$+"C" Case "T" LogDec$=l$+"Temp "+v$+"C" Case "S" LogDec$=l$+"Sensor triggers in last hour="+v$ Case Else LogDec$=l$+"Unknown" End Select End Function