event_thread.py 1011 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import TemplateDevice
  2. import time
  3. import _thread
  4. import json
  5. io1 = TemplateDevice.GPIO()
  6. io1.setPin('PA8')
  7. io1.setMode('in')
  8. io1.enable()
  9. EVENT_SIGAL_IO_RISING_EDGE = 0x01
  10. EVENT_SIGAL_IO_FALLING_EDGE = 0x02
  11. callback_times = 0
  12. def callBack1(signal):
  13. global callback_times
  14. callback_times += 1
  15. print('callback_times: %d' % callback_times)
  16. if signal == EVENT_SIGAL_IO_RISING_EDGE:
  17. print('get rising edge!')
  18. elif signal == EVENT_SIGAL_IO_FALLING_EDGE:
  19. print('get falling edge!')
  20. else:
  21. return signal
  22. io1.addEventCallBack(callBack1)
  23. def thread_test():
  24. print('thread_test')
  25. def insert_task():
  26. # complex json string
  27. for i in range(100):
  28. json_str = '{"a": 1, "b": 2, "c": 3, "d": {"e": 4, "f": 5}, "g": [6, 7, 8], "h": null, "i": false, "j": true, "k": "string", "l": 1.234}'
  29. json_obj = json.loads(json_str)
  30. json_dump = json.dumps(json_obj)
  31. # launch a thread to enable evnet_thread
  32. _thread.start_new_thread(thread_test, ())