Skip to content

Instantly share code, notes, and snippets.

@TakehikoShimojima
Last active August 12, 2018 08:19
Show Gist options
  • Select an option

  • Save TakehikoShimojima/5e104fff1002c247e111d9f117868269 to your computer and use it in GitHub Desktop.

Select an option

Save TakehikoShimojima/5e104fff1002c247e111d9f117868269 to your computer and use it in GitHub Desktop.
from m5stack import *
import units # センサーユニットのライブラリーunitsをインポート
import time
import ambient # Ambientライブラリーをインポート
machine.DAC(machine.Pin(25)).write(0) # スピーカーをオフにする
angle = units.ANGLE() # Angleセンサーのインスタンスを作る
am = ambient.Ambient(100, 'writeKey') # Ambientのインスタンスを作る
lcd.clear() # LCDを消去する
lastA = 0
lastT = time.time()
def menu(a = None, b = None, c = None): # ボタンの上にメニューを書く関数
_, y = lcd.fontSize()
y = 240 - y - 10
if a != None:
lcd.print(a, 40, y)
if b != None:
lcd.print(b, 135, y)
if c != None:
lcd.print(c, 225, y)
def toY(a): # センサー値(0〜100)をY軸の値に変換
return 240 - int(a / 100.0 * 240)
def toH(a): # センサー値をY軸の幅に変換
return int(a / 100.0 * 240)
menu('stop') # ボタンAの上に'stop'を書く
while True:
if buttonA.wasPressed(): # ボタンAが押されたら終わる
break
a = int(angle.read()) # Angleセンサーを読む
lcd.print(a, 0, 0) # Angleセンサー値をLCDに表示
lcd.print(' ')
t = time.time()
if (t - lastT) > 5.0: # 前回から5秒以上経過していたら
r = am.send({'d1': a}) # センサー値をAmbientに送る
r.close()
lastT = t
if abs(lastA - a) < 2: # センサー値がゆらぎの範囲なら描画処理をスキップ
continue
if lastA < a: # 値が大きくなっていたら、白い四角を描画
lcd.rect(120, toY(a), 80, toH(a - lastA) + 1, lcd.WHITE, lcd.WHITE)
else: # 値が小さくなっていたら、黒い四角を描いて消す
lcd.rect(120, toY(lastA) - 1, 80, toH(lastA - a) + 1, lcd.BLACK, lcd.BLACK)
lastA = a
lcd.clear() # LCDを消去
angle.deinit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment