<commit_before>from umqtt.simple import MQTTClient
from machine import Pin
from neopixel import NeoPixel
import ubinascii
import machine
import micropython
import network
# ESP8266 ESP-12 modules have blue, active-low LED on GPIO2, replace
# with something else if needed.
'''
light:
  - platform: mqtt
    name: test
    state_topic: 'led/status'
    command_topic: 'led/switch'
    brightness_state_topic: 'led/brightness/status'
    brightness_command_topic: 'led/brightness/set'
    rgb_state_topic: 'led/rgb/status'
    rgb_command_topic: 'led/rgb/set'
'''
# Wifi 
SSID = ""
PASS = ""

# Default MQTT server to connect to
SERVER = "192.168.0.14"
CLIENT_ID = ubinascii.hexlify(machine.unique_id())
STATE_TOPIC= b"led/status"
COMMAND_TOPIC = b"led/switch"
BRIGHTNESS_STATE_TOPIC = b'led/brightness/status'
BRIGHTNESS_COMMAND_TOPIC = b'led/brightness/set'
RGB_STATE_TOPIC = b'led/rgb/status'
RGB_COMMAND_TOPIC = b'led/rgb/set'

# Pin for NODEMCU and Neopixel
LED_PIN = 5
NUM_OF_LEDS = 6
PIN = Pin(LED_PIN, Pin.OUT)
NEOPIXEL = NeoPixel(PIN, NUM_OF_LEDS)
BRIGHTNESS_STATE = 0
STATE = 0       # I don't know but this is really important for some reason.
RGB_STATE = (0,0,0)

def publish_payload(topic, payload):
    '''publish to a topic with a payload'''
    c.publish(topic, str(payload))
    print("[Publish]\t%s => %s" % (topic, str(payload)))

def set_led(on=True, brightness=255, color=False):
    '''on is True or False
    brightness is 0-255
    color is list [red, green, blue]
    '''
    global RGB_STATE
    if not on:
        new = (0, 0, 0)
        smooth(RGB_STATE, new, 50)
    elif on:
        if color:
            red = int(color[0])
            green = int(color[1])
            blue = int(color[2])
            new = (red, green, blue)
            smooth(RGB_STATE, new, 75)
        else:
            new = (brightness, brightness, brightness)
            smooth(RGB_STATE, new, 75)

def smooth(old, new, smooth, transition=0):
    '''old is deprecated, probably going to be replaced by RGB_STATE
    new is the new color (RED, GREEN, BLUE)
    smooth is how many "frames" of color to create and display
    transition is seconds to transit from one color to the next
    transition is 0 right now, but will be implimented with MQTT JSON'''
    global RGB_STATE
    red = list(linspace(RGB_STATE[0], new[0], smooth))
    green = list(linspace(RGB_STATE[1], new[1], smooth))
    blue = list(linspace(RGB_STATE[2], new[2], smooth))
    for color in zip(red,green,blue):
        for led in range(NUM_OF_LEDS):
            NEOPIXEL[led] = color
        NEOPIXEL.write()
    RGB_STATE = new


def linspace(a,b, n=100):
    if n < 2:
        yield b
    diff = (float(b) - a)/(n - 1)
    for i in range(n):
        yield int(round(diff * i + a))

def sub_cb(topic, msg):
    global STATE
    if topic == COMMAND_TOPIC:
        if msg == b"ON":
            if STATE == 0:
                STATE = 1
                set_led(on=True, brightness=255, color=False)
                publish_payload(STATE_TOPIC, "ON")
                publish_payload(BRIGHTNESS_STATE_TOPIC, 255)
        elif msg == b"OFF":
            if STATE == 1:
                STATE = 0
                BRIGHTNESS_STATE = 0
                set_led(on=False)
                publish_payload(STATE_TOPIC, "OFF")
                publish_payload(BRIGHTNESS_STATE_TOPIC, 0)
    elif topic == BRIGHTNESS_COMMAND_TOPIC:
        brightness = int(msg)
        if brightness < 0 or brightness > 255:
            return<commit_msg>fix naming of some vars<commit_after>from umqtt.simple import MQTTClient
from machine import Pin
from neopixel import NeoPixel
import ubinascii
import machine
import micropython
import network
# ESP8266 ESP-12 modules have blue, active-low LED on GPIO2, replace
# with something else if needed.
'''
light:
  - platform: mqtt
    name: test
    state_topic: 'led/status'
    command_topic: 'led/switch'
    brightness_state_topic: 'led/brightness/status'
    brightness_command_topic: 'led/brightness/set'
    rgb_state_topic: 'led/rgb/status'
    rgb_command_topic: 'led/rgb/set'
'''
# Wifi 
SSID = ""
PASS = ""

# Default MQTT server to connect to
SERVER = "192.168.0.14"
CLIENT_ID = ubinascii.hexlify(machine.unique_id())
STATE_TOPIC= b"led/status"
COMMAND_TOPIC = b"led/switch"
BRIGHTNESS_STATE_TOPIC = b'led/brightness/status'
BRIGHTNESS_COMMAND_TOPIC = b'led/brightness/set'
RGB_STATE_TOPIC = b'led/rgb/status'
RGB_COMMAND_TOPIC = b'led/rgb/set'

# Pin for NODEMCU and Neopixel
LED_PIN = 5
NUM_OF_LEDS = 6
PIN = Pin(LED_PIN, Pin.OUT)
NEOPIXEL = NeoPixel(PIN, NUM_OF_LEDS)
BRIGHTNESS_STATE = 0
STATE = "OFF"       # I don't know but this is really important for some reason.
RGB_STATE = (0,0,0)

def publish_payload(topic, payload):
    '''publish to a topic with a payload'''
    c.publish(topic, str(payload))
    print("[Publish]\t%s => %s" % (topic, str(payload)))

def set_led(on=True, brightness=255, color=False):
    '''on is True or False
    brightness is 0-255
    color is list [red, green, blue]
    '''
    global RGB_STATE
    if not on:
        new = (0, 0, 0)
        smooth(RGB_STATE, new, 50)
    elif on:
        if color:
            red = int(color[0])
            green = int(color[1])
            blue = int(color[2])
            new = (red, green, blue)
            smooth(RGB_STATE, new, 75)
        else:
            new = (brightness, brightness, brightness)
            smooth(RGB_STATE, new, 75)

def smooth(old, new, smooth, transition=0):
    '''old is deprecated, probably going to be replaced by RGB_STATE
    new is the new color (RED, GREEN, BLUE)
    smooth is how many "frames" of color to create and display
    transition is seconds to transit from one color to the next
    transition is 0 right now, but will be implimented with MQTT JSON'''
    global RGB_STATE
    red = list(linspace(RGB_STATE[0], new[0], smooth))
    green = list(linspace(RGB_STATE[1], new[1], smooth))
    blue = list(linspace(RGB_STATE[2], new[2], smooth))
    for color in zip(red,green,blue):
        for led in range(NUM_OF_LEDS):
            NEOPIXEL[led] = color
        NEOPIXEL.write()
    RGB_STATE = new


def linspace(a,b, n=100):
    if n < 2:
        yield b
    diff = (float(b) - a)/(n - 1)
    for i in range(n):
        yield int(round(diff * i + a))

def sub_cb(topic, msg):
    global STATE
    if topic == COMMAND_TOPIC:
        if msg == b"ON" and STATE == "OFF":
            STATE = "ON"
            set_led(on=True, brightness=255, color=False)
            publish_payload(STATE_TOPIC, "ON")
            publish_payload(BRIGHTNESS_STATE_TOPIC, 255)
        elif msg == b"OFF" and STATE == "ON":
            STATE = "OFF"
            BRIGHTNESS_STATE = 0
            set_led(on=False)
            publish_payload(STATE_TOPIC, "OFF")
            publish_payload(BRIGHTNESS_STATE_TOPIC, 0)
    elif topic == BRIGHTNESS_COMMAND_TOPIC:
        brightness = int(msg)
        if brightness < 0 or brightness > 255:
            return
{'fraction_non_alphanumeric': 0.10878300803673938, 'fraction_numerical': 0.019661308840413318, 'mean_word_length': 2.752827140549273, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1480691', 'n_tokens_mistral': 2464, 'n_tokens_neox': 2322, 'n_words': 671}