Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

Trinket response time ?????
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Trinket response time ?????

by hitsware2 on Fri Mar 12, 2021 8:41 pm

Raspberry Pi 3 B + ........ Mu Editor ...... Trinket M0
Code is Python writing to Circuit Python .
Uses up - down ( keyboard ) arrow keys to set PWM frequency .
This works , but for some reason Trinket takes a long time to respond ??????
Code: Select all | TOGGLE FULL SIZE
import pygame
pygame.display.set_mode([1,1])
pwmfreq = 100
print("#### up - down arrow keys set pwmfreq ####")
while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP: pwmfreq = int(2*pwmfreq)
            if event.key == pygame.K_DOWN: pwmfreq = int(pwmfreq/2)
        elif event.type == pygame.KEYUP:
            fw=open("/media/pi/CIRCUITPY/code.py","w")########## open circuitpy
            fw.write("import board; import pwmio\n")############ write to circuitpy
            fw.write("pwmio.PWMOut(board.A2, duty_cycle=30000, frequency=")
            fw.write(str(pwmfreq)); fw.write(")\n"); fw.write("while True: pass")
            fw.close()########################################## close circuitpy
            fr=open("/media/pi/CIRCUITPY/code.py","r")########## open circuitpy
            print(fr.read())#################################### read back circuitpy
            fr.close()########################################## close circuitpy

hitsware2
 
Posts: 44
Joined: Fri Sep 11, 2020 9:54 pm

Re: Trinket response time ?????

by dastels on Fri Mar 12, 2021 9:06 pm

Well, that'll cause CircuitPython on the Trinket M0 to restart, reload, and run the written code. That takes a while. It would be better to talk to a program on the Trinket that reads commands (or just PWM updates) from USB and write that from the code on the Pi.

You can use sys.stdin.read(1) to read a single character from the USB connection.

If you have it read characters into a string until a CR or LF is read, then make sure they are all digits and convert the string to a number which is used to change the PWM... you can test it from a serial connection (i.e. like the REPL).

Dave

dastels
 
Posts: 6105
Joined: Tue Oct 20, 2015 3:22 pm

Re: Trinket response time ?????

by hitsware2 on Fri Mar 12, 2021 10:06 pm

If ( rather than using Mu ) ,
Code: Select all | TOGGLE FULL SIZE
 
import board; import pwmio
pwmio.PWMOut(board.A2, duty_cycle=30000, frequency=500)
while True: pass

I open circuitpy ...... edit the frequency value ....... then save it ......
There is a slight delay , but not much .
When I edit from Python ( with Mu ) ...... then read it back ......
It reads back the change immediately ...... but then sets awhile blinking ......
( ~ 1 / 2 min. or so ) then finally responds ?????
Is there a guide to the blink codes ?

hitsware2
 
Posts: 44
Joined: Fri Sep 11, 2020 9:54 pm

Re: Trinket response time ?????

by dastels on Fri Mar 12, 2021 10:44 pm


dastels
 
Posts: 6105
Joined: Tue Oct 20, 2015 3:22 pm

Re: Trinket response time ?????

by hitsware2 on Sat Mar 13, 2021 4:16 pm

!!!!!!! IT WAS SIMPLY INDENTATION !!!!!!!!!
The
Code: Select all | TOGGLE FULL SIZE
while True : pass

worked slowly , BUT
Code: Select all | TOGGLE FULL SIZE
while True :
     pass

Works as it should : )
Code: Select all | TOGGLE FULL SIZE
import pygame
pygame.display.set_mode([1,1])
pwmfreq = 100
print("#### up - down arrow keys set pwmfreq ####")
while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP: pwmfreq = int(2*pwmfreq)
            if event.key == pygame.K_DOWN: pwmfreq = int(pwmfreq/2)
        elif event.type == pygame.KEYUP:
            fw=open("/media/pi/CIRCUITPY/code.py","w")########## open circuitpy
            fw.write("import board; import pwmio\n")############ write to circuitpy
            fw.write("pwmio.PWMOut(board.A2, duty_cycle=30000, frequency=")
            fw.write(str(pwmfreq)); fw.write(")\n"); fw.write("while True:\n")
            fw.write("    pass")
            fw.close()########################################## close circuitpy
            fr=open("/media/pi/CIRCUITPY/code.py","r")########## open circuitpy
            print(fr.read())#################################### read back circuitpy
            fr.close()########################################## close circuitpy

hitsware2
 
Posts: 44
Joined: Fri Sep 11, 2020 9:54 pm

Re: Trinket response time ?????

by dastels on Sat Mar 13, 2021 7:09 pm

Ha. Oh python... you so silly.

Glad it was so simple to fix.

Dave

dastels
 
Posts: 6105
Joined: Tue Oct 20, 2015 3:22 pm

Re: Trinket response time ?????

by hitsware2 on Sat Mar 13, 2021 8:01 pm

Quit again though : (
Bizarre ....
I may try the Py Pico ......

hitsware2
 
Posts: 44
Joined: Fri Sep 11, 2020 9:54 pm

Re: Trinket response time ?????

by hitsware2 on Sun Mar 14, 2021 4:04 pm

This works :
Code: Select all | TOGGLE FULL SIZE
import pygame
pygame.display.set_mode([1,1])
freq = 100
print("#### up - down arrow keys to set freq ####")
while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP: freq = int(2*freq)
            if event.key == pygame.K_DOWN: freq = int(freq/2)
        elif event.type == pygame.KEYUP:
            fw=open("/media/pi/CIRCUITPY/code.py","w")######## open circuitpy
            fw.write("import board; import simpleio\n")####### write to circuitpy
            fw.write("while True :\n")
            fw.write("   simpleio.tone(board.A2,")
            fw.write(str(freq)); fw.write(")")
            fw.close()######################################## close circuitpy
            fr=open("/media/pi/CIRCUITPY/code.py","r")######## open circuitpy
            print(fr.read())################################## read back circuitpy
            fr.close()######################################## close circuitpy

hitsware2
 
Posts: 44
Joined: Fri Sep 11, 2020 9:54 pm

Re: Trinket response time ?????

by hitsware2 on Tue Mar 16, 2021 7:40 pm

Plaudits to the Pico !
Code: Select all | TOGGLE FULL SIZE
import pygame; pygame.display.set_mode([1,1]); t=0.1
print("#### up - down arrow keys to set freq ####")
while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP: t=t/2
            if event.key == pygame.K_DOWN: t=t*2
            print("frequency = " , 1 / (2 * t))
        elif event.type == pygame.KEYUP:
            fw=open("/media/pi/CIRCUITPY/code.py","w")######## open circuitpy
            fw.write("import board; import digitalio; import time\n")
            fw.write("led = digitalio.DigitalInOut(board.GP0)\n")
            fw.write("led.direction = digitalio.Direction.OUTPUT\n")
            fw.write("while True:\n")
            fw.write("     led.value = not led.value\n"); fw.write("     time.sleep(")
            fw.write(      str(t)); fw.write(")")     
            fw.close()######################################## close circuitpy
            fr=open("/media/pi/CIRCUITPY/code.py","r")######## open circuitpy
            print(fr.read())################################## read back circuitpy
            fr.close()######################################## close circuitpy

hitsware2
 
Posts: 44
Joined: Fri Sep 11, 2020 9:54 pm

Re: Trinket response time ?????

by hitsware2 on Tue Mar 16, 2021 8:28 pm

For some reason the Pi Pico does MUCH better here :
??????????????
Code: Select all | TOGGLE FULL SIZE
import pygame; pygame.display.set_mode([1,1]); t=0.1
print("#### up - down arrow keys to set freq ####")
while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP: t=t/2
            if event.key == pygame.K_DOWN: t=t*2
            print("frequency = " , 1 / (2 * t))
            print("VVVV message to CIRCUITPY VVVV")
        elif event.type == pygame.KEYUP:
            fw=open("/media/pi/CIRCUITPY/code.py","w")######## open circuitpy
            fw.write("import board; import digitalio; import time\n")
            fw.write("led = digitalio.DigitalInOut(board.A2)\n")#Trinket (Output A2)
           # fw.write("led = digitalio.DigitalInOut(board.GP0)\n")#Pi Pico (Output GP0)
            fw.write("led.direction = digitalio.Direction.OUTPUT\n")
            fw.write("while True:\n")
            fw.write("     led.value = not led.value\n"); fw.write("     time.sleep(")
            fw.write(      str(t)); fw.write(")")
            fw.close()######################################## close circuitpy
            fr=open("/media/pi/CIRCUITPY/code.py","r")######## open circuitpy
            print(fr.read())################################## read back circuitpy
            fr.close()######################################## close circuitpy

hitsware2
 
Posts: 44
Joined: Fri Sep 11, 2020 9:54 pm

Re: Trinket response time ?????

by dastels on Wed Mar 17, 2021 9:43 am

The Pico is a completely different design than the Trinket. The only common point is a Cortex-M0 core.

It's an interesting datapoint, though.
Dave

dastels
 
Posts: 6105
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.