event_thread.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. loop_times = 0
  13. def callBack1(signal):
  14. global callback_times
  15. callback_times += 1
  16. print('callback_times: %d' % callback_times)
  17. if signal == EVENT_SIGAL_IO_RISING_EDGE:
  18. print('get rising edge!')
  19. elif signal == EVENT_SIGAL_IO_FALLING_EDGE:
  20. print('get falling edge!')
  21. else:
  22. return signal
  23. io1.addEventCallBack(callBack1)
  24. def thread_test():
  25. print('thread_test')
  26. def insert_task():
  27. # complex json string
  28. for i in range(100):
  29. 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}'
  30. json_obj = json.loads(json_str)
  31. json_dump = json.dumps(json_obj)
  32. # launch a thread to enable evnet_thread
  33. # _thread.start_new_thread(thread_test, ())