changeset 1203:85e2f52b48af

fix event channel sharing. detects cards now Ignore-this: cbcca304a7b39a97179f9f51ac287a1 darcs-hash:128e9368b67bb26b0ca8af03ce7297c8e6f4da70
author drewp <drewp@bigasterisk.com>
date Thu, 21 Feb 2019 23:38:08 -0800
parents 3c5e5c9ac935
children c4aed42cfaa3
files service/rfid_pn532/rfid.nim
diffstat 1 files changed, 25 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/service/rfid_pn532/rfid.nim	Thu Feb 21 22:37:04 2019 -0800
+++ b/service/rfid_pn532/rfid.nim	Thu Feb 21 23:38:08 2019 -0800
@@ -18,30 +18,32 @@
   appeared: bool
 type CardEventChannel = Channel[CardEvent]
 
-type TagWatcher = ref object of RootObj
-  dev: NfcDevice
-  nearbyUids: HashSet[cstring]
-  events: ref CardEventChannel
+
+type FakeTagWatcher = ref object of RootObj
+  events: ptr CardEventChannel
   
-type FakeTagWatcher = ref object of RootObj
-  events: ref CardEventChannel
-  
-proc initFakeTagWatcher(events: ref  CardEventChannel): FakeTagWatcher =
+proc initFakeTagWatcher(events: ptr CardEventChannel): FakeTagWatcher =
   new result
   result.events = events
 
 proc watchForever*(self: FakeTagWatcher) {.thread.} =
-  var events = self.events[]
   while true:
     sleep(2000)
-    events.send(CardEvent(uid: "abcdef", body: "helloworld", appeared: true))
+    self.events[].send(CardEvent(uid: "abcdef", body: "helloworld", appeared: true))
     sleep(2000)
-    events.send(CardEvent(uid: "abcdef", appeared: false))
-  
-proc initTagWatcher(): TagWatcher =
+    self.events[].send(CardEvent(uid: "abcdef", appeared: false))
+
+   
+type TagWatcher = ref object of RootObj
+  dev: NfcDevice
+  nearbyUids: HashSet[cstring]
+  events: ptr CardEventChannel
+
+proc initTagWatcher(events: ptr CardEventChannel): TagWatcher =
   new(result)
   result.nearbyUids.init()
-  
+  result.events = events
+
 proc oneScan(self: TagWatcher) =
   var nearThisPass = initSet[cstring]()
 
@@ -73,7 +75,6 @@
 
   self.nearbyUids = nearThisPass
 
-
 proc scanTags(self: TagWatcher) =
   self.dev = newNfcDevice()
   try:
@@ -82,22 +83,19 @@
   finally:
     self.dev.destroy()
 
-  
-proc watchForever*(args: tuple[self: TagWatcher, events: ref CardEventChannel]) {.thread.} =
-  args.self.events = args.events
+proc watchForever*(self: TagWatcher) {.thread.} =
   while true:
     try:
-      args.self.scanTags()
+      self.scanTags()
     except IOError:
       echo "IOError: restarting nfc now"
 
-
-type TtgArgs = tuple[events: ref CardEventChannel, server: GraphServer]
+      
+type TtgArgs = tuple[events: ptr CardEventChannel, server: GraphServer]
 proc tagsToGraph(args: TtgArgs) {.thread.} =
-  var events = args.events[]
   while true:
     echo "wait for event"
-    let ev = events.recv()
+    let ev = args.events[].recv()
     if ev.appeared:
       args.server.setGraph()
     else:
@@ -105,18 +103,17 @@
 
 proc main() =
 
-  var events: ref CardEventChannel
-  new(events)
-  events[].open()
+  var events: CardEventChannel
+  events.open()
 
-  var tw = initFakeTagWatcher(events)
+  var tw = initTagWatcher(addr events)
   var thr: Thread[tw.type]
   thr.createThread(watchForever, tw)
 
   let server = newGraphServer(port = 10012)
 
   var t2: Thread[TtgArgs]
-  t2.createThread(tagsToGraph, (events, server))
+  t2.createThread(tagsToGraph, (addr events, server))
   
   server.run()