Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Gemma MO: CircuitPython or MicroPython
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: Gemma MO: CircuitPython or MicroPython

by marine_hm on Sat Aug 26, 2017 8:29 am

Not sure how to word this... but, what I think I need: is to write several mini Python Scripts. Then run a main script that calls on the subscripts to run for a certain time, then pulls up the next, etc, then loops back around to the top and does it all over? Is that possible?

Or does it all have to be in one script?

marine_hm
 
Posts: 226
Joined: Wed Dec 23, 2015 12:56 am

Re: Gemma MO: CircuitPython or MicroPython

by danhalbert on Sat Aug 26, 2017 8:53 am

It doesn't have to be in one script, and it doesn't have to be split up. It's just what's convenient for you and your coding. It looks like you're using a script that defines functions wheel() and rainbow_cycle(). You can define more functions to do what you want. You can write some lower-level functions that get called from other functions, and then call a sequence of functions from your top-level while True: loop.

If you want to split functions up into logical groups, you can put some in other files, and then import them. At some point you may want to learn about classes and object-oriented programming. Do you want some pointers to tutorials/courses on basic programming in Python, or do you feel you are beyond that? Look for educational material that talks about structuring your code with functions and classes

danhalbert
 
Posts: 2134
Joined: Tue Aug 08, 2017 12:37 pm

Re: Gemma MO: CircuitPython or MicroPython

by marine_hm on Sat Aug 26, 2017 4:08 pm

Dan;

Yes pointers to USEFUL tutorials would be great! I am fairly new to programming(Self taught). Brand new to Python! The Adafruit Gemma MO is my first exposure Python.

Thanks!

Going over the topics from this site at the moment: https://automatetheboringstuff.com/

Better yet: https://docs.python.org/3/tutorial/index.html
Last edited by marine_hm on Tue Aug 29, 2017 8:13 am, edited 1 time in total.

marine_hm
 
Posts: 226
Joined: Wed Dec 23, 2015 12:56 am

Re: Gemma MO: CircuitPython or MicroPython

by marine_hm on Mon Aug 28, 2017 7:21 am

Will the Gemma AND 12 Neopixel Ring work off 3.7 Lipoly?


Yes, there are many projects that do that kind of thing, for example: https://learn.adafruit.com/kaleidoscope ... a/overview (using the original Trinket, but the principle is the same)

marine_hm
 
Posts: 226
Joined: Wed Dec 23, 2015 12:56 am

Re: Gemma MO: CircuitPython or MicroPython

by marine_hm on Tue Aug 29, 2017 11:12 am

marine_hm wrote:
Will the Gemma AND 12 Neopixel Ring work off 3.7 Lipoly?


Yes, there are many projects that do that kind of thing, for example: https://learn.adafruit.com/kaleidoscope ... a/overview (using the original Trinket, but the principle is the same)


I've read through the code about a dozen times... I think it's starting to make sense, but how does this translate to CircuitPython?

Let me play with it some more before you answer this.

In the above link: Switch mode{
Case 0
Case 1 Is that defining a class?

marine_hm
 
Posts: 226
Joined: Wed Dec 23, 2015 12:56 am

Re: Gemma MO: CircuitPython or MicroPython

by tannewt on Tue Aug 29, 2017 12:53 pm

The equivalent thing in Python is if/elif.

Code: Select all | TOGGLE FULL SIZE
if mode == 0:
  # color stuff
elif mode == 1:
  # color stuff


Switch/case is just a syntax that allows you to handle different values of a single variable (in this case mode). Python doesn't have it but the above pattern is functionally equivalent.

I hope that helps!

tannewt
 
Posts: 1830
Joined: Thu Oct 06, 2016 8:48 pm

Re: Gemma MO: CircuitPython or MicroPython

by marine_hm on Wed Aug 30, 2017 9:48 am

danhalbert wrote:It doesn't have to be in one script, and it doesn't have to be split up. It's just what's convenient for you and your coding. It looks like you're using a script that defines functions wheel() and rainbow_cycle(). You can define more functions to do what you want. You can write some lower-level functions that get called from other functions, and then call a sequence of functions from your top-level while True: loop.

If you want to split functions up into logical groups, you can put some in other files, and then import them. At some point you may want to learn about classes and object-oriented programming. Do you want some pointers to tutorials/courses on basic programming in Python, or do you feel you are beyond that? Look for educational material that talks about structuring your code with functions and classes

For now; I would like to leave it in 1 script until I can digest splitting it up into modules. I know this is way off but you can see what I'm trying to do.
Code: Select all | TOGGLE FULL SIZE
#                   Changes Modes
#
#   Change from Gradient, Flashy, Chase, Fade in-out random colors
#
#
#

import board
import neopixel
import time
pixpin = board.D0
numpix = 12
pixels = neopixel.NeoPixel(pixpin, numpix, brightness=.5, auto_write=False)


###### Gradient ######
def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if (pos < 0):
        return (0, 0, 0)
    if (pos > 255):
        return (0, 0, 0)
    if (pos < 85):
        return (int(pos*3), int(255 - (pos*3)), 0)
    elif (pos < 170):
        pos -= 85
        return (int(255 - pos*3), 0, int(pos*3))
    else:
        pos -= 170
        return (0, int(pos*3), int(255 - pos*3))
 
def rainbow_cycle(wait):
    for j in range(255):
        for i in range(len(pixels)):
            idx = int ((i * 256 / len(pixels)) + j)
            pixels[i] = wheel(idx & 255)
        pixels.show()
        time.sleep(wait)
 
try:
    while True:       
        rainbow_cycle(.0001) #transition speed?
except:
    pass
finally:
    for i in range(len(pixels)):
        pixels[i] = (0,0,0)
    pixels.show()

# SET TIMER TO RUN FOR: SAY, 60 SECONDS, THEN RUN THE NEXT MODE
time.sleep(60)



###### FLASHY ######

def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if (pos < 0):
        return (0, 0, 0)
    if (pos > 255):
        return (0, 0, 0)
    if (pos < 85):
        return (int(pos*17), int(255 - (pos*17)), 0)
    elif (pos < 170):
        pos -= 85
        return (int(255 - pos*17), 0, int(pos*17))
    else:
        pos -= 170
        return (0, int(pos*17), int(255 - pos*17))
 
def rainbow_cycle(wait):
    for j in range(255):
        for i in range(len(pixels)):
            idx = int ((i * 256 / len(pixels)) + j)
            pixels[i] = wheel(idx & 255)
        pixels.show()
        time.sleep(wait)
 
try:
    while True:       
        rainbow_cycle(.0001) #transition speed?
except:
    pass
finally:
    for i in range(len(pixels)):
        pixels[i] = (0,0,0)
    pixels.show()
time.sleep(60)


###### Chase ######
# I haven't found any script that looks like the LEDs are running in a circle

#For now, I need to focus on making these 2 cycle through and run over and over.

#return loop

marine_hm
 
Posts: 226
Joined: Wed Dec 23, 2015 12:56 am

Re: Gemma MO: CircuitPython or MicroPython

by danhalbert on Wed Aug 30, 2017 9:54 am

This looks fine. I don't see a reason to split it up if it's that simple.

Re tutorials: what is your preferred learning style? Books, online maerial, videos?

danhalbert
 
Posts: 2134
Joined: Tue Aug 08, 2017 12:37 pm

Re: Gemma MO: CircuitPython or MicroPython

by marine_hm on Wed Aug 30, 2017 10:34 am

online material, video. Books seem to be very ambiguous with their syntax, uses.

Thanks for taking time with me.

marine_hm
 
Posts: 226
Joined: Wed Dec 23, 2015 12:56 am

Re: Gemma MO: CircuitPython or MicroPython

by marine_hm on Thu Aug 31, 2017 10:22 am

I've played with it for a bit. I wanted a solid color-fade. There's probably a more efficient way of doing it, All I did was change the number of pixels on the board.

Code: Select all | TOGGLE FULL SIZE
import board
import neopixel
import time
 
pixpin = board.D0
numpix = 144
 
#led = DigitalInOut(D13)
#led.direction = Direction.OUTPUT
 
pixels = neopixel.NeoPixel(pixpin, numpix, brightness=.5, auto_write=False)
 
 
def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if (pos < 0):
        return (0, 0, 0)
    if (pos > 255):
        return (0, 0, 0)
    if (pos < 85):
        return (int(pos*3), int(255 - (pos*3)), 0)
    elif (pos < 170):
        pos -= 85
        return (int(255 - pos*3), 0, int(pos*3))
    else:
        pos -= 170
        return (0, int(pos*3), int(255 - pos*3))
 
def rainbow_cycle(wait):
    for j in range(255):
        for i in range(len(pixels)):
            idx = int ((i * 256 / len(pixels)) + j)
            pixels[i] = wheel(idx & 255)
        pixels.show()
        time.sleep(wait)
 
try:
    while True:       
        rainbow_cycle(.0001) #transition speed?
except:
    pass
finally:
    for i in range(len(pixels)):
        pixels[i] = (0,0,0)
    pixels.show()

marine_hm
 
Posts: 226
Joined: Wed Dec 23, 2015 12:56 am

Re: Gemma MO: CircuitPython or MicroPython

by danhalbert on Thu Aug 31, 2017 11:21 am

There are lots of examples of fancy patterns to do with NeoPixels, though most of the examples are written in various Arduino libraries. However, you should be able to understand what's being turned on and off and then convert the code to Python. It's an exercise!

I haven't done this kind of thing much at all, but see for example:
https://www.tweaking4all.com/hardware/a ... p-effects/
https://github.com/FastLED/FastLED/tree/master/examples

danhalbert
 
Posts: 2134
Joined: Tue Aug 08, 2017 12:37 pm

Re: Gemma MO: CircuitPython or MicroPython

by marine_hm on Thu Aug 31, 2017 1:08 pm

Oh, I think I get it. I noticed that when I type the code and ran it, it didn't work. I changed the [ brackets to ( and now it works. I will work it some more and report back. I do this on my free time which seems like never. It may take me a while.

Thanks. Mostly for making me learn rather than just giving me the code.

marine_hm
 
Posts: 226
Joined: Wed Dec 23, 2015 12:56 am

Re: Gemma MO: CircuitPython or MicroPython

by cpunches on Sun Sep 03, 2017 1:10 pm

I've been trying to use the circuitpython dotstar library, but I can't seem to even get the example to work on Gemma M0
Code: Select all | TOGGLE FULL SIZE
import dotstar
from digitalio import *
from board import *
import busio
import time

RED = 0x100000
#initialize uart to receive messages from bluetooth app
uart = busio.UART(D0, D2)

while True:
   data = uart.read(32)
   if data != None:
       datastr = "".join([chr(b) for b in data])
       print(datastr,end="")
        with dotstar.DotStar(APA102_MOSI, APA102_SCK, 1) as stars:
           stars[0] = RED
           stars.show()


Basically I'm going to be taking commands via a bluetooth app and programming a dotstar strand accordingly. I assume I'll have to take a dotstar color command from the UART, deinit the UART, then use the pin I was using for TX for clock or data on the dotstars (I can't seem to initialize the UART to only use one pin like UART(-1, D2) - very frustrating ;)

Anyway, my actual question is why do I get the following error from the dotstar library:
main.py output:
Traceback (most recent call last):
File "main.py", line 18, in <module>
File "main.py", line 17, in <module>
File "libraries/drivers/dotstar/dotstar.py", line 53, in __setitem__
TypeError: 'int' object is not subscriptable

I understand what the error is, but I can't figure out how to work with the library when I'm just using the example code block shown on github.

Code: Select all | TOGGLE FULL SIZE
class DotStar:
    """
    A sequence of dotstars.
    :param ~microcontroller.Pin data: The pin to output dotstar data on.
    :param ~microcontroller.Pin clock: The pin to output dotstar clock on.
    :param int n: The number of dotstars in the chain
    :param int bpp: Bytes per pixel (usually 3 or 4)
    :param float brightness: Brightness of the pixels between 0.0 and 1.0
    Example for Gemma M0:
    .. code-block:: python
        import dotstar
        from board import *
        RED = 0x100000
        with dotstar.DotStar(APA102_MOSI, APA102_SCK, 1) as pixels:
            pixels[0] = RED
            pixels.show()
    """


Any helpful hints?

Thanks.

cpunches
 
Posts: 4
Joined: Sun Sep 03, 2017 1:02 pm

Re: Gemma MO: CircuitPython or MicroPython

by tannewt on Sun Sep 03, 2017 1:16 pm

Hi cpunches,
Please file an issue here about single direction UART: https://github.com/adafruit/circuitpython/issues

The DotStar library is going to get some love this week from me. The short answer to why its wrong is that the example is wrong. Right now the code only works with a tuple. So, please give it a tuple like (10, 0, 0) instead.
Thanks,
Scott

tannewt
 
Posts: 1830
Joined: Thu Oct 06, 2016 8:48 pm

Re: Gemma MO: CircuitPython or MicroPython

by cpunches on Sun Sep 03, 2017 1:24 pm

Thank you Scott for the quick reply! Looking forward to any updates to the dotstar library too :)

tannewt2 wrote:Hi cpunches,
Please file an issue here about single direction UART: https://github.com/adafruit/circuitpython/issues

The DotStar library is going to get some love this week from me. The short answer to why its wrong is that the example is wrong. Right now the code only works with a tuple. So, please give it a tuple like (10, 0, 0) instead.
Thanks,
Scott

cpunches
 
Posts: 4
Joined: Sun Sep 03, 2017 1:02 pm

Please be positive and constructive with your questions and comments.


cron