diff --git a/demo/colors.go b/demo/colors.go new file mode 100644 index 0000000..1a228aa --- /dev/null +++ b/demo/colors.go @@ -0,0 +1,6 @@ +package main + +import "image/color" + +// palette of colors (iron) taken from http://ramphastosramblings.blogspot.com/2014/03/choosing-appropriate-colour-palette-for.html +var colors = []color.RGBA{{0, 0, 10, 255}, {0, 0, 20, 255}, {0, 0, 30, 255}, {0, 0, 37, 255}, {0, 0, 42, 255}, {0, 0, 46, 255}, {0, 0, 50, 255}, {0, 0, 54, 255}, {0, 0, 58, 255}, {0, 0, 62, 255}, {0, 0, 66, 255}, {0, 0, 70, 255}, {0, 0, 74, 255}, {0, 0, 79, 255}, {0, 0, 82, 255}, {1, 0, 85, 255}, {1, 0, 87, 255}, {2, 0, 89, 255}, {2, 0, 92, 255}, {3, 0, 94, 255}, {4, 0, 97, 255}, {4, 0, 99, 255}, {5, 0, 101, 255}, {6, 0, 103, 255}, {7, 0, 105, 255}, {8, 0, 107, 255}, {9, 0, 110, 255}, {10, 0, 112, 255}, {11, 0, 115, 255}, {12, 0, 116, 255}, {13, 0, 117, 255}, {13, 0, 118, 255}, {14, 0, 119, 255}, {16, 0, 120, 255}, {18, 0, 121, 255}, {19, 0, 123, 255}, {21, 0, 124, 255}, {23, 0, 125, 255}, {25, 0, 126, 255}, {27, 0, 128, 255}, {28, 0, 129, 255}, {30, 0, 131, 255}, {32, 0, 132, 255}, {34, 0, 133, 255}, {36, 0, 134, 255}, {38, 0, 135, 255}, {40, 0, 137, 255}, {42, 0, 137, 255}, {44, 0, 138, 255}, {46, 0, 139, 255}, {48, 0, 140, 255}, {50, 0, 141, 255}, {52, 0, 142, 255}, {54, 0, 142, 255}, {56, 0, 143, 255}, {57, 0, 144, 255}, {59, 0, 145, 255}, {60, 0, 146, 255}, {62, 0, 147, 255}, {63, 0, 147, 255}, {65, 0, 148, 255}, {66, 0, 149, 255}, {68, 0, 149, 255}, {69, 0, 150, 255}, {71, 0, 150, 255}, {73, 0, 150, 255}, {74, 0, 150, 255}, {76, 0, 151, 255}, {78, 0, 151, 255}, {79, 0, 151, 255}, {81, 0, 151, 255}, {82, 0, 152, 255}, {84, 0, 152, 255}, {86, 0, 152, 255}, {88, 0, 153, 255}, {90, 0, 153, 255}, {92, 0, 153, 255}, {93, 0, 154, 255}, {95, 0, 154, 255}, {97, 0, 155, 255}, {99, 0, 155, 255}, {100, 0, 155, 255}, {102, 0, 155, 255}, {104, 0, 155, 255}, {106, 0, 155, 255}, {108, 0, 156, 255}, {109, 0, 156, 255}, {111, 0, 156, 255}, {112, 0, 156, 255}, {113, 0, 157, 255}, {115, 0, 157, 255}, {117, 0, 157, 255}, {119, 0, 157, 255}, {120, 0, 157, 255}, {122, 0, 157, 255}, {124, 0, 157, 255}, {126, 0, 157, 255}, {127, 0, 157, 255}, {129, 0, 157, 255}, {131, 0, 157, 255}, {132, 0, 157, 255}, {134, 0, 157, 255}, {135, 0, 157, 255}, {137, 0, 157, 255}, {138, 0, 157, 255}, {139, 0, 157, 255}, {141, 0, 157, 255}, {143, 0, 156, 255}, {145, 0, 156, 255}, {147, 0, 156, 255}, {149, 0, 156, 255}, {150, 0, 155, 255}, {152, 0, 155, 255}, {153, 0, 155, 255}, {155, 0, 155, 255}, {156, 0, 155, 255}, {157, 0, 155, 255}, {159, 0, 155, 255}, {160, 0, 155, 255}, {162, 0, 155, 255}, {163, 0, 155, 255}, {164, 0, 155, 255}, {166, 0, 154, 255}, {167, 0, 154, 255}, {168, 0, 154, 255}, {169, 0, 153, 255}, {170, 0, 153, 255}, {171, 0, 153, 255}, {173, 0, 153, 255}, {174, 1, 152, 255}, {175, 1, 152, 255}, {176, 1, 152, 255}, {176, 1, 152, 255}, {177, 1, 151, 255}, {178, 1, 151, 255}, {179, 1, 150, 255}, {180, 2, 150, 255}, {181, 2, 149, 255}, {182, 2, 149, 255}, {183, 3, 149, 255}, {184, 3, 149, 255}, {185, 4, 149, 255}, {186, 4, 149, 255}, {186, 4, 148, 255}, {187, 5, 147, 255}, {188, 5, 147, 255}, {189, 5, 147, 255}, {190, 6, 146, 255}, {191, 6, 146, 255}, {191, 6, 146, 255}, {192, 7, 145, 255}, {192, 7, 145, 255}, {193, 8, 144, 255}, {193, 9, 144, 255}, {194, 10, 143, 255}, {195, 10, 142, 255}, {195, 11, 142, 255}, {196, 12, 141, 255}, {197, 12, 140, 255}, {198, 13, 139, 255}, {198, 14, 138, 255}, {199, 15, 137, 255}, {200, 16, 136, 255}, {201, 17, 135, 255}, {202, 18, 134, 255}, {202, 19, 133, 255}, {203, 19, 133, 255}, {203, 20, 132, 255}, {204, 21, 130, 255}, {205, 22, 129, 255}, {206, 23, 128, 255}, {206, 24, 126, 255}, {207, 24, 124, 255}, {207, 25, 123, 255}, {208, 26, 121, 255}, {209, 27, 120, 255}, {209, 28, 118, 255}, {210, 28, 117, 255}, {210, 29, 116, 255}, {211, 30, 114, 255}, {211, 32, 113, 255}, {212, 33, 111, 255}, {212, 34, 110, 255}, {213, 35, 107, 255}, {213, 36, 105, 255}, {214, 37, 103, 255}, {215, 38, 101, 255}, {216, 39, 100, 255}, {216, 40, 98, 255}, {217, 42, 96, 255}, {218, 43, 94, 255}, {218, 44, 92, 255}, {219, 46, 90, 255}, {219, 47, 87, 255}, {220, 47, 84, 255}, {221, 48, 81, 255}, {221, 49, 78, 255}, {222, 50, 74, 255}, {222, 51, 71, 255}, {223, 52, 68, 255}, {223, 53, 65, 255}, {223, 54, 61, 255}, {224, 55, 58, 255}, {224, 56, 55, 255}, {224, 57, 51, 255}, {225, 58, 48, 255}, {226, 59, 45, 255}, {226, 60, 42, 255}, {227, 61, 38, 255}, {227, 62, 35, 255}, {228, 63, 32, 255}, {228, 65, 29, 255}, {228, 66, 28, 255}, {229, 67, 27, 255}, {229, 68, 25, 255}, {229, 69, 24, 255}, {230, 70, 22, 255}, {231, 71, 21, 255}, {231, 72, 20, 255}, {231, 73, 19, 255}, {232, 74, 18, 255}, {232, 76, 16, 255}, {232, 76, 15, 255}, {233, 77, 14, 255}, {233, 77, 13, 255}, {234, 78, 12, 255}, {234, 79, 12, 255}, {235, 80, 11, 255}, {235, 81, 10, 255}, {235, 82, 10, 255}, {235, 83, 9, 255}, {236, 84, 9, 255}, {236, 86, 8, 255}, {236, 87, 8, 255}, {236, 88, 8, 255}, {237, 89, 7, 255}, {237, 90, 7, 255}, {237, 91, 6, 255}, {238, 92, 6, 255}, {238, 92, 5, 255}, {238, 93, 5, 255}, {238, 94, 5, 255}, {239, 95, 4, 255}, {239, 96, 4, 255}, {239, 97, 4, 255}, {239, 98, 4, 255}, {240, 99, 3, 255}, {240, 100, 3, 255}, {240, 101, 3, 255}, {241, 102, 3, 255}, {241, 102, 3, 255}, {241, 103, 3, 255}, {241, 104, 3, 255}, {241, 105, 2, 255}, {241, 106, 2, 255}, {241, 107, 2, 255}, {241, 107, 2, 255}, {242, 108, 1, 255}, {242, 109, 1, 255}, {242, 110, 1, 255}, {243, 111, 1, 255}, {243, 112, 1, 255}, {243, 113, 1, 255}, {243, 114, 1, 255}, {244, 115, 0, 255}, {244, 116, 0, 255}, {244, 117, 0, 255}, {244, 118, 0, 255}, {244, 119, 0, 255}, {244, 120, 0, 255}, {244, 122, 0, 255}, {245, 123, 0, 255}, {245, 124, 0, 255}, {245, 126, 0, 255}, {245, 127, 0, 255}, {246, 128, 0, 255}, {246, 129, 0, 255}, {246, 130, 0, 255}, {247, 131, 0, 255}, {247, 132, 0, 255}, {247, 133, 0, 255}, {247, 134, 0, 255}, {248, 135, 0, 255}, {248, 136, 0, 255}, {248, 136, 0, 255}, {248, 137, 0, 255}, {248, 138, 0, 255}, {248, 139, 0, 255}, {248, 140, 0, 255}, {249, 141, 0, 255}, {249, 141, 0, 255}, {249, 142, 0, 255}, {249, 143, 0, 255}, {249, 144, 0, 255}, {249, 145, 0, 255}, {249, 146, 0, 255}, {249, 147, 0, 255}, {250, 148, 0, 255}, {250, 149, 0, 255}, {250, 150, 0, 255}, {251, 152, 0, 255}, {251, 153, 0, 255}, {251, 154, 0, 255}, {251, 156, 0, 255}, {252, 157, 0, 255}, {252, 159, 0, 255}, {252, 160, 0, 255}, {252, 161, 0, 255}, {253, 162, 0, 255}, {253, 163, 0, 255}, {253, 164, 0, 255}, {253, 166, 0, 255}, {253, 167, 0, 255}, {253, 168, 0, 255}, {253, 170, 0, 255}, {253, 171, 0, 255}, {253, 172, 0, 255}, {253, 173, 0, 255}, {253, 174, 0, 255}, {254, 175, 0, 255}, {254, 176, 0, 255}, {254, 177, 0, 255}, {254, 178, 0, 255}, {254, 179, 0, 255}, {254, 180, 0, 255}, {254, 181, 0, 255}, {254, 182, 0, 255}, {254, 184, 0, 255}, {254, 185, 0, 255}, {254, 185, 0, 255}, {254, 186, 0, 255}, {254, 187, 0, 255}, {254, 188, 0, 255}, {254, 189, 0, 255}, {254, 190, 0, 255}, {254, 192, 0, 255}, {254, 193, 0, 255}, {254, 194, 0, 255}, {254, 195, 0, 255}, {254, 196, 0, 255}, {254, 197, 0, 255}, {254, 198, 0, 255}, {254, 199, 0, 255}, {254, 200, 0, 255}, {254, 201, 1, 255}, {254, 202, 1, 255}, {254, 202, 1, 255}, {254, 203, 1, 255}, {254, 204, 2, 255}, {254, 205, 2, 255}, {254, 206, 3, 255}, {254, 207, 4, 255}, {254, 207, 4, 255}, {254, 208, 5, 255}, {254, 209, 6, 255}, {254, 211, 8, 255}, {254, 212, 9, 255}, {254, 213, 10, 255}, {254, 214, 10, 255}, {254, 215, 11, 255}, {254, 216, 12, 255}, {254, 217, 13, 255}, {255, 218, 14, 255}, {255, 218, 14, 255}, {255, 219, 16, 255}, {255, 220, 18, 255}, {255, 220, 20, 255}, {255, 221, 22, 255}, {255, 222, 25, 255}, {255, 222, 27, 255}, {255, 223, 30, 255}, {255, 224, 32, 255}, {255, 225, 34, 255}, {255, 226, 36, 255}, {255, 226, 38, 255}, {255, 227, 40, 255}, {255, 228, 43, 255}, {255, 228, 46, 255}, {255, 229, 49, 255}, {255, 230, 53, 255}, {255, 230, 56, 255}, {255, 231, 60, 255}, {255, 232, 63, 255}, {255, 233, 67, 255}, {255, 234, 70, 255}, {255, 235, 73, 255}, {255, 235, 77, 255}, {255, 236, 80, 255}, {255, 237, 84, 255}, {255, 238, 87, 255}, {255, 238, 91, 255}, {255, 238, 95, 255}, {255, 239, 99, 255}, {255, 239, 103, 255}, {255, 240, 106, 255}, {255, 240, 110, 255}, {255, 241, 114, 255}, {255, 241, 119, 255}, {255, 241, 123, 255}, {255, 242, 128, 255}, {255, 242, 133, 255}, {255, 242, 138, 255}, {255, 243, 142, 255}, {255, 244, 146, 255}, {255, 244, 150, 255}, {255, 244, 154, 255}, {255, 245, 158, 255}, {255, 245, 162, 255}, {255, 245, 166, 255}, {255, 246, 170, 255}, {255, 246, 175, 255}, {255, 247, 179, 255}, {255, 247, 182, 255}, {255, 248, 186, 255}, {255, 248, 189, 255}, {255, 248, 193, 255}, {255, 248, 196, 255}, {255, 249, 199, 255}, {255, 249, 202, 255}, {255, 249, 205, 255}, {255, 250, 209, 255}, {255, 250, 212, 255}, {255, 251, 216, 255}, {255, 252, 219, 255}, {255, 252, 223, 255}, {255, 253, 226, 255}, {255, 253, 229, 255}, {255, 253, 232, 255}, {255, 254, 235, 255}, {255, 254, 238, 255}, {255, 254, 241, 255}, {255, 254, 244, 255}, {255, 255, 246, 255}} diff --git a/demo/main.go b/demo/main.go index 65464de..a2781b1 100644 --- a/demo/main.go +++ b/demo/main.go @@ -1,15 +1,36 @@ package main import ( + "image" "machine" "time" "image/color" + draw2 "golang.org/x/image/draw" + + "tinygo.org/x/drivers/amg88xx" + "tinygo.org/x/drivers/encoders" "tinygo.org/x/drivers/st7789" + "tinygo.org/x/drivers/tone" + "tinygo.org/x/drivers/ws2812" ) +const AMG88XX_SIZE = 8 +const AMG88XX_PIXELS = 24 + +var data [AMG88XX_SIZE * AMG88XX_SIZE]int16 +var pixels [AMG88XX_PIXELS * AMG88XX_PIXELS]int16 +var adj [16]int16 + func main() { + time.Sleep(2 * time.Second) + i2c := machine.I2C1 + i2c.Configure(machine.I2CConfig{ + Frequency: 2.0 * machine.MHz, + SDA: machine.P0_17, + SCL: machine.P0_20, + }) machine.SPI0.Configure(machine.SPIConfig{ SCK: machine.P1_01, @@ -36,19 +57,47 @@ func main() { width, height := display.Size() width = 240 height = 135 - println(width, height) white := color.RGBA{255, 255, 255, 255} red := color.RGBA{255, 0, 0, 255} blue := color.RGBA{0, 0, 255, 255} green := color.RGBA{0, 255, 0, 255} black := color.RGBA{0, 0, 0, 255} + wsPin := machine.P1_11 + wsPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) + ws := ws2812.NewWS2812(wsPin) + wsLeds := [2]color.RGBA{} + for i := range wsLeds { + wsLeds[i] = black + } + machine.InitADC() ax := machine.ADC{Pin: machine.P0_29} ay := machine.ADC{Pin: machine.P0_02} ax.Configure(machine.ADCConfig{}) ay.Configure(machine.ADCConfig{}) + bzrPin := machine.P0_31 + pwm := machine.PWM0 + speaker, _ := tone.New(pwm, bzrPin) + + enc := encoders.NewQuadratureViaInterrupt(machine.P1_00, machine.P0_24) + enc.Configure(encoders.QuadratureConfig{ + Precision: 4, + }) + + buttons := []machine.Pin{machine.P0_22, machine.P1_06, machine.P1_04} + for c := range buttons { + buttons[c].Configure(machine.PinConfig{Mode: machine.PinInputPullup}) + } + + println("STARTING CAMERA") + camera := amg88xx.New(machine.I2C1) + camera.Configure(amg88xx.Config{}) + println("CAMERA STARTED") + + var value int16 + display.FillScreen(black) display.FillRectangle(0, 0, width/2, height/2, white) @@ -57,8 +106,65 @@ func main() { display.FillRectangle(width/2, height/2, width/2, height/2, blue) display.FillRectangle(width/4, height/4, width/2, height/2, black) + redgreen := true + f := 100 + rotaryOldValue := 0 for { println(ax.Get(), ay.Get()) + if redgreen { + wsLeds[0] = red + wsLeds[1] = green + } else { + wsLeds[0] = green + wsLeds[1] = red + } + redgreen = !redgreen + ws.WriteColors(wsLeds[:]) + f++ + + if f >= 100 { + f = 0 + speaker.SetNote(tone.C5) + time.Sleep(100 * time.Millisecond) + speaker.Stop() + } + + if newValue := enc.Position(); newValue != rotaryOldValue { + println("value: ", newValue) + rotaryOldValue = newValue + } + println("ROTARY BTN:", !buttons[0].Get(), "UP:", !buttons[1].Get(), "DOWN:", !buttons[2].Get()) + + // get the values of the sensor in millicelsius + camera.ReadPixels(&data) + + srcImage := image.NewRGBA(image.Rect(0, 0, 8, 8)) + for j := 0; j < AMG88XX_SIZE; j++ { + for i := 0; i < AMG88XX_SIZE; i++ { + value = data[63-(i+j*AMG88XX_SIZE)] + // 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 + } + } + srcImage.Set(i, j, colors[value]) + } + } + + dst := image.NewRGBA(image.Rect(0, 0, 24, 24)) + draw2.BiLinear.Scale(dst, image.Rect(0, 0, 24, 24), srcImage, image.Rect(0, 0, 8, 8), draw2.Over, nil) + + for j := 0; j < AMG88XX_PIXELS; j++ { + for i := 0; i < AMG88XX_PIXELS; i++ { + display.FillRectangle(int16(60+(AMG88XX_PIXELS-i-1)*5), 7+int16(j*5), 5, 5, dst.RGBAAt(i, j)) + } + } + time.Sleep(100 * time.Millisecond) } } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6b6b888 --- /dev/null +++ b/go.mod @@ -0,0 +1,12 @@ +module github.com/conejoninja/nicebadge + +go 1.25.0 + +require tinygo.org/x/drivers v0.34.0 + +replace tinygo.org/x/drivers => /home/conejo/go/src/tinygo.org/x/drivers + +require ( + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect + golang.org/x/image v0.38.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0814d7e --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= +golang.org/x/image v0.38.0 h1:5l+q+Y9JDC7mBOMjo4/aPhMDcxEptsX+Tt3GgRQRPuE= +golang.org/x/image v0.38.0/go.mod h1:/3f6vaXC+6CEanU4KJxbcUZyEePbyKbaLoDOe4ehFYY= +tinygo.org/x/drivers v0.34.0 h1:lw8ePJeUSn9oICKBvQXHC9TIE+J00OfXfkGTrpXM9Iw= +tinygo.org/x/drivers v0.34.0/go.mod h1:ZdErNrApSABdVXjA1RejD67R8SNRI6RKVfYgQDZtKtk=