rtc
import rtc from 'mikrojs/rtc'Store small values in RTC memory that persist across deep sleep cycles. Useful for counters, state flags, and accumulated data in battery-powered applications. RTC memory is lost on hard reset or power loss.
The default export is an Rtc singleton.
Usage
import rtc from 'mikrojs/rtc'
const bootCount = rtc.createValue('boots', 0)
bootCount.inc()
console.log('Boot #', bootCount.get())Methods
rtc.createValue(key, defaultValue?)
createValue<T>(key: string): RTCValue<T>
createValue<T>(key: string, defaultValue: T): RTCValue<T>Create a handle to a named value in RTC memory. Supported types: number, string, boolean, undefined. If defaultValue is provided, it's returned by get() when the key doesn't exist.
When T is number, the returned value also has inc() and dec() methods.
const counter = rtc.createValue('counter', 0)
const flag = rtc.createValue<boolean>('initialized')rtc.clear()
clear(): voidErase all RTC memory data.
rtc.info()
info(): RtcMemoryInfoReturns current RTC memory usage.
const {used, total, entries} = rtc.info()
console.log(`${entries} entries, ${used}/${total} bytes`)RTCValue methods
value.get()
get(): TRead the current value. Returns defaultValue (or undefined) if the key hasn't been set.
value.set(value)
set(value: T): voidWrite a new value.
value.update(updater)
update(updater: (value: T) => T): voidRead, transform, and write in one step.
counter.update((n) => n + 10)value.delete()
delete(): voidRemove the key from RTC memory.
RTCNumber methods
Available when T is number:
value.inc(by?)
inc(by?: number): numberIncrement and return the new value. Defaults to incrementing by 1.
value.dec(by?)
dec(by?: number): numberDecrement and return the new value. Defaults to decrementing by 1.
Types
RtcMemoryInfo
interface RtcMemoryInfo {
used: number // bytes used
total: number // total bytes available
entries: number // number of stored keys
}