Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import posix
- import locks
- import nesper/general, nesper/uarts, nesper/tasks
- import ./example
- import ./modem
- import ./esp_pthread
- import ./shared
- var t {.rtcNoInit.}: int
- const TAG* = "main"
- type Guarded[T] = object
- lock: Lock
- data {.guard: lock.}: T
- proc newGuarded[T](data: T): ref Guarded[T] =
- new(result)
- initLock(result.lock)
- withLock(result.lock):
- result.data = data
- proc sendAT(params: pointer): pointer {.rtosThread.} =
- var res = cast[ptr ref Guarded[string]](params)
- var mdm = newModem(
- port = UART_NUM_1,
- tx_pin = GPIO_NUM_17,
- rx_pin = GPIO_NUM_16)
- if not mdm.init():
- loge(TAG, "Error initialising modem")
- return
- while true:
- withLock(res.lock):
- echo "Modem thread: " & res.data
- let (success, _) = mdm.sendAT("")
- if success:
- echo "Yep, ends with OK"
- else:
- echo "No it doesn't"
- vTaskDelay(pdMS_TO_TICKS(300))
- proc myMain(params: pointer): pointer {.rtosThread.} =
- var res = cast[ptr ref Guarded[string]](params) # Sort of gross, but
- echo "We're in the main thread"
- while true:
- withLock(res.lock):
- echo "Main thread: " & res.data
- vTaskDelay(pdMS_TO_TICKS(150))
- app_main():
- if t != 42:
- echo "Nope, its not 42"
- t = 42
- else:
- echo "Yes, it is!"
- vTaskDelay(pdMS_TO_TICKS(500))
- esp_restart()
- ## All of this is dead code
- var guardedString = newGuarded("Hello, world")
- check: esp_pthread_init()
- var cfg = esp_pthread_get_default_config()
- cfg.stack_size = csize_t(1024 * 8)
- check: esp_pthread_set_cfg(addr cfg)
- echo "Hello, Nim!"
- example.run()
- var mainThread: Pthread
- var mainRes = pthread_create(addr mainThread, nil, myMain, addr guardedString)
- if mainRes != 0:
- echo "Error creating main thread!"
- var modemThread: Pthread
- var modemRes = pthread_create(addr modemThread, nil, sendAT, addr guardedString)
- if modemRes != 0:
- echo "Error creating modem thread!"
- discard pthread_join(mainThread, nil)
- discard pthread_join(modemThread, nil)
- assert false, "Should never get here"
Advertisement
Add Comment
Please, Sign In to add comment