Advertisement
Guest User

RGB Led

a guest
Aug 11th, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. from machine import Pin, PWM
  2. from time import sleep
  3.  
  4. PWM_MAX_DUTY = 1023
  5. PWM_MIN_DUTY = 0
  6. PWM_DUTY_RANGE = PWM_MAX_DUTY - PWM_MIN_DUTY
  7.  
  8.  
  9. def fraction_to_duty(fraction, reverse_polarity):
  10.     duty = int(round(PWM_DUTY_RANGE * fraction)) + PWM_MIN_DUTY
  11.     return PWM_MAX_DUTY - duty if reverse_polarity else duty
  12.  
  13.  
  14. def value_to_duty(value, max_value, reverse_polarity):
  15.     duty = int(round((PWM_DUTY_RANGE * value) / max_value)) + PWM_MIN_DUTY
  16.     return PWM_MAX_DUTY - duty if reverse_polarity else duty
  17.  
  18.  
  19. def unpack_color(color):
  20.     return color & 0xFF0000, color & 0x00FF00, color & 0x0000FF
  21.  
  22.  
  23. class RGBLed:
  24.     class Channel:
  25.         RED = 0
  26.         GREEN = 1
  27.         BLUE = 2
  28.  
  29.     def __init__(self, r_pin, g_pin, b_pin, inverse_polarity=False):
  30.         self._inverse_polarity = inverse_polarity
  31.         initial_duty = PWM_MAX_DUTY if self._inverse_polarity else 0
  32.         self._r_pwm = PWM(r_pin, freq=500, duty=initial_duty)
  33.         self._g_pwm = PWM(g_pin, freq=500, duty=initial_duty)
  34.         self._b_pwm = PWM(b_pin, freq=500, duty=initial_duty)
  35.         self._pwm_array = [self._r_pwm, self._g_pwm, self._b_pwm]
  36.  
  37.     def deinit(self):
  38.         [x.deinit() for x in self._pwm_array]
  39.  
  40.     def __enter__(self):
  41.         return self
  42.  
  43.     def __exit__(self, exc_type, exc_val, exc_tb):
  44.         self.deinit()
  45.  
  46.     def set_channel_color(self, value, channel):
  47.         pwm = self._pwm_array[channel]
  48.         pwm.duty(value_to_duty(value, 0xFF, self._inverse_polarity))
  49.  
  50.     def set_color(self, color):
  51.         channel_values = color if isinstance(color, list) else unpack_color(color)
  52.         for i, pwm in enumerate(self._pwm_array):
  53.             pwm.duty(value_to_duty(channel_values[i], 0xFF, self._inverse_polarity))
  54.  
  55.  
  56. def loop_colors(rgb_led, sleep_time=0.01):
  57.     color = [0xFF, 0, 0]
  58.     for color_dec in range(0, 2):
  59.         color_inc = color_dec + 1 if color_dec < 1 else 0
  60.         for i in range(0, 0xFF):
  61.             color[color_dec] -= 1
  62.             color[color_inc] += 1
  63.             print(color)
  64.             rgb_led.set_color(color)
  65.             sleep(sleep_time)
  66.  
  67.  
  68. def loop_colors_2(rgb_led, sleep_time=0.01):
  69.     max_color = 0xFF
  70.     rgb_led.set_color([0, 0, 0])
  71.     for color_pass in range(0, 2):
  72.         for color in range(0, max_color + 1):
  73.             rgb_led.set_channel_color(color if color_pass == 0 else max_color - color, RGBLed.Channel.RED)
  74.             sleep(sleep_time)
  75.  
  76.  
  77. def main():
  78.     r_pin = Pin(14, Pin.OUT)
  79.     g_pin = Pin(12, Pin.OUT)
  80.     b_pin = Pin(13, Pin.OUT)
  81.     with RGBLed(r_pin, g_pin, b_pin) as rgb_led:
  82.         while True:
  83.             loop_colors(rgb_led)
  84.  
  85.  
  86. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement