mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-16 12:53:23 +00:00
AMG88xx thermal camera module
This commit is contained in:
committed by
Ron Evans
parent
08cc84ce48
commit
ebceed6014
@@ -13,6 +13,8 @@ smoke-test:
|
|||||||
@md5sum ./build/test.hex
|
@md5sum ./build/test.hex
|
||||||
tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/adxl345/main.go
|
tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/adxl345/main.go
|
||||||
@md5sum ./build/test.hex
|
@md5sum ./build/test.hex
|
||||||
|
tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/amg88xx
|
||||||
|
@md5sum ./build/test.hex
|
||||||
tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/apa102/main.go
|
tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/apa102/main.go
|
||||||
@md5sum ./build/test.hex
|
@md5sum ./build/test.hex
|
||||||
tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/apa102/itsybitsy-m0/main.go
|
tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/apa102/itsybitsy-m0/main.go
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ The following 45 devices are supported.
|
|||||||
|----------|-------------|
|
|----------|-------------|
|
||||||
| [ADT7410 I2C Temperature Sensor](https://www.analog.com/media/en/technical-documentation/data-sheets/ADT7410.pdf) | I2C |
|
| [ADT7410 I2C Temperature Sensor](https://www.analog.com/media/en/technical-documentation/data-sheets/ADT7410.pdf) | I2C |
|
||||||
| [ADXL345 accelerometer](http://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf) | I2C |
|
| [ADXL345 accelerometer](http://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf) | I2C |
|
||||||
|
| [AMG88xx 8x8 Thermal camera sensor](https://cdn-learn.adafruit.com/assets/assets/000/043/261/original/Grid-EYE_SPECIFICATIONS%28Reference%29.pdf) | I2C |
|
||||||
| [APA102 RGB LED](https://cdn-shop.adafruit.com/product-files/2343/APA102C.pdf) | SPI |
|
| [APA102 RGB LED](https://cdn-shop.adafruit.com/product-files/2343/APA102C.pdf) | SPI |
|
||||||
| [AT24CX 2-wire serial EEPROM](https://www.openimpulse.com/blog/wp-content/uploads/wpsc/downloadables/24C32-Datasheet.pdf) | I2C |
|
| [AT24CX 2-wire serial EEPROM](https://www.openimpulse.com/blog/wp-content/uploads/wpsc/downloadables/24C32-Datasheet.pdf) | I2C |
|
||||||
| [BH1750 ambient light sensor](https://www.mouser.com/ds/2/348/bh1750fvi-e-186247.pdf) | I2C |
|
| [BH1750 ambient light sensor](https://www.mouser.com/ds/2/348/bh1750fvi-e-186247.pdf) | I2C |
|
||||||
|
|||||||
@@ -0,0 +1,158 @@
|
|||||||
|
// Package amg88xx provides a driver for the AMG88XX Thermal Camera
|
||||||
|
//
|
||||||
|
// Datasheet:
|
||||||
|
// https://cdn-learn.adafruit.com/assets/assets/000/043/261/original/Grid-EYE_SPECIFICATIONS%28Reference%29.pdf
|
||||||
|
package amg88xx // import "tinygo.org/x/drivers/amg88xx"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"machine"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Device wraps an I2C connection to a AMG88xx device.
|
||||||
|
type Device struct {
|
||||||
|
bus machine.I2C
|
||||||
|
Address uint16
|
||||||
|
data []uint8
|
||||||
|
interruptMode InterruptMode
|
||||||
|
interruptEnable uint8
|
||||||
|
}
|
||||||
|
|
||||||
|
type InterruptMode uint8
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new AMG88xx connection. The I2C bus must already be
|
||||||
|
// configured.
|
||||||
|
//
|
||||||
|
// This function only creates the Device object, it does not touch the device.
|
||||||
|
func New(bus machine.I2C) Device {
|
||||||
|
return Device{
|
||||||
|
bus: bus,
|
||||||
|
Address: AddressHigh,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Configure sets up the device for communication
|
||||||
|
func (d *Device) Configure(cfg Config) {
|
||||||
|
d.data = make([]uint8, 128)
|
||||||
|
|
||||||
|
d.SetPCTL(NORMAL_MODE)
|
||||||
|
d.SetReset(INITIAL_RESET)
|
||||||
|
d.SetFrameRate(FPS_10)
|
||||||
|
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadPixels returns the 64 values (8x8 grid) of the sensor converted to millicelsius
|
||||||
|
func (d *Device) ReadPixels(buffer *[64]int16) {
|
||||||
|
d.bus.ReadRegister(uint8(d.Address), PIXEL_OFFSET, d.data)
|
||||||
|
for i := 0; i < 64; i++ {
|
||||||
|
buffer[i] = int16((uint16(d.data[2*i+1]) << 8) | uint16(d.data[2*i]))
|
||||||
|
if (buffer[i] & (1 << 11)) > 0 { // temperature negative
|
||||||
|
buffer[i] &= ^(1 << 11)
|
||||||
|
buffer[i] = -buffer[i]
|
||||||
|
}
|
||||||
|
buffer[i] *= PIXEL_TEMP_CONVERSION
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPCTL sets the PCTL
|
||||||
|
func (d *Device) SetPCTL(pctl uint8) {
|
||||||
|
d.bus.WriteRegister(uint8(d.Address), PCTL, []byte{pctl})
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetReset sets the reset value
|
||||||
|
func (d *Device) SetReset(rst uint8) {
|
||||||
|
d.bus.WriteRegister(uint8(d.Address), RST, []byte{rst})
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFrameRate configures the frame rate
|
||||||
|
func (d *Device) SetFrameRate(framerate uint8) {
|
||||||
|
d.bus.WriteRegister(uint8(d.Address), FPSC, []byte{framerate & 0x01})
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetMovingAverageMode sets the moving average mode
|
||||||
|
func (d *Device) SetMovingAverageMode(mode bool) {
|
||||||
|
var value uint8
|
||||||
|
if mode {
|
||||||
|
value = 1
|
||||||
|
}
|
||||||
|
d.bus.WriteRegister(uint8(d.Address), AVE, []byte{value << 5})
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetInterruptLevels sets the interrupt levels
|
||||||
|
func (d *Device) SetInterruptLevels(high int16, low int16) {
|
||||||
|
d.SetInterruptLevelsHysteresis(high, low, (high*95)/100)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetInterruptLevelsHysteresis sets the interrupt levels with hysteresis
|
||||||
|
func (d *Device) SetInterruptLevelsHysteresis(high int16, low int16, hysteresis int16) {
|
||||||
|
high = high / PIXEL_TEMP_CONVERSION
|
||||||
|
if high < -4095 {
|
||||||
|
high = -4095
|
||||||
|
}
|
||||||
|
if high > 4095 {
|
||||||
|
high = 4095
|
||||||
|
}
|
||||||
|
d.bus.WriteRegister(uint8(d.Address), INTHL, []byte{uint8(high & 0xFF)})
|
||||||
|
d.bus.WriteRegister(uint8(d.Address), INTHL, []byte{uint8((high & 0xFF) >> 4)})
|
||||||
|
|
||||||
|
low = low / PIXEL_TEMP_CONVERSION
|
||||||
|
if low < -4095 {
|
||||||
|
low = -4095
|
||||||
|
}
|
||||||
|
if low > 4095 {
|
||||||
|
low = 4095
|
||||||
|
}
|
||||||
|
d.bus.WriteRegister(uint8(d.Address), INTHL, []byte{uint8(low & 0xFF)})
|
||||||
|
d.bus.WriteRegister(uint8(d.Address), INTHL, []byte{uint8((low & 0xFF) >> 4)})
|
||||||
|
|
||||||
|
hysteresis = hysteresis / PIXEL_TEMP_CONVERSION
|
||||||
|
if hysteresis < -4095 {
|
||||||
|
hysteresis = -4095
|
||||||
|
}
|
||||||
|
if hysteresis > 4095 {
|
||||||
|
hysteresis = 4095
|
||||||
|
}
|
||||||
|
d.bus.WriteRegister(uint8(d.Address), INTHL, []byte{uint8(hysteresis & 0xFF)})
|
||||||
|
d.bus.WriteRegister(uint8(d.Address), INTHL, []byte{uint8((hysteresis & 0xFF) >> 4)})
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableInterrupt enables the interrupt pin on the device
|
||||||
|
func (d *Device) EnableInterrupt() {
|
||||||
|
d.interruptEnable = 1
|
||||||
|
d.bus.WriteRegister(uint8(d.Address), INTC, []byte{((uint8(d.interruptMode) << 1) | d.interruptEnable) & 0x03})
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableInterrupt disables the interrupt pin on the device
|
||||||
|
func (d *Device) DisableInterrupt() {
|
||||||
|
d.interruptEnable = 0
|
||||||
|
d.bus.WriteRegister(uint8(d.Address), INTC, []byte{((uint8(d.interruptMode) << 1) | d.interruptEnable) & 0x03})
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetInterruptMode sets the interrupt mode
|
||||||
|
func (d *Device) SetInterruptMode(mode InterruptMode) {
|
||||||
|
d.interruptMode = mode
|
||||||
|
d.bus.WriteRegister(uint8(d.Address), INTC, []byte{((uint8(d.interruptMode) << 1) | d.interruptEnable) & 0x03})
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInterrupt reads the state of the triggered interrupts
|
||||||
|
func (d *Device) GetInterrupt() []uint8 {
|
||||||
|
data := make([]uint8, 8)
|
||||||
|
d.bus.ReadRegister(uint8(d.Address), INT_OFFSET, data)
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClearInterrupt clears any triggered interrupts
|
||||||
|
func (d *Device) ClearInterrupt() {
|
||||||
|
d.SetReset(FLAG_RESET)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadThermistor reads the onboard thermistor
|
||||||
|
func (d *Device) ReadThermistor() int16 {
|
||||||
|
data := make([]uint8, 2)
|
||||||
|
d.bus.ReadRegister(uint8(d.Address), TTHL, data)
|
||||||
|
return (int16((uint16(data[1])<<8)|uint16(data[0])) * THERMISTOR_CONVERSION) / 10
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package amg88xx
|
||||||
|
|
||||||
|
// The I2C address which this device listens to.
|
||||||
|
const AddressHigh = 0x69
|
||||||
|
const AddressLow = 0x68
|
||||||
|
|
||||||
|
const (
|
||||||
|
PCTL = 0x00
|
||||||
|
RST = 0x01
|
||||||
|
FPSC = 0x02
|
||||||
|
INTC = 0x03
|
||||||
|
STAT = 0x04
|
||||||
|
SCLR = 0x05
|
||||||
|
AVE = 0x07
|
||||||
|
INTHL = 0x08
|
||||||
|
INTHH = 0x09
|
||||||
|
INTLL = 0x0A
|
||||||
|
INTLH = 0x0B
|
||||||
|
IHYSL = 0x0C
|
||||||
|
IHYSH = 0x0D
|
||||||
|
TTHL = 0x0E
|
||||||
|
TTHH = 0x0F
|
||||||
|
INT_OFFSET = 0x010
|
||||||
|
PIXEL_OFFSET = 0x80
|
||||||
|
|
||||||
|
// power modes
|
||||||
|
NORMAL_MODE = 0x00
|
||||||
|
SLEEP_MODE = 0x01
|
||||||
|
STAND_BY_60 = 0x20
|
||||||
|
STAND_BY_10 = 0x21
|
||||||
|
|
||||||
|
// resets
|
||||||
|
FLAG_RESET = 0x30
|
||||||
|
INITIAL_RESET = 0x3F
|
||||||
|
|
||||||
|
// frame rates
|
||||||
|
FPS_10 = 0x00
|
||||||
|
FPS_1 = 0x01
|
||||||
|
|
||||||
|
// interrupt modes
|
||||||
|
DIFFERENCE InterruptMode = 0x00
|
||||||
|
ABSOLUTE_VALUE InterruptMode = 0x01
|
||||||
|
|
||||||
|
PIXEL_TEMP_CONVERSION = 250
|
||||||
|
THERMISTOR_CONVERSION = 625
|
||||||
|
)
|
||||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,56 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image/color"
|
||||||
|
"machine"
|
||||||
|
|
||||||
|
"tinygo.org/x/drivers/st7735"
|
||||||
|
|
||||||
|
"tinygo.org/x/drivers/amg88xx"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
machine.SPI1.Configure(machine.SPIConfig{
|
||||||
|
SCK: machine.SPI1_SCK_PIN,
|
||||||
|
MOSI: machine.SPI1_MOSI_PIN,
|
||||||
|
MISO: machine.SPI1_MISO_PIN,
|
||||||
|
Frequency: 8000000,
|
||||||
|
})
|
||||||
|
machine.I2C0.Configure(machine.I2CConfig{SCL: machine.SCL_PIN, SDA: machine.SDA_PIN})
|
||||||
|
|
||||||
|
display := st7735.New(machine.SPI1, machine.TFT_RST, machine.TFT_DC, machine.TFT_CS, machine.TFT_LITE)
|
||||||
|
display.Configure(st7735.Config{
|
||||||
|
Rotation: st7735.ROTATION_90,
|
||||||
|
})
|
||||||
|
display.FillScreen(color.RGBA{0, 0, 0, 255})
|
||||||
|
|
||||||
|
camera := amg88xx.New(machine.I2C0)
|
||||||
|
camera.Configure(amg88xx.Config{})
|
||||||
|
|
||||||
|
var data [64]int16
|
||||||
|
var value int16
|
||||||
|
for {
|
||||||
|
// get the values of the sensor in millicelsius
|
||||||
|
camera.ReadPixels(&data)
|
||||||
|
|
||||||
|
for j := int16(0); j < 8; j++ {
|
||||||
|
for i := int16(0); i < 8; i++ {
|
||||||
|
value = data[63-(i+j*8)]
|
||||||
|
// treat anything below 18°C as 18°C
|
||||||
|
if value < 18000 {
|
||||||
|
value = 0
|
||||||
|
} else {
|
||||||
|
value = (value - 18000) / 36
|
||||||
|
// our color array only have 433 values, avoid getting a value that doesn't exist
|
||||||
|
if value > 432 {
|
||||||
|
value = 432
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// show the image on the PyBadge's display
|
||||||
|
display.FillRectangle(16+i*16, j*16, 16, 16, colors[value])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user