Skip to content

Instantly share code, notes, and snippets.

@iktakahiro
Last active October 9, 2015 05:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iktakahiro/5d719a9090db6bff322a to your computer and use it in GitHub Desktop.
Save iktakahiro/5d719a9090db6bff322a to your computer and use it in GitHub Desktop.

mraa

LED (Lチカ)

>>> import mraa
>>> led = mraa.Gpio(6) # 初期化
>>> led.dir(mraa.DIR_OUT) # 出力対象としてセット
0
>>> led.getPin() # ピン番号を取得
6
>>> led.write(1) # 点灯
0
>>> led.write(0) # 消灯
0
>>> led.write(2) # 1以上の数値でも 点灯
0

デジタルスイッチのON/OFF

import mraa
import time

class Counter(object):
    count = 0

c = Counter()
def check_switch(args):

    if c.count % 2 == 0:
        print('ON')
    else:
        print('OFF')

    c.count += 1

x = mraa.Gpio(2)
x.dir(mraa.DIR_IN)
x.isr(mraa.EDGE_BOTH, check_switch, 1)

time.sleep(500)

アナログつまみの値取得

>>> import mraa
>>> angle = mraa.Aio(0)
>>> angle.read()
47

>>> angle.read()
668

温度取得

>>> import math
>>> import mraa
>>> from decimal import Decimal, ROUND_UP, get_context
>>> get_context().prec = 8
>>> sensor = mraa.Aio(1)
>>> value = sensor.read()
>>> value
433

>>> resistance = (1023 - value) * 1000 / Decimal(value)
>>> resistance
Decimal('1362.5866')

>>> fahrenheit = 1 / (Decimal(math.log(resistance / 10000)) / 3975 + 1 / Decimal(298.15)) - Decimal(273.15)
>>> fahrenheit
Decimal('77.409550')

>>> celsius = (Decimal(5) / Decimal(9)) * (fahrenheit - Decimal(32))
>>> celsius
Decimal('25.227528')

明度センサー

>>> import mraa
>>> light = mraa.Aio(1)
>>> light.read()
675

UPM

LCD

>>> import pyupm_i2clcd as lcd
>>> my_lcd = lcd.Jhd1313m1(6, 0x3E, 0x62)  # 初期化
>>> my_lcd.setColor(0, 255, 0) # 緑
>>> my_lcd.setColor(0, 255, 255) # 水色 
>>> my_lcd.setColor(0, 0, 255) # 青

>>> my_lcd.write("Hello")
>>> my_lcd.write("World")
>>> my_lcd.clear() # 表示内容を消去

>>> my_lcd.setCursor(0, 0)
>>> my_lcd.write("PyCon JP")
>>> my_lcd.write(" 2015")
>>> my_lcd.setCursor(1, 0) # 2行目, 1文字目
>>> my_lcd.write("I'm Happy!")
>>> my_lcd.clear() # 表示内容を消去

>>> my_lcd.write("Hello World")
>>> my_lcd.scroll(True) # 実行するたびに左にスクロール
>>> my_lcd.home() # 位置を戻す

>>> my_lcd.scrollDisplayLeft() # 左にスクロール
>>> my_lcd.scrollDisplayRight() # 右にスクロール

>>> my_lcd.cursorOn() # カーソル表示をON
>>> my_lcd.cursorOff() # カーソル表示をON
>>> my_lcd.cursorBlinkOn() # 点滅カーソルをON
>>> my_lcd.cursorBlinkOff() # 点滅カーソルをOFF

>>> my_lcd.displayOff() # 表示をOFF
>>> my_lcd.displayOn() # 表示をON
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment