Created
March 15, 2022 15:37
-
-
Save benevpi/f7b71cc49839158458290e5468d025e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from machine import Pin, ADC | |
import array | |
from rp2 import PIO, StateMachine, asm_pio | |
import uctypes | |
from uctypes import BF_POS, BF_LEN, UINT32, BFUINT32, struct | |
from math import floor, ceil | |
from time import sleep | |
import _thread | |
import gc | |
invader = [[0,0,1,0,0,0,0,0,1,0,0]] | |
invader.append([0,0,0,1,0,0,0,1,0,0,0]) | |
invader.append([0,0,1,1,1,1,1,1,1,0,0]) | |
invader.append([0,1,1,0,1,1,1,0,1,1,0]) | |
invader.append([1,1,1,1,1,1,1,1,1,1,1]) | |
invader.append([1,0,1,1,1,1,1,1,1,0,1]) | |
invader.append([1,0,1,0,0,0,0,0,1,0,1]) | |
invader.append([0,0,0,1,1,0,1,1,0,0,0]) | |
@asm_pio(out_init=(PIO.OUT_LOW, PIO.OUT_LOW,PIO.OUT_LOW,PIO.OUT_LOW,PIO.OUT_LOW,PIO.OUT_LOW,PIO.OUT_LOW,PIO.OUT_LOW, PIO.OUT_LOW,PIO.OUT_LOW,PIO.OUT_LOW,PIO.OUT_LOW,PIO.OUT_LOW,PIO.OUT_LOW), out_shiftdir=PIO.SHIFT_RIGHT) | |
def two_seven_bit_dac(): # also want FIFO join. | |
wrap_target() | |
label("loop") | |
pull() | |
out(pins, 14) [10] | |
jmp("loop") | |
wrap() | |
data_position = 0 | |
def add_point(data, x, y): | |
data.append(x << 7 | y) | |
def draw_invader(data, x, y): | |
for i in range(len(invader)): | |
for j in range(len(invader[i])): | |
if invader[i][j] == 1: | |
add_point(data, x+j, y-i) | |
def draw_square(data, x1, y1, x2, y2, points_per_line): | |
width_inc = (x1-x2) / points_per_line | |
height_inc = (y1-y2) / points_per_line | |
for i in range(points_per_line): | |
add_point(data, x1, int(y2+i*height_inc)) | |
for i in range(points_per_line): | |
add_point(data, x2, int(y2+i*height_inc)) | |
for i in range(points_per_line): | |
add_point(data, int(x2+i*width_inc), y1) | |
for i in range(points_per_line): | |
add_point(data, int(x2+i*width_inc), y2) | |
data=[] | |
draw_invader(data, 0,0) | |
sm = rp2.StateMachine(0, two_seven_bit_dac, freq=100000, out_base=Pin(0)) | |
sm.active(1) | |
packed_data = array.array('I', [0 for _ in range(len(data))]) | |
for i in range(len(data)): | |
packed_data[i] = data[i] | |
def second_core(): | |
global packed_data | |
x_direction = 1 | |
y_direction = 1 | |
x = 20 | |
y = 0 | |
while True: | |
x = x+x_direction | |
y = y+y_direction | |
if (x > 70) : | |
x = 70 | |
x_direction = -1 | |
if (x < 0): | |
x_direction = 1 | |
x=0 | |
if (y > 70) : | |
y = 70 | |
y_direction = -1 | |
if (y < 0): | |
y_direction = 1 | |
y=0 | |
second_data = [] | |
gc.collect() # needed! | |
draw_invader(second_data, x, y) | |
for i in range(len(second_data)): | |
packed_data[i] = second_data[i] | |
sleep(0.1) | |
_thread.start_new_thread(second_core, ()) | |
while True: | |
sm.put(packed_data,0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment