max3010x

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2021 License: MIT Imports: 5 Imported by: 1

README

max3010x

Version PkgGoDev License Go version

Go library to use the MAX3010x sensor for heart rate and SpO2 readings. Uses periph.io to handle I2C communication with the sensor.

This library has only been tested with a MAX30102 on a Raspberry Pi 4. If you have a MAX30100, please help me with testing.

TL;DR

You can download and build this test program:

$ go get github.com/cgxeiji/max3010x/max3010x
$ max3010x

demo.gif

How to use?

It is as simple as:

func main() {
    sensor, err := max3010x.New()
    if err != nil {
        log.Fatal(err)
    }
    defer sensor.Close()

    // Detect the heart rate
    hr, err := sensor.HeartRate()
    if errors.Is(err, max3010x.ErrNotDetected) {
        hr = 0
    } else if err != nil {
        log.Fatal(err)
    }
    fmt.Println("Heart rate:", hr)

    // Detect the SpO2 level
    spO2, err := sensor.SpO2()
    if errors.Is(err, max3010x.ErrNotDetected) {
        spO2 = 0
    } else if err != nil {
        log.Fatal(err)
    }
    fmt.Println("SpO2:", spO2)
}

Trying to read the heart rate or SpO2 values when the sensor is not in contact with a person will return a max3010x.ErrNotDetected error. You are free to handle this however you like.

Low-level interface

If you need to access specific functions of each sensor, or want to work with raw data, you can cast the sensor to the specific device:

func main() {
    sensor, err := max3010x.New()
    if err != nil {
        log.Fatal(err)
    }

    defer sensor.Close()
    device, err := sensor.ToMax30102()
    if errors.Is(err, max3010x.ErrWrongDevice) {
        fmt.Println("device is not MAX30102")
        return
    } else if err != nil {
        log.Fatal(err)
    }

    // Get the values for the IR and red LEDs.
    ir, red, err := device.IRRed()
    if err != nil {
        log.Fatal(err)
    }
}

Any questions or feedback?

Issues and pull-requests are welcomed!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrWrongDevice is thrown when trying to convert a max3010x.Device
	// interface to the underlying *Device struct and the device does not match
	// the PartID.
	ErrWrongDevice = errors.New("wrong device")
	// ErrNotDetected is thrown when trying to read a heart rate or SpO2 level
	// and nothing is detected on the sensor (e.g. no finger is placed on the
	// sensor when the function is called).
	ErrNotDetected = errors.New("nothing detected on the sensor")
	// ErrTooNoisy is thrown when trying to read data and has too much
	// variation, therefore consistent measurements cannot be done (e.g.
	// ambient light, moving finger, etc.).
	ErrTooNoisy = errors.New("data has too much noise")
)

Functions

This section is empty.

Types

type Device

type Device struct {

	// PartID is the byte part ID as set by the manufacturer.
	// MAX30100: 0x11 or max30100.PartID
	// MAX30102: 0x15 or max30102.PartID
	PartID byte
	RevID  byte
	// contains filtered or unexported fields
}

Device defines a MAX3010x device.

func New

func New(options ...Option) (*Device, error)

New returns a new MAX3010x device.

func (*Device) Calibrate

func (d *Device) Calibrate() error

Calibrate calibrates the power of each LED.

func (*Device) Close

func (d *Device) Close()

Close closes the devices and cleans after itself.

func (*Device) HeartRate

func (d *Device) HeartRate() (float64, error)

HeartRate returns the current heart rate. Heart rate is expected to be between 10 to 250 beats per minute. Values outside that range are considered invalid and the function will continue to sample until a valid bpm is found. If no contact is detect on the sensor, this function returns 0 with an ErrNotDetected error. If the sensor cannot detect a beat after 1s, it returns 0 with an ErrTooNoisy error.

func (*Device) Shutdown

func (d *Device) Shutdown() error

Shutdown sets the device into power-save mode.

func (*Device) SpO2

func (d *Device) SpO2() (float64, error)

SpO2 returns the SpO2 value in 100%.

func (*Device) Startup

func (d *Device) Startup() error

Startup wakes the device from power-save mode.

func (*Device) Temperature

func (d *Device) Temperature() (float64, error)

Temperature returns the current temperature of the device.

func (*Device) ToMax30102

func (d *Device) ToMax30102() (*max30102.Device, error)

ToMax30102 converts a max3010x device to a max30102 device to access low level functions. Check the package max3010x/max30102 for detailed behavior.

type Option

type Option func(d *Device) Option

An Option configures a device.

func OnAddr

func OnAddr(addr uint16) Option

OnAddr can be used to specify alternative I²C name. By default, the address is 0x57.

func OnBus

func OnBus(name string) Option

OnBus can be used to specify I²C bus name ("/dev/i2c-2", "I2C2", "2"). By default, the bus name is "", which selects the first available bus.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL