-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nudges.go
124 lines (111 loc) · 2.08 KB
/
nudges.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"image/color"
"time"
"github.com/itohio/FishFeeder/icons"
)
var (
selectedTime = time.Now()
selectedNudge = -1
iconImages = [][]uint16{
icons.FoodPng,
icons.AquariumPng,
icons.FilterPng,
}
colors = []color.RGBA{
{G: 8},
{B: 8},
{R: 8},
}
nudges = []Nudge{
{
timestamp: time.Now(),
delay: time.Hour * 24,
nudge: makeNudge(colors[0], icons.FoodPng),
},
{
timestamp: time.Now(),
delay: time.Hour * 24 * 7,
nudge: makeNudge(colors[1], icons.AquariumPng),
},
{
timestamp: time.Now(),
delay: time.Hour * 24 * 30,
nudge: makeNudge(colors[2], icons.FilterPng),
},
}
)
func maxColor(c color.RGBA) color.RGBA {
m := uint8(255)
r := 0.0
if 255-c.R < m {
m = 255 - c.R
r = 255 / float64(c.R)
}
if 255-c.G < m {
m = 255 - c.G
r = 255 / float64(c.G)
}
if 255-c.B < m {
m = 255 - c.B
r = 255 / float64(c.B)
}
return color.RGBA{
R: uint8(float64(c.R) * r),
G: uint8(float64(c.G) * r),
B: uint8(float64(c.B) * r),
}
}
func fill(c color.RGBA) {
display.FillScreen(c)
}
func flash(c color.RGBA, n *Nudge) bool {
for j := 0; j < 2; j++ {
time.Sleep(time.Millisecond * 333)
for i := 0; i < 3; i++ {
fill(maxColor(c))
ledPin.Low()
time.Sleep(time.Millisecond * 100)
fill(color.RGBA{R: 0x16, G: 0x16, B: 0x16})
ledPin.High()
time.Sleep(time.Millisecond * 100)
if clicked() {
return true
}
}
fill(color.RGBA{})
}
return false
}
func clicked() bool {
if cmd, ok := <-command; ok && (cmd >= RESET || cmd == FEED) {
if cmd == FEED {
go func() {
command <- FEED
}()
}
return true
}
return false
}
func sleep(n *Nudge, icon []uint16) bool {
for i := 0; i < 30; i++ {
fill(color.RGBA{})
display.DrawRGBBitmap((160-40)/2, (80-40)/2+(int16(i%2))*5, icon, 40, 40)
time.Sleep(time.Millisecond * 100)
if clicked() {
return true
}
}
return false
}
func makeNudge(c color.RGBA, icon []uint16) func(*Nudge) {
return func(n *Nudge) {
for {
if flash(c, n) || sleep(n, icon) {
display.FillScreen(color.RGBA{})
break
}
}
}
}