i2c_console.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #! /usr/bin/env python3
  2. #
  3. # For etsting the Pi to Arduino i2c communication and
  4. # process.
  5. #
  6. #| Character | Function |
  7. #|+---------+|---------------------------------|
  8. #| w | Aim higher
  9. #| s | Aim lower
  10. #| a | Aim left
  11. #| d | Aim right
  12. #| r | Increase load arm (see note 1)
  13. #| f | reverse load arm (no effect)
  14. #| space | Stop all
  15. #| L | Move until load switch activates
  16. #| F | Fire and reload (see note 2)
  17. #
  18. #note 1: Arduino will stop automatically when the
  19. #load sensor comes on.
  20. #
  21. #note 2: For "Fire", if the load switch is not activated, it will perform a load
  22. #action first, and then move forward until the switch deactivates, and
  23. #then load again. The end result is that it is reloaded.
  24. import smbus
  25. import time
  26. bus = smbus.SMBus(1)
  27. # Match the arduino
  28. SLAVE_ADDRESS = 0x04
  29. #def writeNumber(value):
  30. # bus.write_byte(SLAVE_ADDRESS, value)
  31. # return -1
  32. #
  33. #def readNumber():
  34. # number = bus.read_byte(SLAVE_ADDRESS)
  35. # return number
  36. while True:
  37. var = input("Enter asdwrf, <space>, or F:")
  38. if not var:
  39. continue
  40. try:
  41. bus.write_byte(SLAVE_ADDRESS, ord(var[0]))
  42. print("Sent '{}' to pi.".format(ord(var[0])))
  43. except:
  44. print("Bus I/O Error. ATMega328 may not be powered up.")