In [ ]:
from pynq.board import LED
In [ ]:
from pynq.board import RGBLED
In [ ]:
from pynq.board import Switch
In [ ]:
from pynq.board import Button
In [ ]:
from pynq.board import LED, RGBLED, Switch, Button
In [ ]:
from pynq.board import LED
led = LED(0)
In [ ]:
led.on()
In [ ]:
led.off()
In [ ]:
led.toggle()
In [ ]:
print(led.read())
In [ ]:
from pynq.board import RGBLED
rgbled = RGBLED(4)
In [ ]:
value = 3
rgbled.on(value)
In [ ]:
rgbled.off()
In [ ]:
print(rgbled.read())
In [ ]:
from pynq.board import Button
button = Button(1)
In [ ]:
value = button.read()
print(value)
In [ ]:
from pynq.board import Switch
switch = Switch(0)
In [ ]:
print(switch.read())
In [ ]:
from pynq.board import LED
import time

led = LED(1)
for i in range(40):
    led.toggle()
    time.sleep(0.1)
In [ ]:
from pynq.board import LED, RGBLED
import time

MAX_LEDS = 4
MAX_RGB_LEDS = 2
MAX_RGB_VALUE = 8
leds = [LED(i) for i in range(MAX_LEDS)]
rgb_leds = [RGBLED(i + MAX_LEDS) for i in range(MAX_RGB_LEDS)]

for i in range(MAX_LEDS):
    leds[i].off()
    
for i in range(MAX_RGB_LEDS):
    rgb_leds[i].off()

for i in range(100):
    rgb_leds [i % MAX_RGB_LEDS].on(i % MAX_RGB_VALUE)
    leds[i % MAX_LEDS].toggle()
    time.sleep(0.1)

for i in range(MAX_LEDS):
    leds[i].off()
    
for i in range(MAX_RGB_LEDS):
    rgb_leds[i].off()
In [ ]:
from pynq.board import LED, Button
import time

N = 4
leds = [LED(i) for i in range(N)]
btCount = Button(0)
for i in range(N):
    leds[i].off()

count = 0

while(count < 16):
    
    # wait for button to be pressed
    while(not btCount.read()):
        time.sleep(0.1)
    while(btCount.read()):
        time.sleep(0.1)
    
    # update counter
    count = count + 1
    ledVals = [False] * N # reset all LEDs
    temp = count
    for i in range(0, N):
        if(temp % 2 == 1):
            ledVals[i] = True
        temp = temp // 2
    
    # update leds
    for i in range(0, N):
        if ledVals[i] == True:
            leds[i].on()
        else:
            leds[i].off()
    
for i in range(N):
    leds[i].off()
In [ ]:
from pynq.board import LED, RGBLED, Button, Switch
import time

BUTTON_NUM = 4
RGB_LED_NUM = 2
RGB_OFFSET = 4
SWITCH_NUM = 2

buttons = [Button(i) for i in range(BUTTON_NUM)]
rgb_leds = [RGBLED(i + RGB_OFFSET) for i in range(RGB_LED_NUM)]
switches = [Switch(i) for i in range(SWITCH_NUM)]

exit = 0

while(not exit):
    
    time.sleep(0.1)
    
    button_val = [False] * 3
    value = 0
    for i in range(BUTTON_NUM - 1):
        if buttons[i].read():
            button_val[i] = True
            value += 2 ** i
    
    for i in range(SWITCH_NUM):
        if switches[i].read():
            rgb_leds[i].on(value)
        else:
            rgb_leds[i].off()
    
    if buttons[BUTTON_NUM - 1].read():
        exit = 1

    

NumPy and matplotlib

In [ ]:
import numpy as np
a = np.array([1,2,3,4])
print("Array a: ")
print(a)
list1 = [5,6,7,8]
b = np.array(list1)
print("Array b: ")
print(b)
tuple1 = (9, 10, 11, 12)
c = np.array(tuple1)
print("Array c: ")
print(c)
list2 = [13, 14, 15, 16]
d = np.array([list1, list2])
print("Array d: ")
print(d)
In [ ]:
d.shape
In [ ]:
d.ndim
In [ ]:
d.itemsize
In [ ]:
d.strides
In [ ]:
array_zero = np.zeros((2, 3))
print(array_zero)
In [ ]:
array_ones = np.ones([4, 3])
print(array_ones)
In [ ]:
array_empty = np.empty((3, 3))
print(array_empty)
In [ ]:
array_identity = np.identity(3)
print(array_identity)
In [ ]:
array_eye = np.eye(3)
print(array_eye)
In [ ]:
array_eye = np.eye(2,3)
print(array_eye)
In [ ]:
array_eye = np.eye(2, k=1)
print(array_eye)
In [ ]:
a = np.array([0, 1, 2, 3, 4, 5])
print(a)
a = a.reshape((2, 3))
print(a)
In [ ]:
a = np.array([0, 1, 2, 3, 4, 5])
print(a)
a.resize((2, 3))
print(a)
In [ ]:
val1 = int(input("Number of rows: "))
val2 = int(input("Number of columns: "))
shape = (val1, val2)
size = val1 * val2
arr = np.arange(size)
print(arr)
new_arr = arr.reshape(shape)
print(new_arr)
In [ ]:
val1 = int(input("Number of rows: "))
val2 = int(input("Number of columns: "))
shape = (val1, val2)
size = val1 * val2
arr = np.arange(size)
print(arr)
arr.resize(shape)
print(arr)
In [ ]:
a = np.arange(6)
b = a[0:3]
c = a[:3]
print(a)
print(b)
print(c)
In [ ]:
a = np.arange(18)
a.resize((3, 6))
s = a[:2, 1:3]
print(a)
print(s)
In [ ]:
a = np.array([1, 2, 3, 4])
a + 1
In [ ]:
a - 1
In [ ]:
a * 2
In [ ]:
a / 2
In [ ]:
a ** 2
In [ ]:
a = np.array([1, 2, 3, 4])
b = np.ones(4) + 2
a + b
In [ ]:
a - b
In [ ]:
a * b
In [ ]:
2 ** a + 5
In [ ]:
np.sin(a)
In [ ]:
a = np.array([1, 2, 3, 4])
b = np.ones(4) + 2
a == b
In [ ]:
a > b
In [ ]:
np.array_equal(a, b)
In [ ]:
c = a
np.array_equal(a, c)
In [ ]:
a = np.array([0, 1, 1, 0], dtype=bool)
b = np.array([1, 0, 1, 1], dtype=bool)
np.logical_or(a, b)
In [ ]:
np.logical_and(a, b)
In [ ]:
np.random.seed(100)
np.random.random(4)
In [ ]:
np.random.uniform(1, 10, 5)
In [ ]:
mean = 0
std = 1
np.random.normal(mean, std, 5)
In [ ]:
%matplotlib inline

import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.show()
In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([20, 28, 12, 44])
plt.show()
In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([2, 6, 8, 10], [20, 28, 12, 44])
plt.show()
In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

xVal = np.arange(0, 5, 0.2)
yVal = np.exp(xVal)
plt.plot(xVal, yVal, 'r^')
plt.show()
In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

xVal = np.arange(0,np.pi*2,0.1)
plt.plot(xVal, np.sin(xVal), 'r-')
plt.plot(xVal, np.cos(xVal), 'b--')
plt.plot([0,np.pi*2], [0,0], 'k-')
plt.show()
In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

xVal = np.arange(0,np.pi*2,0.1)
plt.plot(xVal, np.sin(xVal), 'r-', xVal, np.cos(xVal), 'b--', [0,np.pi*2], [0,0], 'k-')
plt.show()
In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

def f(x):
    return 2*x*x

x = np.arange(0,2.1,0.1)
plt.plot(x, f(x), 'r-')
plt.title("Body kinematics")
plt.ylabel("Space [m]")
plt.xlabel("Time [s]")
plt.axis([-0.4,2.4,-0.8,8.8])
plt.grid(True)
plt.show()
In [ ]:
%matplotlib inline

import matplotlib.pyplot as plt
lines = plt.plot([2,4,6,8])
plt.setp(lines, 'linewidth', 0.5, 'marker', '^')
plt.show()
In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

xVal = np.arange(0,np.pi*2,0.1)
line1, = plt.plot(xVal, np.sin(xVal), 'r-')
line2, = plt.plot(xVal, np.cos(xVal), 'b--')
plt.legend([line1, line2], ['f1','f2'])
plt.show()
In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2

plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()
In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

def f(x):
    return np.cos(x) + x + 2

def g(x):
    return 2 * np.abs(x)

x = np.arange(-2,10.1,0.1)
fLine, = plt.plot(x,f(x))
gLine, = plt.plot(x,g(x))
plt.legend([fLine, gLine], ['f(x)','g(x)'])
plt.grid(True)
plt.show()
In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt

def tri(wa, wid, periods):
    steps = periods*2 + 1
    xVal = [x*wav/2.0 for x in range(steps)]
    yVal = [wid if i % 2 else 0 for i in range(steps)]
    return xVal, yVal

wav = float(input("Insert the wavelength: "))
wid = float(input("Insert the wave width: "))
periods = int(input("Insert the number of periods: "))

x, y = tri(wav, wid, periods)

plt.plot(x, y)
plt.grid(True)
plt.show()