changeset 397:0bc26b4fc427

a little further on toplevel nim code. event passing is not working Ignore-this: 86bdf86c796f2cf2ce2f245a0468c4eb
author drewp@bigasterisk.com
date Thu, 21 Feb 2019 22:37:04 -0800
parents 66f1ed94c2e4
children 8fd15a372ed8
files service/rfid_pn532/graphserver.nim service/rfid_pn532/rfid.nim
diffstat 2 files changed, 50 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/service/rfid_pn532/graphserver.nim	Sat Jan 19 11:07:27 2019 -0800
+++ b/service/rfid_pn532/graphserver.nim	Thu Feb 21 22:37:04 2019 -0800
@@ -87,3 +87,7 @@
     poll()
 
 
+# Replace graph contents
+proc setGraph*(self: GraphServer) =
+  echo "graph replaced"
+  
--- a/service/rfid_pn532/rfid.nim	Sat Jan 19 11:07:27 2019 -0800
+++ b/service/rfid_pn532/rfid.nim	Thu Feb 21 22:37:04 2019 -0800
@@ -9,6 +9,7 @@
 import graphserver
 import tags
 import threadpool
+import os
 import sets
 
 type CardEvent = object of RootObj
@@ -17,14 +18,28 @@
   appeared: bool
 type CardEventChannel = Channel[CardEvent]
 
-
-var events: CardEventChannel
+type TagWatcher = ref object of RootObj
+  dev: NfcDevice
+  nearbyUids: HashSet[cstring]
+  events: ref CardEventChannel
+  
+type FakeTagWatcher = ref object of RootObj
+  events: ref CardEventChannel
+  
+proc initFakeTagWatcher(events: ref  CardEventChannel): FakeTagWatcher =
+  new result
+  result.events = events
 
-type TagWatcher = object of RootObj
-  dev: NfcDevice
-  nearbyUids: var HashSet[cstring]
-
-proc initTagWatcher(): var TagWatcher =
+proc watchForever*(self: FakeTagWatcher) {.thread.} =
+  var events = self.events[]
+  while true:
+    sleep(2000)
+    events.send(CardEvent(uid: "abcdef", body: "helloworld", appeared: true))
+    sleep(2000)
+    events.send(CardEvent(uid: "abcdef", appeared: false))
+  
+proc initTagWatcher(): TagWatcher =
+  new(result)
   result.nearbyUids.init()
   
 proc oneScan(self: TagWatcher) =
@@ -47,14 +62,14 @@
     tag.connect()
     try:
       echo &" block1: {tag.readBlock(1).escape}"
-      events.send(CardEvent(uid: tag.uid(), body: tag.readBlock(1),
+      self.events[].send(CardEvent(uid: tag.uid(), body: tag.readBlock(1),
                             appeared: true))
       #tag.writeBlock(1, toBlock("helloworld"))
     finally:
       tag.disconnect()
 
   for uid in self.nearbyUids.difference(nearThisPass):
-    events.send(CardEvent(uid: uid, appeared: false))
+    self.events[].send(CardEvent(uid: uid, appeared: false))
 
   self.nearbyUids = nearThisPass
 
@@ -68,32 +83,41 @@
     self.dev.destroy()
 
   
-proc watchForever*(self: TagWatcher) {.thread.} =
+proc watchForever*(args: tuple[self: TagWatcher, events: ref CardEventChannel]) {.thread.} =
+  args.self.events = args.events
   while true:
     try:
-      self.scanTags()
+      args.self.scanTags()
     except IOError:
       echo "IOError: restarting nfc now"
-  
+
 
-proc tagsToGraph() {.thread.} =
+type TtgArgs = tuple[events: ref CardEventChannel, server: GraphServer]
+proc tagsToGraph(args: TtgArgs) {.thread.} =
+  var events = args.events[]
   while true:
     echo "wait for event"
-    echo &"ev {events.recv()}"
-  
+    let ev = events.recv()
+    if ev.appeared:
+      args.server.setGraph()
+    else:
+      args.server.setGraph()
 
 proc main() =
-  events.open()
 
-  var tw = initTagWatcher()
-  var thr: Thread[void]
-  thr.createThread(tw.watchForever)
+  var events: ref CardEventChannel
+  new(events)
+  events[].open()
 
-  var t2: Thread[void]
-  t2.createThread(tagsToGraph)
-  
+  var tw = initFakeTagWatcher(events)
+  var thr: Thread[tw.type]
+  thr.createThread(watchForever, tw)
 
   let server = newGraphServer(port = 10012)
+
+  var t2: Thread[TtgArgs]
+  t2.createThread(tagsToGraph, (events, server))
+  
   server.run()
 
 main()