-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeed.go
89 lines (76 loc) · 1.84 KB
/
feed.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
package main
import (
"image/color"
"time"
"github.com/itohio/FishFeeder/icons"
)
var (
feedingTime = time.Now()
feedingCount = 0
// Feeding scheme. Depends on the amount of food, how fast it sinks, how observant fish are, etc.
// Half of my fish don't notice the food until it is all on the ground if I dump it all right away.
feedingDelays = []time.Duration{
time.Second * 30, // feed some faster fish and wait for 30s for other fish to notice food
time.Second * 30, // feed again
time.Second * 30, // feed some really slow fish
time.Second * 3, // this is ignored, just did not bother to clean it up algorithm-wise
}
)
func handleFeeding() {
if feedingCount >= len(feedingDelays) {
feedingCount = 0
}
if feedingCount == 0 {
return
}
var (
sum time.Duration
cur time.Duration
curTmp time.Duration
)
for i, d := range feedingDelays {
if i == len(feedingDelays)-1 {
break
}
sum += d
if i+1 < feedingCount {
cur += d
}
}
cur += time.Since(feedingTime)
for i, d := range feedingDelays {
if i == len(feedingDelays)-1 {
break
}
curTmp += d
w := int16(160 * (curTmp.Seconds() / sum.Seconds()))
display.FillRectangle(w, 0, 1, 10, color.RGBA{G: 40})
}
w := int16(160 * (cur.Seconds() / sum.Seconds()))
display.FillRectangle(0, 0, w, 7, color.RGBA{G: 70})
if time.Since(feedingTime) > feedingDelays[feedingCount-1] {
feedingTime = time.Now()
go func() {
command <- FEED
}()
}
}
func feed() {
feedingTime = time.Now()
feedingCount += 1
dispenseFood()
if feedingCount >= len(feedingDelays) {
go func() {
command <- RESET_1
}()
feedingCount = 0
}
}
func dispenseFood() {
display.FillScreen(color.RGBA{})
display.DrawRGBBitmap((160-40)/2, (80-40)/2, icons.FoodPng, 40, 40)
servo.SetAngle(5)
time.Sleep(time.Millisecond * 200)
servo.SetAngle(92)
display.FillScreen(color.RGBA{})
}