Skip to content

Instantly share code, notes, and snippets.

@audioplastic
Created February 22, 2021 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audioplastic/02be99fbf0d6687d3cd96fe5d96dd2bd to your computer and use it in GitHub Desktop.
Save audioplastic/02be99fbf0d6687d3cd96fe5d96dd2bd to your computer and use it in GitHub Desktop.
Pi Pico test script to calculate primes and report status in built in LED every 20 seconds
from machine import Pin, Timer
import utime, math
led = Pin(25, Pin.OUT)
tim = Timer()
pIdx = 0;
x = 0;
latestPrime=0;
def is_prime(n: int) -> bool:
"""Primality test using 6k+-1 opt."""
if n<=3:
return n>1
if n%2 ==0 or n%3==0:
return False
i=5
while i** 2 <=n:
if n%i==0 or n%(i+2)==0:
return False
i+=6
return True
def dah():
led.value(1)
utime.sleep(0.25)
led.value(0)
utime.sleep(0.25)
def dit():
led.value(1)
utime.sleep(0.05)
led.value(0)
utime.sleep(0.25)
def alert_flicker():
for i in range(0,20):
led.value(1)
utime.sleep(0.02)
led.value(0)
utime.sleep(0.02)
utime.sleep(0.5)
def led_binary(myInt):
for char in bin(myInt)[2:]:
if char == '0':
dit()
else:
dah()
def led_dec(myInt):
for char in str(math.floor(myInt/1000.)):
if int(char)>0:
for i in range(int(char)):
dit()
else:
dah()
utime.sleep(0.5)
def disp_latest_prime(timer):
print("prime" , pIdx , " = " , latestPrime)
alert_flicker()
# led_binary(pIdx)
led_dec(pIdx)
led.value(0)
tim.init(freq=0.05, mode=Timer.PERIODIC, callback = disp_latest_prime)
while True:
x+=1
if is_prime(x):
latestPrime=x;
pIdx+=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment