Advertisement
MrBartek

Main.py secureapp

Dec 17th, 2021
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import time
  4. from rpi_ws281x import *
  5.  
  6. import smbus
  7. from time import sleep
  8. #import w1thermsensor
  9.  
  10. # LED strip configuration:
  11. LED_COUNT = 16
  12. LED_PIN = 18
  13. LED_FREQ_HZ = 800000
  14. LED_DMA = 10
  15. LED_BRIGHTNESS = 255
  16. LED_INVERT = False
  17. LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
  18.  
  19.  
  20. #some MPU6050 Registers and their Address
  21. PWR_MGMT_1 = 0x6B
  22. SMPLRT_DIV = 0x19
  23. CONFIG = 0x1A
  24. GYRO_CONFIG = 0x1B
  25. INT_ENABLE = 0x38
  26. ACCEL_XOUT_H = 0x3B
  27. ACCEL_YOUT_H = 0x3D
  28. ACCEL_ZOUT_H = 0x3F
  29. GYRO_XOUT_H = 0x43
  30. GYRO_YOUT_H = 0x45
  31. GYRO_ZOUT_H = 0x47
  32.  
  33.  
  34. def MPU_Init():
  35. bus.write_byte_data(Device_Address, SMPLRT_DIV, 7)
  36. bus.write_byte_data(Device_Address, PWR_MGMT_1, 1)
  37. bus.write_byte_data(Device_Address, CONFIG, 0)
  38. bus.write_byte_data(Device_Address, GYRO_CONFIG, 24)
  39. bus.write_byte_data(Device_Address, INT_ENABLE, 1)
  40.  
  41. def read_raw_data(addr):
  42. high = bus.read_byte_data(Device_Address, addr)
  43. low = bus.read_byte_data(Device_Address, addr+1)
  44.  
  45. value = ((high << 8) | low)
  46.  
  47. if(value > 32768):
  48. value = value - 65536
  49. return value
  50.  
  51. def reset():
  52. for i in range(strip.numPixels()):
  53. strip.setPixelColor(i,Color(0,0,0))
  54.  
  55. # Define functions which animate LEDs in various ways.
  56. def colorWipe(strip, color, wait_ms=50):
  57. """Wipe color across display a pixel at a time."""
  58. for i in range(strip.numPixels()):
  59. strip.setPixelColor(i, color)
  60. strip.show()
  61. time.sleep(wait_ms/1000.0)
  62.  
  63. def theaterChase(strip, color, wait_ms=50, iterations=10):
  64. """Movie theater light style chaser animation."""
  65. for j in range(iterations):
  66. for q in range(3):
  67. for i in range(0, strip.numPixels(), 3):
  68. strip.setPixelColor(i+q, color)
  69. strip.show()
  70. time.sleep(wait_ms/1000.0)
  71. for i in range(0, strip.numPixels(), 3):
  72. strip.setPixelColor(i+q, 0)
  73.  
  74. def wheel(pos):
  75. """Generate rainbow colors across 0-255 positions."""
  76. if pos < 85:
  77. return Color(pos * 3, 255 - pos * 3, 0)
  78. elif pos < 170:
  79. pos -= 85
  80. return Color(255 - pos * 3, 0, pos * 3)
  81. else:
  82. pos -= 170
  83. return Color(0, pos * 3, 255 - pos * 3)
  84.  
  85. def rainbow(strip, wait_ms=20, iterations=1):
  86. """Draw rainbow that fades across all pixels at once."""
  87. for j in range(256*iterations):
  88. for i in range(strip.numPixels()):
  89. strip.setPixelColor(i, wheel((i+j) & 255))
  90. strip.show()
  91. time.sleep(wait_ms/1000.0)
  92.  
  93.  
  94.  
  95. if __name__ == '__main__':
  96. strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
  97. strip.begin()
  98. colorWipe(strip, Color(0, 255, 0))
  99.  
  100.  
  101. bus = smbus.SMBus(1)
  102. Device_Address = 0x68
  103. MPU_Init()
  104.  
  105. #sensor = w1thermsensor.W1ThermSensor()
  106.  
  107. print('Press Ctrl-C to quit.')
  108.  
  109. try:
  110. while True:
  111. #Read Accelerometer raw value
  112. acc_x = read_raw_data(ACCEL_XOUT_H)
  113. acc_y = read_raw_data(ACCEL_YOUT_H)
  114. acc_z = read_raw_data(ACCEL_ZOUT_H)
  115.  
  116. #Read Gyroscope raw value
  117. gyro_x = read_raw_data(GYRO_XOUT_H)
  118. gyro_y = read_raw_data(GYRO_YOUT_H)
  119. gyro_z = read_raw_data(GYRO_ZOUT_H)
  120.  
  121. #Full scale range +/- 250 degree/C as per sensitivity scale factor
  122. Ax = acc_x/16384.0
  123. Ay = acc_y/16384.0
  124. Az = acc_z/16384.0
  125.  
  126. Gx = gyro_x/131.0
  127. Gy = gyro_y/131.0
  128. Gz = gyro_z/131.0
  129. print ("Gx=%.2f" %Gx, u'\u00b0'+ "/s", "\tGy=%.2f" %Gy, u'\u00b0'+ "/s", "\tGz=%.2f" %Gz, u'\u00b0'+ "/s", "\tAx=%.2f g" %Ax, "\tAy=%.2f g" %Ay, "\tAz=%.2f g" %Az)
  130.  
  131. #if Gx>=-1 or Gx>=1:
  132. if Gx<-1.0:
  133. theaterChase(strip, Color(255, 0, 0))
  134.  
  135. if Gy<-1:
  136. theaterChase(strip, Color(255, 0, 0))
  137.  
  138. if Gz<-1:
  139. theaterChase(strip, Color(255, 0, 0))
  140.  
  141.  
  142. #temp = sensor.get_temperature()
  143. #print(temp)
  144. reset()
  145.  
  146. except KeyboardInterrupt:
  147. colorWipe(strip, Color(0,0,0), 10)
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement