Created
March 15, 2022 16:33
-
-
Save benevpi/f09ba0ec3e606438f2ec31877050a3ef 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_invader_row(data, x, y, num): | |
for i in range(num): | |
draw_invader(data, x+(i*12), y) | |
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_row(data, 0,0,5) | |
draw_invader_row(data, 0,0,5) | |
draw_square(data, 0,0,10,5, 20) | |
sm = rp2.StateMachine(0, two_seven_bit_dac, freq=10000000, 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_ship = 0 | |
y_ship = 10 | |
x = 20 | |
y = 50 | |
while True: | |
x = x+x_direction | |
y = 50 | |
if (x > 50) : | |
x = 50 | |
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) | |
draw_invader_row(second_data, x,y,5) | |
draw_invader_row(second_data, x,y+10,5) | |
draw_square(data, x_ship+10,y_ship+5,x_ship,y_ship,20) | |
for i in range(len(second_data)): | |
packed_data[i] = second_data[i] | |
sleep(0.05) | |
_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