Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1.  
  2. def clear(np):
  3. for i in range(np.n):
  4. np[i] = (0, 0, 0)
  5. np.write()
  6.  
  7.  
  8. def white(np, brightness=255):
  9. for i in range(np.n):
  10. np[i] = (brightness, brightness, brightness)
  11. np.write()
  12.  
  13. def demo(np):
  14. import utime
  15.  
  16. n = np.n
  17.  
  18. # cycle
  19. for i in range(4 * n):
  20. for j in range(n):
  21. np[j] = (0, 0, 0)
  22. np[i % n] = (255, 255, 255)
  23. np.write()
  24. utime.sleep_ms(25)
  25.  
  26. # bounce
  27. for i in range(4 * n):
  28. for j in range(n):
  29. np[j] = (0, 0, 128)
  30. if (i // n) % 2 == 0:
  31. np[i % n] = (0, 0, 0)
  32. else:
  33. np[n - 1 - (i % n)] = (0, 0, 0)
  34. np.write()
  35. utime.sleep_ms(60)
  36.  
  37. # fade in/out
  38. for i in range(0, 4 * 256, 8):
  39. for j in range(n):
  40. if (i // 256) % 2 == 0:
  41. val = i & 0xff
  42. else:
  43. val = 255 - (i & 0xff)
  44. np[j] = (val, 0, 0)
  45. np.write()
  46.  
  47. # clear
  48. for i in range(n):
  49. np[i] = (0, 0, 0)
  50. np.write()
  51.  
  52. def slide(np):
  53. import utime
  54.  
  55. position = 0
  56. add = False
  57.  
  58. while True:
  59.  
  60. position += 1
  61. n = np.n
  62.  
  63.  
  64. if position > n:
  65. position = 0
  66.  
  67. brightness = 255
  68.  
  69. for i in range(n):
  70. pos = i + position
  71. if pos >= n:
  72. pos = pos - n
  73.  
  74. np[pos] = (brightness, brightness, brightness)
  75.  
  76. if brightness < 10:
  77. add = True
  78. elif brightness > 250:
  79. add = False
  80.  
  81. if add:
  82. brightness += 10
  83. else:
  84. brightness -= 10
  85.  
  86. np.write()
  87. utime.sleep_ms(10)
  88.  
  89. def gradient_light(np, position=0):
  90. color = [122, 255, 0]
  91. value = 255/np.n*2
  92. add = [True, False, True]
  93.  
  94. for pos in range(np.n):
  95. for i in range(len(color)):
  96. if color[i] < 10:
  97. add[i] = True
  98. elif color[i] > 245:
  99. add[i] = False
  100.  
  101. if add[i]:
  102. color[i] += value
  103. else:
  104. color[i] -= value
  105.  
  106. if position+pos >= np.n:
  107. pos = pos - np.n
  108.  
  109. np[position+pos] = tuple(int(i) for i in color)
  110.  
  111. np.write()
  112.  
  113. def gradient_light(np, position=0):
  114. color = [122, 255, 0]
  115. value = 255/np.n*2
  116. add = [True, False, True]
  117.  
  118. for pos in range(np.n):
  119. for i in range(len(color)):
  120. if color[i] < value:
  121. add[i] = True
  122. elif color[i] > 255-value:
  123. add[i] = False
  124.  
  125. if add[i]:
  126. color[i] += value
  127. else:
  128. color[i] -= value
  129.  
  130. if position+pos >= np.n:
  131. pos = pos - np.n
  132.  
  133. np[position+pos] = tuple(int(i) for i in color)
  134.  
  135. np.write()
  136.  
  137. def gradient_colorfull(np, position=0):
  138. r = 255
  139. g = 0
  140. b = 0
  141. value = 255/np.n*3
  142. r_add = -1
  143. g_add = +1
  144. b_add = 0
  145.  
  146. for pos in range(np.n):
  147. if r < value and r_add == -1:
  148. r_add = 0
  149. elif r > 255-value and r_add == +1:
  150. r_add = -1
  151. g_add = +1
  152.  
  153. if g < value and g_add == -1:
  154. g_add = 0
  155. elif g > 255-value and g_add == +1:
  156. g_add = -1
  157. b_add = +1
  158.  
  159. if b < value and b_add == -1:
  160. b_add = 0
  161. elif b > 255-value and b_add == +1:
  162. b_add = -1
  163. r_add = +1
  164.  
  165.  
  166. r += r_add * value
  167. g += g_add * value
  168. b += b_add * value
  169. if position+pos >= np.n:
  170. pos = pos - np.n
  171.  
  172. np[position+pos] = (int(r), int(g), int(b))
  173.  
  174. np.write()
  175.  
  176. def color_slide(np, gradient_f=gradient_colorfull):
  177. import utime
  178.  
  179. position = 0
  180.  
  181. n = np.n
  182.  
  183. while True:
  184. position += 1
  185.  
  186. if position > n:
  187. position = 0
  188.  
  189. gradient_f(np, position=position)
  190.  
  191. utime.sleep_ms(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement