Skip to content

Instantly share code, notes, and snippets.

@drewp
Last active September 19, 2018 22:23
Show Gist options
  • Select an option

  • Save drewp/d299548fd28c9db22dc1c8f0b1cf8f8a to your computer and use it in GitHub Desktop.

Select an option

Save drewp/d299548fd28c9db22dc1c8f0b1cf8f8a to your computer and use it in GitHub Desktop.
type mgos_gpio_int_handler_f* = proc (pin: cint, arg: pointer) {.cdecl.}
proc mgos_gpio_set_int_handler*(pin: cint, mode: mgos_gpio_int_mode, cb: mgos_gpio_int_handler_f, arg: pointer): bool {.importc, header: "mgos_gpio.h".}
proc mgos_gpio_enable_int*(pin: cint): bool {.importc, header: "mgos_gpio.h".}
type Knob = ref object of RootObj
pin1: cint
pin2: cint
pos: int
proc onChange(pin: cint, this: Knob) {.cdecl.} =
let a = mgos_gpio_read(this.pin1)
let b = mgos_gpio_read(this.pin2)
log("a=" & $a & " b=" & $b)
if a != b:
log("knob " & $this.pos)
this.pos += (if a: 1 else: -1)
proc newKnob(pin1: cint, pin2: cint): Knob =
result.new()
result.pin1 = pin1
result.pin2 = pin2
result.pos = 0
log("enable")
for pin in [pin1, pin2]:
log("any edge is " & $MGOS_GPIO_INT_EDGE_ANY)
discard mgos_gpio_set_int_handler(
pin,
MGOS_GPIO_INT_EDGE_ANY,
cast[mgos_gpio_int_handler_f](onChange),
cast[pointer](result))
discard mgos_gpio_enable_int(cast[cint](pin))
var knob: Knob
type Display = ref object of RootObj
proc mgos_app_init*(): mgos_app_init_result {.exportc.} =
NimMain()
log("hello mgos_app_init")
discard mgos_gpio_set_mode(36, MGOS_GPIO_MODE_INPUT) # sw
discard mgos_gpio_set_mode(37, MGOS_GPIO_MODE_INPUT) # d
discard mgos_gpio_set_mode(38, MGOS_GPIO_MODE_INPUT) # clk
log("new")
knob = newKnob(pin1 = 37, pin2 = 38)
log("app_init done")
return MGOS_APP_INIT_SUCCESS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment