暫無描述

at42qt1070.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/python
  2. # Author: Zion Orent <zorent@ics.com>
  3. # Copyright (c) 2015 Intel Corporation.
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining
  6. # a copy of this software and associated documentation files (the
  7. # "Software"), to deal in the Software without restriction, including
  8. # without limitation the rights to use, copy, modify, merge, publish,
  9. # distribute, sublicense, and/or sell copies of the Software, and to
  10. # permit persons to whom the Software is furnished to do so, subject to
  11. # the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be
  14. # included in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. import time, sys, signal, atexit
  24. import pyupm_at42qt1070 as upmAt42qt1070
  25. # functions
  26. def printButtons(touchObj):
  27. buttonPressed = False
  28. buttons = touchObj.getButtons()
  29. sys.stdout.write("Buttons Pressed: ")
  30. for i in range(7):
  31. if (buttons & (1 << i)):
  32. sys.stdout.write(str(i) + " ")
  33. buttonPressed = True
  34. if (not buttonPressed):
  35. sys.stdout.write("None")
  36. print " "
  37. if (touchObj.isCalibrating()):
  38. print "Calibration is occurring."
  39. if (touchObj.isOverflowed()):
  40. print "Overflow was detected."
  41. # Global code that runs on startup
  42. I2C_BUS = upmAt42qt1070.AT42QT1070_I2C_BUS
  43. DEFAULT_I2C_ADDR = upmAt42qt1070.AT42QT1070_DEFAULT_I2C_ADDR
  44. # Instantiate an AT42QT1070 on I2C bus 0
  45. myTouchSensor = upmAt42qt1070.AT42QT1070(I2C_BUS,
  46. DEFAULT_I2C_ADDR)
  47. # Exit handlers
  48. def SIGINTHandler(signum, frame):
  49. raise SystemExit
  50. def exitHandler():
  51. print "Exiting"
  52. sys.exit(0)
  53. # This function lets you run code on exit, including functions from myTouchSensor
  54. atexit.register(exitHandler)
  55. # This function stops python from printing a stacktrace when you hit control-C
  56. signal.signal(signal.SIGINT, SIGINTHandler)
  57. # Print the button being touched every 0.1 seconds
  58. while(1):
  59. myTouchSensor.updateState()
  60. printButtons(myTouchSensor)
  61. time.sleep(.1)