Documentation
¶
Overview ¶
Package rtc facilitates working with real-time clocks (RTCs). High level functions such as NewTicker and NewTimer encapsulate the details of working with the RTC while providing interfaces that are similar to Go's time.NewTicker and time.NewTimer respectively. If more flexible programming of the RTC is needed, the NewRTC function returns an rtc object that exposes all RTC functionality. When this object is instantiated, the RTC device file is kept open until the Close function is called. For convenience, static utility functions are also provided to open and close the RTC when only one function is needed. For example, reading the clock once is possible simply by calling rtc.Time(). Note that when working with the RTC, the highest resolution for time values is one second as defined in unix.RTCTime. https://www.kernel.org/doc/html/latest/admin-guide/rtc.html https://blog.cloudflare.com/its-go-time-on-linux/ https://man7.org/linux/man-pages/man4/rtc.4.html https://code.woboq.org/linux/linux/drivers/char/rtc.c.html
Index ¶
- func CancelWakeAlarm(dev string) (err error)
- func GetAlarm(dev string) (t time.Time, err error)
- func GetClocks() (devices []string, err error)
- func GetEpoch(dev string) (epoch uint, err error)
- func GetFrequency(dev string) (frequency uint, err error)
- func GetTime(dev string) (t time.Time, err error)
- func GetWakeAlarm(dev string) (enabled bool, pending bool, t time.Time, err error)
- func SetAlarm(dev string, t time.Time) (err error)
- func SetAlarmInterrupt(dev string, enable bool) (err error)
- func SetEpoch(dev string, epoch uint) (err error)
- func SetFrequency(dev string, frequency uint) (err error)
- func SetPeriodicInterrupt(dev string, enable bool) (err error)
- func SetTime(dev string, t time.Time) (err error)
- func SetUpdateInterrupt(dev string, enable bool) (err error)
- func SetWakeAlarm(dev string, t time.Time) (err error)
- type Alarm
- type RTC
- func (c *RTC) CancelWakeAlarm() (err error)
- func (c *RTC) Close() (err error)
- func (c *RTC) GetAlarm() (t time.Time, err error)
- func (c *RTC) GetEpoch() (epoch uint, err error)
- func (c *RTC) GetFrequency() (frequency uint, err error)
- func (c *RTC) GetTime() (t time.Time, err error)
- func (c *RTC) GetWakeAlarm() (enabled bool, pending bool, t time.Time, err error)
- func (c *RTC) SetAlarm(t time.Time) (err error)
- func (c *RTC) SetAlarmInterrupt(enable bool) (err error)
- func (c *RTC) SetEpoch(epoch uint) (err error)
- func (c *RTC) SetFrequency(frequency uint) (err error)
- func (c *RTC) SetPeriodicInterrupt(enable bool) (err error)
- func (c *RTC) SetTime(t time.Time) (err error)
- func (c *RTC) SetUpdateInterrupt(enable bool) (err error)
- func (c *RTC) SetWakeAlarm(t time.Time) (err error)
- type Tick
- type Ticker
- type Timer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CancelWakeAlarm ¶
CancelWakeAlarm cancels the wake alarm for the specified real-time clock device.
func GetAlarm ¶ added in v1.0.0
GetAlarm returns the alarm time for the specified real-time clock device.
Example ¶
package main import ( "fmt" "github.com/cleroux/rtc" ) func main() { t, err := rtc.GetAlarm("/dev/rtc") if err != nil { panic(err) } fmt.Printf("Current alarm time: %v\n", t) }
Output:
func GetClocks ¶ added in v1.0.0
GetClocks returns a list of real-time clocks in the system.
Example ¶
package main import ( "fmt" "github.com/cleroux/rtc" ) func main() { clocks, err := rtc.GetClocks() if err != nil { panic(err) } for _, clock := range clocks { fmt.Printf("Clock found: %s\n", clock) } }
Output:
func GetEpoch ¶ added in v1.0.0
GetEpoch reads the epoch from the specified real-time clock device.
Example ¶
package main import ( "fmt" "github.com/cleroux/rtc" ) func main() { epoch, err := rtc.GetEpoch("/dev/rtc") if err != nil { panic(err) } fmt.Printf("Current epoch: %d\n", epoch) }
Output:
func GetFrequency ¶ added in v1.0.0
GetFrequency returns the frequency of the specified real-time clock device.
Example ¶
package main import ( "fmt" "github.com/cleroux/rtc" ) func main() { frequency, err := rtc.GetFrequency("/dev/rtc") if err != nil { panic(err) } fmt.Printf("Frequency: %d\n", frequency) }
Output:
func GetTime ¶ added in v1.0.0
GetTime reads the time from the specified real-time clock device.
Example ¶
package main import ( "fmt" "github.com/cleroux/rtc" ) func main() { t, err := rtc.GetTime("/dev/rtc") if err != nil { panic(err) } fmt.Printf("Current time: %v\n", t) }
Output:
func GetWakeAlarm ¶ added in v1.0.0
GetWakeAlarm returns the current state of the wake alarm for the specified real-time clock device.
func SetAlarm ¶
SetAlarm sets the alarm time for the specified real-time clock device.
Example ¶
package main import ( "time" "github.com/cleroux/rtc" ) func main() { if err := rtc.SetAlarm("/dev/rtc", time.Now().Add(time.Minute)); err != nil { panic(err) } }
Output:
func SetAlarmInterrupt ¶
SetAlarmInterrupt enables or disables the alarm interrupt for the specified real-time clock device.
Example ¶
package main import ( "github.com/cleroux/rtc" ) func main() { if err := rtc.SetAlarmInterrupt("/dev/rtc", true); err != nil { panic(err) } }
Output:
func SetEpoch ¶
SetEpoch sets the epoch on the specified real-time clock device.
Example ¶
package main import ( "github.com/cleroux/rtc" ) func main() { if err := rtc.SetEpoch("/dev/rtc", 99); err != nil { panic(err) } }
Output:
func SetFrequency ¶
SetFrequency sets the periodic interrupt frequency of the specified real-time clock device.
Example ¶
package main import ( "github.com/cleroux/rtc" ) func main() { if err := rtc.SetFrequency("/dev/rtc", 64); err != nil { panic(err) } }
Output:
func SetPeriodicInterrupt ¶
SetPeriodicInterrupt enables or disables periodic interrupts for the specified real-time clock device.
func SetTime ¶
SetTime sets the time for the specified real-time clock device.
Example ¶
package main import ( "time" "github.com/cleroux/rtc" ) func main() { if err := rtc.SetTime("/dev/rtc", time.Now()); err != nil { panic(err) } }
Output:
func SetUpdateInterrupt ¶
SetUpdateInterrupt enables or disables the update interrupt for the specified real-time clock device.
Types ¶
type RTC ¶ added in v1.0.0
type RTC struct {
// contains filtered or unexported fields
}
Example ¶
package main import ( "fmt" "github.com/cleroux/rtc" ) func main() { clock, err := rtc.NewRTC("/dev/rtc") if err != nil { panic(err) } defer clock.Close() t, err := clock.GetTime() if err != nil { panic(err) } fmt.Printf("Current RTC Time: %v\n", t) }
Output:
func (*RTC) CancelWakeAlarm ¶ added in v1.0.0
CancelWakeAlarm cancels the real-time clock's wake alarm.
func (*RTC) GetFrequency ¶ added in v1.0.0
GetFrequency returns the periodic interrupt frequency.
func (*RTC) GetWakeAlarm ¶ added in v1.0.0
GetWakeAlarm returns the real-time clock's wake alarm time.
func (*RTC) SetAlarmInterrupt ¶ added in v1.0.0
SetAlarmInterrupt enables or disables the real-time clock's alarm interrupt.
func (*RTC) SetFrequency ¶ added in v1.0.0
SetFrequency sets the frequency of the real-time clock's periodic interrupt.
func (*RTC) SetPeriodicInterrupt ¶ added in v1.0.0
SetPeriodicInterrupt enables or disables the real-time clock's periodic interrupts.
func (*RTC) SetTime ¶ added in v1.0.0
SetTime sets the time for the specified real-time clock device.
func (*RTC) SetUpdateInterrupt ¶ added in v1.0.0
SetUpdateInterrupt enables or disables the real-time clock's update interrupt.
type Ticker ¶ added in v0.1.1
type Ticker struct { C <-chan Tick // contains filtered or unexported fields }
func NewTicker ¶
Example ¶
package main import ( "fmt" "github.com/cleroux/rtc" ) func main() { ticker, err := rtc.NewTicker("/dev/rtc", 2) if err != nil { panic(err) } defer ticker.Stop() for tick := range ticker.C { fmt.Printf("Tick. Frame:%d Time:%v Delta:%v Missed:%d\n", tick.Frame, tick.Time, tick.Delta, tick.Missed) } }
Output:
type Timer ¶ added in v0.1.1
type Timer struct { C <-chan Alarm // contains filtered or unexported fields }
func NewTimer ¶
NewTimer creates a new Timer that will send an Alarm with the current time on its channel after at least duration d.
Example ¶
package main import ( "fmt" "time" "github.com/cleroux/rtc" ) func main() { timer, err := rtc.NewTimer("/dev/rtc", time.Minute) if err != nil { panic(err) } defer timer.Stop() alarm := <-timer.C fmt.Printf("Alarm. Time:%v\n", alarm.Time) }
Output:
func NewTimerAt ¶
NewTimerAt creates a new Timer that will send an Alarm on its channel after the given time.
func (*Timer) Stop ¶ added in v0.1.1
Stop prevents the Timer from firing. It returns true if the call stops the timer, false if the timer has already expired or been stopped. Stop does not close the channel, to prevent a read from the channel succeeding incorrectly.
To ensure the channel is empty after a call to Stop, check the return value and drain the channel. For example, assuming the program has not received from t.C already:
if !t.Stop() { <-t.C }
This cannot be done concurrent to other receives from the Timer's channel or other calls to the Timer's Stop method.