Skip to content

Commit 79c9683

Browse files
committed
reorganise sources, also combine fbscreenshot read and rotate passes
1 parent 795aab1 commit 79c9683

5 files changed

Lines changed: 67 additions & 31 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,3 +575,8 @@ type
575575
kind*: uint16
576576
code*: KeyCode
577577
value*: int32
578+
579+
proc pollReadable*(fd: cint, timeoutMs: cint): bool =
580+
var pfd = TPollfd(fd: fd, events: POLLIN, revents: 0)
581+
let rc = poll(addr pfd, 1, timeoutMs)
582+
result = rc > 0 and (pfd.revents and POLLIN) != 0

src/common.nim renamed to src/common/fbscreenshot.nim

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,24 @@ proc fbscreenshot*(output: string) =
1515
if bytesRead != fbData.len:
1616
raise newException(IOError, "Failed to read complete framebuffer data")
1717

18-
var pixels = newSeq[uint8](FbWidth * FbHeight * 4)
19-
18+
var rotatedPixels = newSeq[uint8](FbHeight * FbWidth * 4)
19+
2020
for y in 0..<FbHeight:
2121
for x in 0..<FbWidth:
2222
let idx = (y * FbWidth * 2 + x * 2)
2323
let pixel = uint16(fbData[idx]) or (uint16(fbData[idx + 1]) shl 8)
24-
24+
2525
let r = uint8((pixel shr 11) and 0x1F)
2626
let g = uint8((pixel shr 5) and 0x3F)
2727
let b = uint8(pixel and 0x1F)
28-
29-
let pixelIdx = (y * FbWidth + x) * 4
30-
pixels[pixelIdx + 0] = (r shl 3) or (r shr 2)
31-
pixels[pixelIdx + 1] = (g shl 2) or (g shr 4)
32-
pixels[pixelIdx + 2] = (b shl 3) or (b shr 2)
33-
pixels[pixelIdx + 3] = 255
34-
35-
var rotated = newSeq[uint8](FbHeight * FbWidth * 4)
36-
for y in 0..<FbHeight:
37-
for x in 0..<FbWidth:
38-
let srcIdx = (y * FbWidth + x) * 4
28+
3929
let dstX = FbHeight - 1 - y
4030
let dstY = x
41-
let dstIdx = (dstY * FbHeight + dstX) * 4
42-
43-
rotated[dstIdx + 0] = pixels[srcIdx + 0]
44-
rotated[dstIdx + 1] = pixels[srcIdx + 1]
45-
rotated[dstIdx + 2] = pixels[srcIdx + 2]
46-
rotated[dstIdx + 3] = pixels[srcIdx + 3]
31+
let dstIdx = (x * FbHeight + dstX) * 4
32+
33+
rotatedPixels[dstIdx] = (r shl 3) or (r shr 2)
34+
rotatedPixels[dstIdx + 1] = (g shl 2) or (g shr 4)
35+
rotatedPixels[dstIdx + 2] = (b shl 3) or (b shr 2)
36+
rotatedPixels[dstIdx + 3] = 255
4737

48-
discard savePNG32(output, rotated, FbHeight, FbWidth)
38+
discard savePNG32(output, rotatedPixels, FbHeight, FbWidth)

src/common/led.nim

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import std/strformat
2+
3+
const SunxiLedPath = "/sys/devices/platform/sunxi-led/leds"
4+
5+
type LedColour* = enum
6+
Red = 1
7+
Green = 2
8+
Blue = 3
9+
10+
type LedTrigger* = enum
11+
On = "default-on"
12+
Off = "none"
13+
BatteryChargingOrFull = "lradc_battery-charging-or-full"
14+
BatteryCharging = "lradc_battery-charging"
15+
BatteryFull = "lradc_battery-full"
16+
BatteryChargingBlinkFullSolid = "lradc_battery-charging-blink-full-solid"
17+
MMC0 = "mmc0"
18+
MMC1 = "mmc1"
19+
MMC2 = "mmc2"
20+
Timer = "timer"
21+
Heartbeat = "heartbeat"
22+
DoubleFlash = "doubleflash"
23+
Backlight = "backlight"
24+
GPIO = "gpio"
25+
26+
proc setLedBrightness*(colour: LedColour, val: int) =
27+
writeFile(
28+
&"{SunxiLedPath}/led{colour.ord}/brightness",
29+
$max(0, min(val, 255))
30+
)
31+
32+
proc setLedTrigger*(colour: LedColour, trigger: LedTrigger) =
33+
writeFile(
34+
&"{SunxiLedPath}/led{colour.ord}/trigger",
35+
$trigger
36+
)
37+
38+
proc setLedTimer*(colour: LedColour, delayOff: int, delayOn: int) =
39+
setLedTrigger(colour, LedTrigger.Timer)
40+
writeFile(
41+
&"{SunxiLedPath}/led{colour.ord}/delay_off",
42+
$delayOff
43+
)
44+
writeFile(
45+
&"{SunxiLedPath}/led{colour.ord}/delay_on",
46+
$delayOn
47+
)

src/fbscreenshot/fbscreenshot.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import std/[cmdline, strformat]
2-
from ../common import fbscreenshot
2+
import ../common/fbscreenshot
33

44
when isMainModule:
55
if paramCount() < 1:

src/quark_hotkeyd/quark_hotkeyd.nim

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import std/[posix, strformat, strutils, times, osproc, os, sets, options, monotimes]
22
from std/monotimes import MonoTime
33

4-
from ../common import fbscreenshot
5-
from evdev import InputEvent, EventKind, KeyCode
4+
import ../common/[fbscreenshot, evdev, led]
65

76
const
87
InputDev = "/dev/input/event0"
@@ -170,7 +169,7 @@ proc killCmdToRun() =
170169
discard kill(Pid(pid), SIGTERM)
171170

172171
proc screenshotHandler() =
173-
setLed(2, true)
172+
setLedTrigger(LedColour.Green, LedTrigger.On)
174173
let now = now()
175174
let filename = &"/mnt/SDCARD/Saves/screenshots/Screenshot_{now.year:04}{ord(now.month):02}{now.monthday:02}_{now.hour:02}{now.minute:02}{now.second:02}.png"
176175

@@ -179,7 +178,7 @@ proc screenshotHandler() =
179178
except:
180179
discard
181180

182-
setLed(2, false)
181+
setLedTrigger(LedColour.Green, LedTrigger.Off)
183182

184183
proc quicksaveHandler() =
185184
discard startProcess("/bin/sh", args = @["/mnt/SDCARD/System/scripts/quicksave.sh"])
@@ -193,11 +192,6 @@ proc rebootHandler() =
193192
sync()
194193
discard execl("/mnt/SDCARD/System/bin/reboot", "reboot", nil)
195194

196-
proc pollReadable(fd: cint, timeoutMs: cint): bool =
197-
var pfd = TPollfd(fd: fd, events: POLLIN, revents: 0)
198-
let rc = poll(addr pfd, 1, timeoutMs)
199-
result = rc > 0 and (pfd.revents and POLLIN) != 0
200-
201195
proc main() =
202196
let fd = posix.open(InputDev, O_RDONLY or O_NONBLOCK)
203197
if fd < 0:

0 commit comments

Comments
 (0)