esp8266.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. def gpio(pin, mode='r', value=0,default=-1):
  2. pmap = {
  3. b'A0' : 0,
  4. b'D3' : 0,
  5. b'D10' : 1,
  6. b'LED' : 2, #inverted
  7. b'D4' : 2,
  8. b'D9' : 3,
  9. b'D2' : 4,
  10. b'D1' : 5,
  11. #6
  12. b'SD0' : 7,
  13. b'SD1' : 8,
  14. b'SD2' : 9,
  15. b'SDD2' : 9,
  16. b'SDD3' : 10,
  17. b'SD3' : 10,
  18. b'CMD' : 11,
  19. b'SDCMD' : 11,
  20. b'D6' : 12,
  21. b'HMISO' : 12,
  22. b'D7' : 13,
  23. b'HMOSI' : 13,
  24. b'D5' : 14,
  25. b'HCLK' : 14,
  26. b'D8' : 15,
  27. b'HCS' : 15,
  28. b'D0' : 16,
  29. }
  30. if isinstance(pin,str):
  31. pin = pin.upper()
  32. thepin = pmap.get( bytes(pin,'utf-8'), default)
  33. else:
  34. thepin = int(pin)
  35. if not thepin in use.GPIO:
  36. import machine
  37. if 'w' in mode:
  38. instance = machine.Pin(thepin,mode=machine.Pin.OUT,value=value)
  39. if 's' in mode:
  40. pdb('211:servo')
  41. if 'p' in mode:
  42. pdb('213:pwm')
  43. elif 'r' in mode:
  44. instance = machine.Pin(thepin,mode=machine.Pin.IN)
  45. else:
  46. pdb('27: miss pin (%S)%s mode' % (thepin, pin) )
  47. raise ValueError("invalid pin mode r/w != '%s'" % mode)
  48. use.GPIO[thepin] = instance
  49. return use.GPIO.get(thepin)