import RPi.GPIO as GPIO
from matplotlib import pyplot as plt
from matplotlib import animation
import numpy as np
import random
import time
import spidev
spi=spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz=1000000
def ReadChannel(channel):
adc=spi.xfer2([1,(8+channel)<<4,0])
data=((adc[1]&3)<<8)+adc[2]
return data
mcp3008_channel=0
fig = plt.figure() #figure(๋ํ) ์์ฑ
ax = plt.subplot(211, xlim=(0, 50), ylim=(0, 1024))
ax_2 = plt.subplot(212, xlim=(0, 50), ylim=(0, 512))
max_points = 50
max_points_2 = 50
line, = ax.plot(np.arange(max_points),
np.ones(max_points, dtype=np.float)*np.nan, lw=1, c='blue',ms=1)
line_2, = ax_2.plot(np.arange(max_points_2),
np.ones(max_points, dtype=np.float)*np.nan, lw=1,ms=1)
def init():
return line
def init_2():
return line_2
def animate(i):
y = ReadChannel(mcp3008_channel)
old_y = line.get_ydata()
new_y = np.r_[old_y[1:], y]
line.set_ydata(new_y)
print(new_y)
return line
def animate_2(i):
y_2 = random.randint(0,512)
old_y_2 = line_2.get_ydata()
new_y_2 = np.r_[old_y_2[1:], y_2]
line_2.set_ydata(new_y_2)
print(new_y_2)
return line_2
anim = animation.FuncAnimation(fig, animate , init_func= init ,frames=200, interval=50, blit=False)
anim_2 = animation.FuncAnimation(fig, animate_2 , init_func= init_2 ,frames=200, interval=10, blit=False)
plt.show()
๋ผ์ฆ๋ฒ ๋ฆฌํ์ด์์, ์ผ์๊ฐ์ ๋ฐ์ 2๊ฐ์ ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๋ ค๋ ๋ชฉ์ ์ด ์์์ต๋๋ค. (๋ค๋ง ์ผ์๊ฐ ํ๋๋ฟ์ด๋ผ ํ๋๋ ๋๋คํจ์๋ก ๋์ฒด)
์ ๊ทธ๋ํ : ๊ฐ๋ณ์ ํญ ์ผ์๊ฐ
์๋ : ๋๋คํจ์
* ์ ๋ฆฌํ ์ฝ๋
import RPi.GPIO as GPIO
from matplotlib import pyplot as plt
from matplotlib import animation
import numpy as np
import random, time, spidev
spi=spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz=1000000
def ReadChannel(channel):
adc=spi.xfer2([1,(8+channel)<<4,0])
data=((adc[1]&3)<<8)+adc[2]
return data
mcp3008_channel=0
fig = plt.figure()
ax = plt.subplot(211, xlim=(0, 50), ylim=(0, 1024))
ax_2 = plt.subplot(212, xlim=(0, 50), ylim=(0, 512))
max_points = 50
max_points_2 = 50
line, = ax.plot(np.arange(max_points),
np.ones(max_points, dtype=np.float)*np.nan, lw=1, c='blue',ms=1)
line_2, = ax_2.plot(np.arange(max_points_2),
np.ones(max_points, dtype=np.float)*np.nan, lw=1,ms=1)
def animate(i):
y = ReadChannel(mcp3008_channel)
# y = random.randint(0,1000)
old_y = line.get_ydata()
new_y = np.r_[old_y[1:], y]
line.set_ydata(new_y)
print(new_y)
return line
def animate_2(i):
y_2 = random.randint(0,512)
old_y_2 = line_2.get_ydata()
new_y_2 = np.r_[old_y_2[1:], y_2]
line_2.set_ydata(new_y_2)
print(new_y_2)
return line_2
anim = animation.FuncAnimation(fig, animate ,interval = 10)
anim_2 = animation.FuncAnimation(fig, animate_2 , interval=10)
plt.show()