-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inky impression button handler (#52)
- Loading branch information
1 parent
e29702e
commit 425efc9
Showing
14 changed files
with
5,983 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,67 @@ | ||
import pixie, json, strformat, strutils | ||
import pixie, json, strformat, strutils, tables | ||
import lib/lgpio | ||
import frameos/types | ||
import frameos/channels | ||
|
||
import gpioHandler/gpioHandler as gpioHandler | ||
# TODO: make this configurable in the UI | ||
let inputPins = [ | ||
# pin, line flags, edge, debounce | ||
(5, LG_SET_PULL_UP, LG_FALLING_EDGE, 100000), | ||
(6, LG_SET_PULL_UP, LG_FALLING_EDGE, 100000), | ||
(16, LG_SET_PULL_UP, LG_FALLING_EDGE, 100000), | ||
(24, LG_SET_PULL_UP, LG_FALLING_EDGE, 100000) | ||
] | ||
let pinLabels = { | ||
5: "A", | ||
6: "B", | ||
16: "C", | ||
24: "D" | ||
}.toTable | ||
|
||
type Driver* = ref object of FrameOSDriver | ||
logger: Logger | ||
handler: int | ||
|
||
proc log(logger: Logger, message: string) = | ||
logger.log(%*{"event": "driver:gpioButton", "log": message}) | ||
proc log(message: string) = | ||
log(%*{"event": "driver:gpioButton", "log": message}) | ||
|
||
proc init*(frameOS: FrameOS): Driver = | ||
log(frameOS.logger, "Initializing GPIO button driver") | ||
proc alertsHandler(num_alerts: cint, alerts: lgGpioAlert_p, userdata: pointer) {.cdecl.} = | ||
let alerts = cast[ptr UncheckedArray[lgGpioAlert_t]](alerts) | ||
for i in 0 .. num_alerts - 1: | ||
let gpio = alerts[i].report.gpio.int | ||
let level = alerts[i].report.level.int | ||
let label = pinLabels.getOrDefault(gpio) | ||
sendEvent("button", %*{"pin": gpio, "label": label, "level": level}) | ||
|
||
let callback = proc (pin: cint, value: cint) = | ||
frameOS.logger.log(%*{"event": "gpio:press", "pin": $pin, "value": $value}) | ||
proc determineGPIODevice(): int = | ||
try: | ||
if readFile("/proc/cpuinfo").find("Raspberry Pi 5") >= 0: | ||
return 4 | ||
except: | ||
discard | ||
return 0 | ||
|
||
let handler = gpioHandler.init(callback).int | ||
if handler == -1: | ||
log(frameOS.logger, "Failed to initialize GPIO button driver") | ||
proc init*(frameOS: FrameOS): Driver = | ||
log("Initializing GPIO button driver") | ||
let gpioDevice = determineGPIODevice() | ||
|
||
result = Driver( | ||
name: "gpioButton", | ||
logger: frameOS.logger, | ||
handler: handler | ||
) | ||
let h = lgGpiochipOpen(gpioDevice.cint) | ||
result = Driver(name: "gpioButton", handler: h) | ||
if h < 0: | ||
log(&"gpiochip{gpioDevice} open failed") | ||
return | ||
|
||
let pins = [5.cint, 6.cint, 16.cint, 24.cint] | ||
for pin in pins: | ||
if gpioHandler.registerButton(pin.cint).int == -1: | ||
log(frameOS.logger, &"Failed to register GPIO button {pin}") | ||
for (pin, lineFlags, edge, debounce) in inputPins: | ||
log(&"Listening on GPIO {pin}") | ||
if lgGpioClaimInput(h, lineFlags.cint, pin.cint) < 0: | ||
log(&"Unable to claim GPIO {pin} for input") | ||
continue | ||
let res = lgGpioClaimAlert(h, 0, edge.cint, pin.cint, -1) | ||
if res < 0: | ||
log(&"Unable to claim GPIO {pin} for alerts: {lguErrorText(res)}") | ||
continue | ||
if lgGpioSetAlertsFunc(h, pin.cint, alertsHandler, nil) < 0: | ||
log(&"Unable to set alerts handler for GPIO {pin}") | ||
continue | ||
if lgGpioSetDebounce(h, pin.cint, debounce.cint) < 0: | ||
log(&"Unable to set debounce for GPIO {pin}") | ||
continue |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
frameos/src/drivers/gpioButton/gpioHandler/gpioHandler.nim
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
c2nim: | ||
`nimble path c2nim`/c2nim --importc gpioHandler.h |
Oops, something went wrong.