Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. # 2019-09-26 SK, MIT license
  2. """
  3. MicroPython code - nRF port
  4. Accessing the peripherals of the MPow/Desay DS-D6 wristband
  5. """
  6.  
  7. """
  8. pins.cvs content:
  9. USB_VOLT,P2
  10. BAT_VOLT,P3
  11. OLED_RES,P4
  12. OLED_SCK,P5
  13. OLED_MOSI,P6
  14. HEART_SCL,P7
  15. HEART_SDA,P8
  16. ACC_SCL,P13
  17. ACC_SDA,P14
  18. ACC_INT,P15
  19. UART_RX,P22
  20. UART_TX,P23
  21. MOTOR,P25
  22. HEART_EN,P26
  23. OLED_DC,P28
  24. OLED_CS,P29
  25. SWITCH,P30
  26. """
  27.  
  28.  
  29. ## Motor
  30. from machine import Pin
  31. from time import sleep_ms
  32. motor = Pin("MOTOR", Pin.OUT)
  33. motor.on()
  34. sleep_ms(1000)
  35. motor.off()
  36. # PWM TEST. Doesn't work
  37. # from machine import Pin, PWM
  38. # motor = Pin("MOTOR", Pin.OUT)
  39. # motor_pwm = PWM(motor, period = 16000, duty=16000)
  40. # motor_pwm.deinit()
  41.  
  42.  
  43. ## Switch
  44. from time import sleep_ms
  45. from machine import Pin
  46. switch = Pin("SWITCH", Pin.IN)
  47. count = 0
  48. while count<100:
  49. count += 1
  50. print(switch.value())
  51. sleep_ms(100)
  52.  
  53.  
  54. ## Voltages
  55. from time import sleep_ms
  56. from machine import Pin, ADC
  57. usb_volt_pin = Pin("USB_VOLT", Pin.IN)
  58. usb_volt_adc = ADC(usb_volt_pin)
  59. bat_volt_pin = Pin("BAT_VOLT", Pin.IN)
  60. bat_volt_adc = ADC(bat_volt_pin)
  61. inc2volt = 5/200 # 5 volts for 200 increments
  62. count = 0
  63. while count<100:
  64. count += 1
  65. print("USB voltage : {}V".format(inc2volt * usb_volt_adc.value()))
  66. print("Bat. voltage: {}V".format(inc2volt * bat_volt_adc.value()))
  67. sleep_ms(100)
  68.  
  69.  
  70. ## Screen SSD1306 128*32 OLED display with 64 line memory (requires slight modification of the standard library)
  71. from time import sleep_ms
  72. from machine import Pin, SPI
  73. from ssd1306dsd6 import SSD1306_SPI
  74. oled_sck = Pin("OLED_SCK")
  75. oled_mosi = Pin("OLED_MOSI")
  76. oled_spi = SPI("OLED_SPI", sck=oled_sck, mosi=oled_mosi) # OLED is on SPI bus 1. sck and mosi can be omitted as they are implicit.
  77. oled_dc = Pin("OLED_DC")
  78. oled_res = Pin("OLED_RES")
  79. oled_cs = Pin("OLED_CS")
  80. oled = SSD1306_SPI(width=128, height=32, spi=oled_spi, dc=oled_dc, res=oled_res, cs=oled_cs)
  81. oled.poweron()
  82. oled.write_cmd(0xc0) # To mirror screen
  83. oled.fill(0)
  84. oled.text('Hello', 0, 0)
  85. oled.text('World', 0, 10)
  86. oled.show()
  87. sleep_ms(5000)
  88. oled.poweroff()
  89.  
  90.  
  91. ## Heart Rate Sensor Pixart PAH8001EI-2G
  92. from machine import Pin, I2C
  93. from ustruct import unpack_from
  94. heart_scl = Pin("HEART_SCL")
  95. heart_sda = Pin("HEART_SDA")
  96. heart_en = Pin("HEART_EN", Pin.OUT)
  97. heart_en.on() # Turn ON heart rate sensor (i2c gets feedback + green LED shines)
  98. heart_i2c = I2C(0, scl=heart_scl, sda=heart_sda) # Heart rate sensor is on bus 0. scl and sda should be implicit. However omitting them yields an error.
  99. i2c_bus0_devices = heart_i2c.scan()
  100. print(i2c_bus0_devices) # Heart rate sensor address is 107
  101. heart_i2c_address = 107
  102. heart_i2c.writeto_mem(heart_i2c_address, 0x7f, 0x00) # Change to bank 0
  103. heart_prod_id1 = heart_i2c.readfrom_mem(heart_i2c_address, 0x00, 1) # Product ID1
  104. unpack_from("B", heart_prod_id1)[0] # 0x30=48
  105. heart_prod_id2 = heart_i2c.readfrom_mem(heart_i2c_address, 0x01, 1) # Product ID2
  106. hex(unpack_from("B", heart_prod_id2)[0]) # 0xdx
  107. heart_en.off()
  108.  
  109.  
  110. ## Accelerometer Kionix KX023
  111. from machine import Pin, I2C
  112. from array import array
  113. from time import sleep_ms
  114. acc_scl = Pin("ACC_SCL")
  115. acc_sda = Pin("ACC_SDA")
  116. acc_int = Pin("ACC_INT", Pin.OUT)
  117. acc_i2c = I2C(1, scl = acc_scl, sda = acc_sda) # Accelerometer is on bus 1. scl and sda should be implicit. However omitting them yields an error.
  118. i2c_bus1_devices = acc_i2c.scan()
  119. print(i2c_bus1_devices) # Acc i2c address is 0x1f=31
  120. acc_i2c_address = 31
  121. acc_whoami_address = 0x0f
  122. acc_buffer1 = array("B", [0]) # uint
  123. acc_i2c.readfrom_mem_into(acc_i2c_address, acc_whoami_address, acc_buffer1)
  124. acc_whoami_content = acc_buffer1[0]
  125. print(acc_whoami_content) # Must be 0x15=21
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement