/
slideshow.nim
executable file
·311 lines (260 loc) · 9.03 KB
/
slideshow.nim
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import std/random
import std/typetraits
import picostdlib
import picostdlib/pico/rand
import picostdlib/hardware/watchdog
import pimoroni_pico/libraries/pico_graphics/drawjpeg
import pimoroni_pico/libraries/inky_frame
import pimoroni_pico/libraries/pico_graphics/error_diffusion
const pictureDelay = 5
discard stdioInitAll()
var inky: InkyFrame
inky.boot()
var fs: FATFS
var jpegDecoder: JpegDecoder[PicoGraphicsPen3Bit]
echo "Detected Inky Frame model: ", inky.kind
inky.init()
inky.led(Led.LedActivity, 100)
echo "Wake Up Events: ", inky.getWakeUpEvents()
# if cyw43ArchInit() == PicoOk:
# echo "Initialized Cyw43"
# if inky.isBatteryPowered():
# echo "Running on battery!"
# echo "VSYS voltage: ", inky.getBatteryVoltage(), " V"
# else:
# echo "Powered by VBUS. Voltage: ", inky.getBatteryVoltage(), " V"
# else:
# echo "Could not initialize Cyw43"
jpegDecoder.init(inky)
jpegDecoder.errDiff.matrix = FloydSteinberg
jpegDecoder.errDiff.alternateRow = true
jpegDecoder.errDiff.variableDither = true
jpegDecoder.errDiff.hybridDither = false
jpegDecoder.colorModifier = proc (color: var Rgb) =
color = color.level(gamma=1.4)
proc drawFile(filename: string) =
inky.led(LedActivity, 50)
let startTime = getAbsoluteTime()
inky.setPen(White)
inky.setBorder(White)
inky.clear()
let (x, y, w, h) = case inky.kind:
of InkyFrame4_0: (0, 0, inky.width, inky.height)
of InkyFrame5_7: (0, -1, 600, 450)
of InkyFrame7_3: (-27, 0, 854, 480)
# let (x, y, w, h) = (0, 0, inky.width, inky.height)
if jpegDecoder.drawJpeg(filename, x, y, w, h, gravity=(0.5f, 0.5f), contains = true, DrawMode.ErrorDiffusion) == 1:
let endTime = getAbsoluteTime()
echo "Time: ", diffUs(startTime, endTime) div 1000, "ms"
inky.led(LedActivity, 100)
echo "Updating... (" & filename & ")"
inky.update()
echo "Update complete. Sleeping..."
inky.led(LedActivity, 0)
# inky.sleep(pictureDelay, true)
sleepMs(pictureDelay * 60 * 1000)
else:
inky.led(LedActivity, 0)
iterator walkDir(directory: string): FILINFO =
var file: FILINFO
var dir: DIR
discard f_opendir(dir.addr, directory.cstring)
while f_readdir(dir.addr, file.addr) == FR_OK and file.fname[0].bool:
yield file
proc getFileN(directory: string; n: Natural): FILINFO =
var i = 0
for file in walkDir(directory):
if i == n:
return file
inc(i)
proc inkyProc() =
inky.led(Led.LedActivity, 0)
echo "Starting..."
echo "Mounting SD card..."
let fr = f_mount(fs.addr, "".cstring, 1)
if fr != FR_OK:
echo "Failed to mount SD card, error: ", fr
if EvtBtnA in inky.getWakeUpEvents():
inky.led(LedA, 50)
echo "Drawing HSL/LCh chart..."
let startTime = getAbsoluteTime()
var errDiff = ErrorDiffusion[inky](backend: autobackend(inky))
errDiff.init(inky, 0, 0, inky.width, inky.height, FloydSteinberg)
errDiff.alternateRow = true
errDiff.hybridDither = false
errDiff.orientation = 0
if errDiff.backend == ErrorDiffusionBackend.BackendPsram:
errDiff.psramAddress = PsramAddress inky.width * inky.height
inky.setPen(White)
inky.clear()
var p = Point()
var row = newSeq[RgbLinear](inky.width)
for y in 0..<inky.height:
stdout.write $(y+1) & " of " & $inky.height & "\r"
stdout.flushFile()
p.y = y
let yd = y.float32 / inky.height.float32
let l = yd
for x in 0..<inky.width:
p.x = x
let xd = x / inky.width
let hue = xd * 2
var color = if xd < 0.5:
inky.createPenHsl(hue, 1.0, 1.02 - l * 1.04)
else:
LChToLab(1 - l, 0.5, hue * 360).fromLab()
# inky.setPen(color)
# inky.setPixel(p)
row[x] = color
errDiff.write(0, y, row)
errDiff.process()
errDiff.deinit()
let endTime = getAbsoluteTime()
echo "Time: ", diffUs(startTime, endTime) div 1000, "ms"
echo "Updating..."
inky.led(LedA, 100)
inky.update()
inky.led(LedA, 0)
elif EvtBtnB in inky.getWakeUpEvents():
inky.led(LedB, 50)
echo "Drawing bubbles..."
let startTime = getAbsoluteTime()
inky.setPen(White)
inky.clear()
const bubbleCount = 100
for i in 0..<bubbleCount:
echo i, " of ", bubbleCount
let size = 25 + rand(50)
let x = rand(inky.bounds.w)
let y = rand(inky.bounds.h)
let p = Point(x: x, y: y)
inky.setPen(Black)
inky.circle(p, size)
var color = inky.createPenHsl(rand(1.0f).float32, 0.5f + rand(0.5f).float32, 0.25f + rand(0.5f).float32).fromLinear()
color = color.saturate(1.50f).level(black=0.05f, white=0.97f, gamma=1.8f)
inky.setPen(color)
# inky.setPen(uint 2 + rand(4))
inky.circle(p, size - 2)
let endTime = getAbsoluteTime()
echo "Time: ", diffUs(startTime, endTime) div 1000, "ms"
echo "Updating..."
inky.led(LedB, 100)
inky.update()
inky.led(LedB, 0)
elif EvtBtnC in inky.getWakeUpEvents():
inky.led(LedC, 50)
echo "Drawing palette stripes..."
let startTime = getAbsoluteTime()
inky.setPen(Black)
inky.rectangle(constructRect(0, 0, inky.width div 8, inky.height))
inky.setPen(White)
inky.rectangle(constructRect((inky.width div 8) * 1, 0, inky.width div 8, inky.height))
inky.setPen(Green)
inky.rectangle(constructRect((inky.width div 8) * 2, 0, inky.width div 8, inky.height))
inky.setPen(Blue)
inky.rectangle(constructRect((inky.width div 8) * 3, 0, inky.width div 8, inky.height))
inky.setPen(Red)
inky.rectangle(constructRect((inky.width div 8) * 4, 0, inky.width div 8, inky.height))
inky.setPen(Yellow)
inky.rectangle(constructRect((inky.width div 8) * 5, 0, inky.width div 8, inky.height))
inky.setPen(Orange)
inky.rectangle(constructRect((inky.width div 8) * 6, 0, inky.width div 8, inky.height))
inky.setPen(Clean)
inky.rectangle(constructRect((inky.width div 8) * 7, 0, inky.width div 8, inky.height))
let endTime = getAbsoluteTime()
echo "Time: ", diffUs(startTime, endTime) div 1000, "ms"
echo "Updating..."
inky.led(LedC, 100)
inky.update()
inky.led(LedC, 0)
elif EvtBtnD in inky.getWakeUpEvents():
inky.led(LedD, 50)
echo "Drawing triangles and lines..."
let startTime = getAbsoluteTime()
inky.setPen(White)
inky.clear()
const triCount = 50
for i in 0..<triCount:
echo i, " of ", triCount
let size = 50 + rand(50)
let x = rand(inky.bounds.w)
let y = rand(inky.bounds.h)
var p1 = Point(x: x, y: y)
var p2 = p1 + Point(x: size, y: size)
var p3 = p1 + Point(x: -size, y: size)
inky.setPen(inky.createPenHsl(rand(1.0f).float32, 0.5f + rand(0.5f).float32, 0.25f + rand(0.5f).float32).fromLinear())
# inky.setPen(uint 2 + rand(4))
inky.triangle(p1, p2, p3)
const lineCount = 30
for i in 0..<lineCount:
echo i, " of ", lineCount
let x = rand(inky.bounds.w)
let y = rand(inky.bounds.h)
let size = 50 + rand(50)
let thickness = 5 + rand(8)
var p1 = Point(x: x - rand(size), y: y - rand(size))
var p2 = Point(x: x + rand(size), y: y + rand(size))
inky.setPen(inky.createPenHsl(rand(1.0f).float32, 0.5f + rand(0.5f).float32, 0.25f + rand(0.5).float32).fromLinear())
# inky.setPen(uint 2 + rand(4))
inky.thickLine(p1, p2, thickness)
let endTime = getAbsoluteTime()
echo "Time: ", diffUs(startTime, endTime) div 1000, "ms"
echo "Updating..."
inky.led(LedD, 100)
inky.update()
inky.led(LedD, 0)
elif EvtBtnE in inky.getWakeUpEvents():
inky.led(LedE, 50)
echo "Cleaning..."
inky.setPen(Clean)
inky.setBorder(Orange)
let startTime = getAbsoluteTime()
inky.clear()
let endTime = getAbsoluteTime()
echo "Time to clear: ", diffUs(startTime, endTime) div 1000, "ms"
echo "First update..."
inky.led(LedE, 100)
inky.update()
inky.setBorder(White)
echo "Second update..."
inky.update()
inky.led(LedE, 0)
if fr == FR_OK:
echo "Listing SD card contents.."
let directory = "/images"
var fileCount = 0
for i in walkDir(directory):
inc(fileCount)
echo "number of files: ", fileCount
var fileOrder = newSeq[int](fileCount)
for i in 0..<fileCount:
fileOrder[i] = i
let seed = cast[int64](getRand64())
echo "rand seed: ", seed
randomize(seed)
fileOrder.shuffle()
echo "shuffled file order:"
for i in fileOrder:
let file = getFileN(directory, i)
echo "- ", file.getFname()
echo "starting main image loop"
for i in fileOrder:
let file = getFileN(directory, i)
echo "- ", file.getFname(), " ", file.fsize
if file.fsize == 0:
continue
echo "- file timestamp: ", $file.getFileDate(), " ", $file.getFileTime()
let filename = directory & "/" & file.getFname()
drawFile(filename)
discard f_unmount("")
sleepMs(30 * 1000)
watchdogReboot(0, 0, 0)
inkyProc()
while true:
inky.led(LedActivity, 0)
# cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, true)
sleepMs(250)
inky.led(LedActivity, 100)
# cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, false)
sleepMs(250)
tightLoopContents()