The Raspberry Pi Thread [6]


  1. Posts : 15,037
    Windows 10 IoT
    Thread Starter
       #1551

    Figured it out. Some may scratch there head at my code, but it works and does exactly what I wanted.
    Just had to do some math's.

    Code:
    import time
    import plasma
    import machine
    import micropython
    
    from plasma import plasma_stick
    from pimoroni_i2c import PimoroniI2C
    from pimoroni import BREAKOUT_GARDEN_I2C_PINS  # or PICO_EXPLORER_I2C_PINS or HEADER_I2C_PINS
    from breakout_encoder_wheel import BreakoutEncoderWheel, UP, DOWN, LEFT, RIGHT, CENTRE, NUM_BUTTONS, NUM_LEDS
    
    BUTTON_NAMES = ["Up", "Down", "Left", "Right", "Centre"]
    
    i2c = PimoroniI2C(plasma_stick.SDA, plasma_stick.SCL)
    
    wheel = BreakoutEncoderWheel(i2c)
    #position = 0
    last_pressed = [False] * NUM_BUTTONS
    pressed = [False] * NUM_BUTTONS
    
    GRB_LEDS = 144
    
    # WS2812 / NeoPixel™ LEDs
    led_strip = plasma.WS2812(GRB_LEDS, 0, 0, plasma_stick.DAT, color_order=plasma.COLOR_ORDER_GRB)
    led_strip.start()
    
    brightness = (0.5)
    
    red = (1 / 360)
    green = (130 / 360)
    blue = (250 / 360)
    yellow = (60 / 360)
    orange = (30 / 360)
    white = (1.0)
    
    while True:
        
        # Read all of the encoder wheel's buttons
        for b in range(NUM_BUTTONS):
            pressed[b] = wheel.pressed(b)
            if pressed[b] != last_pressed[b]:
                print(BUTTON_NAMES[b], "Pressed" if pressed[b] else "Released")
            last_pressed[b] = pressed[b]
    
        if pressed[UP]:
            wheel.clear()
            for i in range(NUM_LEDS):
                wheel.set_hsv(i, 1.0, 0, brightness)
            for s in range(GRB_LEDS):
                led_strip.set_hsv(s, 1.0,  0, brightness)
            wheel.set_hsv(0, 1.0, 0, brightness)
            wheel.set_hsv(6, red, 1.0, brightness)
            wheel.set_hsv(12, green, 1.0, brightness)
            wheel.set_hsv(18, blue, 1.0, brightness)
            color = white
    
        if pressed[RIGHT]:
            wheel.clear()
            for i in range(NUM_LEDS):
                wheel.set_hsv(i, red, 1.0, brightness)
            for s in range(GRB_LEDS):
                led_strip.set_hsv(s, red,  1.0, brightness) 
            wheel.set_hsv(0, 1.0, 0, brightness)
            wheel.set_hsv(6, red, 1.0, brightness)
            wheel.set_hsv(12, green, 1.0, brightness)
            wheel.set_hsv(18, blue, 1.0, brightness)
            color = red
            
        if pressed[DOWN]:
            wheel.clear()
            for i in range(NUM_LEDS):
                wheel.set_hsv(i, green, 1.0, brightness)
            for s in range(GRB_LEDS):
                led_strip.set_hsv(s, green,  1.0, brightness)
            wheel.set_hsv(0, 1.0, 0, brightness)
            wheel.set_hsv(6, red, 1.0, brightness)
            wheel.set_hsv(12, green, 1.0, brightness)
            wheel.set_hsv(18, blue, 1.0, brightness)
            color = green
            
        if pressed[LEFT]:
            wheel.clear()
            for i in range(NUM_LEDS):
                wheel.set_hsv(i, blue, 1.0, brightness)
            for s in range(GRB_LEDS):
                led_strip.set_hsv(s, blue,  1.0, brightness)         
            wheel.set_hsv(0, 1.0, 0, brightness)
            wheel.set_hsv(6, red, 1.0, brightness)
            wheel.set_hsv(12, green, 1.0, brightness)
            wheel.set_hsv(18, blue, 1.0, brightness)
            color = blue
            
        if pressed[CENTRE]:
            wheel.clear()
            for s in range(GRB_LEDS):
                led_strip.set_hsv(s, 0, 0, 0)              
    
            
        # Has the dial been turned since the last time we checked?
        change = wheel.delta()
        if change != 0:
            # Print out the direction the dial was turned, and the count
            if change > 0:
                print("Clockwise, Count =", wheel.count())
            else:
                print("Counter Clockwise, Count =", wheel.count())
    
            # Record the new position (from 0 to 23)
            position = wheel.count()
            '''         
            if position < 1:
                position = 1
                
            if position > 25:
                position = 25
            '''    
            print ("Position =", position)
            
            brightness = ((position + 25) / 50)
            if brightness < 0:
                brightness = 0
                
            if brightness > 1:
                brightness = 1
            
            print ("Brightness =", brightness)
            
            wheel.clear()
            
            if color == white:
                for i in range(NUM_LEDS):
                    wheel.set_hsv(i, 1.0, 0, brightness)
                for s in range(GRB_LEDS):
                    led_strip.set_hsv(s, 1.0,  0, brightness)         
    
            else:
                for i in range(NUM_LEDS):
                    wheel.set_hsv(i, color, 1.0, brightness)
                for s in range(GRB_LEDS):
                    led_strip.set_hsv(s, color,  1.0, brightness)         
    
            wheel.set_hsv(0, 1.0, 0, brightness)
            wheel.set_hsv(6, red, 1.0, brightness)
            wheel.set_hsv(12, green, 1.0, brightness)
            wheel.set_hsv(18, blue, 1.0, brightness)
    
        wheel.show()
      My Computer


  2. Posts : 15,037
    Windows 10 IoT
    Thread Starter
       #1552

    Next step is to have the buttons launch custom patterns. That's for another day. Really liking how it turned out and is currently working. Thinking of swapping out the 5V 4A supply for a 5V 10A supply.
    Then I can be "Blinded by the light" lol.
      My Computer


  3. Posts : 5,707
    insider build 10586.3 win10 pro 64
       #1553

    alphanumeric said:
    Figured it out. Some may scratch there head at my code, but it works and does exactly what I wanted.
    Just had to do some math's.

    Code:
    import time
    import plasma
    import machine
    import micropython
    
    from plasma import plasma_stick
    from pimoroni_i2c import PimoroniI2C
    from pimoroni import BREAKOUT_GARDEN_I2C_PINS  # or PICO_EXPLORER_I2C_PINS or HEADER_I2C_PINS
    from breakout_encoder_wheel import BreakoutEncoderWheel, UP, DOWN, LEFT, RIGHT, CENTRE, NUM_BUTTONS, NUM_LEDS
    
    BUTTON_NAMES = ["Up", "Down", "Left", "Right", "Centre"]
    
    i2c = PimoroniI2C(plasma_stick.SDA, plasma_stick.SCL)
    
    wheel = BreakoutEncoderWheel(i2c)
    #position = 0
    last_pressed = [False] * NUM_BUTTONS
    pressed = [False] * NUM_BUTTONS
    
    GRB_LEDS = 144
    
    # WS2812 / NeoPixel™ LEDs
    led_strip = plasma.WS2812(GRB_LEDS, 0, 0, plasma_stick.DAT, color_order=plasma.COLOR_ORDER_GRB)
    led_strip.start()
    
    brightness = (0.5)
    
    red = (1 / 360)
    green = (130 / 360)
    blue = (250 / 360)
    yellow = (60 / 360)
    orange = (30 / 360)
    white = (1.0)
    
    while True:
        
        # Read all of the encoder wheel's buttons
        for b in range(NUM_BUTTONS):
            pressed[b] = wheel.pressed(b)
            if pressed[b] != last_pressed[b]:
                print(BUTTON_NAMES[b], "Pressed" if pressed[b] else "Released")
            last_pressed[b] = pressed[b]
    
        if pressed[UP]:
            wheel.clear()
            for i in range(NUM_LEDS):
                wheel.set_hsv(i, 1.0, 0, brightness)
            for s in range(GRB_LEDS):
                led_strip.set_hsv(s, 1.0,  0, brightness)
            wheel.set_hsv(0, 1.0, 0, brightness)
            wheel.set_hsv(6, red, 1.0, brightness)
            wheel.set_hsv(12, green, 1.0, brightness)
            wheel.set_hsv(18, blue, 1.0, brightness)
            color = white
    
        if pressed[RIGHT]:
            wheel.clear()
            for i in range(NUM_LEDS):
                wheel.set_hsv(i, red, 1.0, brightness)
            for s in range(GRB_LEDS):
                led_strip.set_hsv(s, red,  1.0, brightness) 
            wheel.set_hsv(0, 1.0, 0, brightness)
            wheel.set_hsv(6, red, 1.0, brightness)
            wheel.set_hsv(12, green, 1.0, brightness)
            wheel.set_hsv(18, blue, 1.0, brightness)
            color = red
            
        if pressed[DOWN]:
            wheel.clear()
            for i in range(NUM_LEDS):
                wheel.set_hsv(i, green, 1.0, brightness)
            for s in range(GRB_LEDS):
                led_strip.set_hsv(s, green,  1.0, brightness)
            wheel.set_hsv(0, 1.0, 0, brightness)
            wheel.set_hsv(6, red, 1.0, brightness)
            wheel.set_hsv(12, green, 1.0, brightness)
            wheel.set_hsv(18, blue, 1.0, brightness)
            color = green
            
        if pressed[LEFT]:
            wheel.clear()
            for i in range(NUM_LEDS):
                wheel.set_hsv(i, blue, 1.0, brightness)
            for s in range(GRB_LEDS):
                led_strip.set_hsv(s, blue,  1.0, brightness)         
            wheel.set_hsv(0, 1.0, 0, brightness)
            wheel.set_hsv(6, red, 1.0, brightness)
            wheel.set_hsv(12, green, 1.0, brightness)
            wheel.set_hsv(18, blue, 1.0, brightness)
            color = blue
            
        if pressed[CENTRE]:
            wheel.clear()
            for s in range(GRB_LEDS):
                led_strip.set_hsv(s, 0, 0, 0)              
    
            
        # Has the dial been turned since the last time we checked?
        change = wheel.delta()
        if change != 0:
            # Print out the direction the dial was turned, and the count
            if change > 0:
                print("Clockwise, Count =", wheel.count())
            else:
                print("Counter Clockwise, Count =", wheel.count())
    
            # Record the new position (from 0 to 23)
            position = wheel.count()
            '''         
            if position < 1:
                position = 1
                
            if position > 25:
                position = 25
            '''    
            print ("Position =", position)
            
            brightness = ((position + 25) / 50)
            if brightness < 0:
                brightness = 0
                
            if brightness > 1:
                brightness = 1
            
            print ("Brightness =", brightness)
            
            wheel.clear()
            
            if color == white:
                for i in range(NUM_LEDS):
                    wheel.set_hsv(i, 1.0, 0, brightness)
                for s in range(GRB_LEDS):
                    led_strip.set_hsv(s, 1.0,  0, brightness)         
    
            else:
                for i in range(NUM_LEDS):
                    wheel.set_hsv(i, color, 1.0, brightness)
                for s in range(GRB_LEDS):
                    led_strip.set_hsv(s, color,  1.0, brightness)         
    
            wheel.set_hsv(0, 1.0, 0, brightness)
            wheel.set_hsv(6, red, 1.0, brightness)
            wheel.set_hsv(12, green, 1.0, brightness)
            wheel.set_hsv(18, blue, 1.0, brightness)
    
        wheel.show()
    great job, Cape Breton Code
      My Computer


  4. Posts : 15,037
    Windows 10 IoT
    Thread Starter
       #1554

    This time around I actually know what most of the code does. The HSV stuff is hard (for me at least) to wrap your head around. I have no choice though, as the RGB LED's in my strip, don't support a global brightness function in the standard way. Sometimes it's, work with what you got, or spend more money. I chose the former over the later.
    Just have to pick some nice patterns from the Pimoroni Plasma Stick examples, and code them in to work on a button press on the Encoder Wheel. I'll try and get some pics, but my camera has a hard time with illuminated led's. They just bloom in the picture or it refuses to focus. I may try my Pi HD camera, it has the adjustable aperture (F Stop in camera speak).
      My Computer


  5. Posts : 5,707
    insider build 10586.3 win10 pro 64
       #1555

    just going through some old orders on amazon ,and it shows this case ,cant find it around and i dont remeber it .didnt order it for you by any chance , lol 503 - Service Unavailable Error
      My Computer


  6. Posts : 15,037
    Windows 10 IoT
    Thread Starter
       #1556

    No, it wasn't me. Not this time.
      My Computer


  7. Posts : 305
    Win 10 and 11
       #1557

    I redid the serial connections from the PC to the Pi so that I can use RTS/CTS signalling to keep the Pi from being overrun from the PC's data stream. I am going to try to use the signals to gate the information going into the Pi. I have to fiddle with PySerial to get this to work. Hopefully it will. If it doesn't, I am going to have to use a timer in the PC application to space out the data in the stream. The idea is to have the PC ask if the Pi is ready for data, and have the Pi answer "yes" or "no". If yes, the PC will dump a data packet into the Pi for processing, otherwise it will wait for a yes signal when the Pi is ready.

    I am not keen on using a timer for the PC app as it will chew up CPU cycles and add some complexity that I do not want to have in the PC code. The first thing I am going to try though is to have the PC just barf out strings of data every 500 mS and see if the Pi can process it that fast. (Perhaps 1 sec intervals). If that doesn't work, I will fiddle with the RTS/CTS lines and see if that will help the timing.

    On a more positive note, I got my icons finished and sized. They are stored as little bitmaps that the Pi loads for every page. One for CPU, one for GPU, a little fan icon for the fans page, etc.

    I am getting this built slowly. I am finding the Python rather difficult, though. More study is needed. The PC code was a snap. I wish the Pi was easier to program. I am using Visual Studio Code with Python extensions, so I have Intellisense helping me not make syntax errors, and it interfaces directly with the Pi over the network. VS Code executes the Python directly on the Pi, so that is really handy. Sure is better than having a second keyboard and a dedicated monitor, or even worse, trying to write code on the Pi's little 7 inch monitor. My eyes aren't that good. lol
      My Computers


  8. Posts : 15,037
    Windows 10 IoT
    Thread Starter
       #1558

    @ Catnip, yeah, the Official 7 inch touch screen resolution can be hard on the eyes. And I whole heartily agree that Python is a challenge. It is for me anyway. Indents are a PITA at times. Placement / order of code is big factor too.

    I have the rainbow pattern added as an option for my desktop lighting. I even managed to get the brightness to work on the fly for it. The pattern shows up on the LED ring as well as the LED strip, which "I think" is cool. If your going to show off, show off in stile. lol.

    Pressing Up gets me White, Left is RED, Right is Blue, and Down is Rainbow (was originally Green). Center Button turns all the LED’s off. Turning the encoder wheel clockwise increases the brightness, and counter clockwise lowers it.

    Code:
    import time
    import plasma
    import machine
    import micropython
    
    from plasma import plasma_stick
    from pimoroni_i2c import PimoroniI2C
    from pimoroni import BREAKOUT_GARDEN_I2C_PINS  # or PICO_EXPLORER_I2C_PINS or HEADER_I2C_PINS
    from breakout_encoder_wheel import BreakoutEncoderWheel, UP, DOWN, LEFT, RIGHT, CENTRE, NUM_BUTTONS, NUM_LEDS
    
    BUTTON_NAMES = ["Up", "Down", "Left", "Right", "Centre"]
    
    i2c = PimoroniI2C(plasma_stick.SDA, plasma_stick.SCL)
    
    wheel = BreakoutEncoderWheel(i2c)
    last_pressed = [False] * NUM_BUTTONS
    pressed = [False] * NUM_BUTTONS
    
    STRIP_LEDS = 144
    SPEED = 20
    UPDATES = 60
    
    # WS2812 / NeoPixel™ LEDs
    led_strip = plasma.WS2812(STRIP_LEDS, 0, 0, plasma_stick.DAT, color_order=plasma.COLOR_ORDER_GRB)
    led_strip.start()
    
    offset = 0.0
    
    brightness = (0.5)
    
    pattern = 0
    
    red = (1 / 360)
    green = (130 / 360)
    blue = (250 / 360)
    yellow = (60 / 360)
    orange = (30 / 360)
    white = (1.0)
    
    while True:
        
        # Read all of the encoder wheel's buttons
        for b in range(NUM_BUTTONS):
            pressed[b] = wheel.pressed(b)
            if pressed[b] != last_pressed[b]:
                print(BUTTON_NAMES[b], "Pressed" if pressed[b] else "Released")
            last_pressed[b] = pressed[b]
    
        if pressed[UP]:
            pattern = 0
            wheel.clear()
            for i in range(NUM_LEDS):
                wheel.set_hsv(i, 1, 0, brightness)
            for s in range(STRIP_LEDS):
                led_strip.set_hsv(s, 1, 0, brightness)
            wheel.set_hsv(0, 1.0, 0, 1.0)
            wheel.set_hsv(6, blue, 1.0, 1.0)
            wheel.set_hsv(11, blue, 1.0, 1.0)
            wheel.set_hsv(12, green, 1.0, 1.0)
            wheel.set_hsv(13, red, 1.0, 1.0)
            wheel.set_hsv(18, red, 1.0, 1.0)
            color = white
    
        if pressed[RIGHT]:
            pattern = 0
            wheel.clear()
            for i in range(NUM_LEDS):
                wheel.set_hsv(i, blue, 1.0, brightness)
            for s in range(STRIP_LEDS):
                led_strip.set_hsv(s, blue,  1.0, brightness) 
            wheel.set_hsv(0, 1.0, 0, 1.0)
            wheel.set_hsv(6, blue, 1.0, 1.0)
            wheel.set_hsv(11, blue, 1.0, 1.0)
            wheel.set_hsv(12, green, 1.0, 1.0)
            wheel.set_hsv(13, red, 1.0, 1.0)
            wheel.set_hsv(18, red, 1.0, 1.0)
            color = blue
            
        if pressed[DOWN]:
            pattern = 1
            wheel.clear()
            '''
            for i in range(NUM_LEDS):
                wheel.set_hsv(i, green, 1.0, brightness)
            for s in range(STRIP_LEDS):
                led_strip.set_hsv(s, green,  1.0, brightness)
                
            wheel.set_hsv(0, 1.0, 0, 1.0)
            wheel.set_hsv(6, blue, 1.0, 1.0)
            wheel.set_hsv(11, blue, 1.0, 1.0)
            wheel.set_hsv(12, green, 1.0, 1.0)
            wheel.set_hsv(13, red, 1.0, 1.0)
            wheel.set_hsv(18, red, 1.0, 1.0)
            '''
            
        if pressed[LEFT]:
            pattern = 0
            wheel.clear()
            for i in range(NUM_LEDS):
                wheel.set_hsv(i, red, 1.0, brightness)
            for s in range(STRIP_LEDS):
                led_strip.set_hsv(s, red,  1.0, brightness)         
            wheel.set_hsv(0, 1.0, 0, 1.0)
            wheel.set_hsv(6, blue, 1.0, 1.0)
            wheel.set_hsv(11, blue, 1.0, 1.0)
            wheel.set_hsv(12, green, 1.0, 1.0)
            wheel.set_hsv(13, red, 1.0, 1.0)
            wheel.set_hsv(18, red, 1.0, 1.0)
            color = red
            
        if pressed[CENTRE]:
            pattern = 0
            wheel.clear()
            for s in range(STRIP_LEDS):
                led_strip.set_hsv(s, 0, 0, 0)              
    
            
        # Has the dial been turned since the last time we checked?
        change = wheel.delta()
        if change != 0:
            # Print out the direction the dial was turned, and the count
            if change > 0:
                print("Clockwise, Count =", wheel.count())
            else:
                print("Counter Clockwise, Count =", wheel.count())
    
            position = wheel.count()
            
            brightness = ((position + 25) / 50)
            
            if brightness < 0:
                brightness = 0
                
            if brightness > 1:
                brightness = 1
            
            print ("Brightness =", brightness)
            
            wheel.clear()
            
            if pattern == 0:
            
                if color == white:
                    for i in range(NUM_LEDS):
                        wheel.set_hsv(i, 1.0, 0, brightness)
                    for s in range(STRIP_LEDS):
                        led_strip.set_hsv(s, 1.0,  0, brightness)         
    
                else:
                    for i in range(NUM_LEDS):
                        wheel.set_hsv(i, color, 1.0, brightness)
                    for s in range(STRIP_LEDS):
                        led_strip.set_hsv(s, color,  1.0, brightness)         
    
                wheel.set_hsv(0, 1.0, 0, 1.0)
                wheel.set_hsv(6, blue, 1.0, 1.0)
                wheel.set_hsv(11, blue, 1.0, 1.0)
                wheel.set_hsv(12, green, 1.0, 1.0)
                wheel.set_hsv(13, red, 1.0, 1.0)
                wheel.set_hsv(18, red, 1.0, 1.0)
                
        if pattern == 1:
            SPEED = min(255, max(1, SPEED))
            offset += float(SPEED) / 2000.0
            offset %= 1
            
            for s in range(STRIP_LEDS):
                hue = (offset + float(s) / STRIP_LEDS) % 1
                led_strip.set_hsv(s, hue + offset, 1.0, brightness)
            for i in range(NUM_LEDS):
                wheel.set_hsv(i, hue + offset, 1.0, brightness)            
                
            time.sleep(1.0 / UPDATES)        
    
        wheel.show()
      My Computer


  9. Posts : 305
    Win 10 and 11
       #1559

    In an impressive feat of software engineering (at least for me) I am able to put icons on my Pi's screen now and have them controlled by the PC. When the PC says use a particular icon, the Pi displays it in the right spot, and changing the icon is successful. No glitching!

    Now I just have to read up on the PyGame library to learn how to do the bar graphs and other things I want to do, but I thought I would start with the eye candy and go from there. It's very satisfying to be able to tell something what to do and actually have it do it. Once I get the graphics and other display things figured out I can start writing the code for the Pi. The PC code is almost all done. Over all, this project is going much smoother than I thought it would. Yay!
      My Computers


  10. Posts : 15,037
    Windows 10 IoT
    Thread Starter
       #1560

    Found a glitch or two in my code while playing around with different button combo's. Also did some cleanup of repeated code that didn't need to be. Python and Micro Python is so unforgiving of a misplaced indent. Miss a tab and weird things happen, or what you wanted to happen doesn't.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 04:37.
Find Us




Windows 10 Forums