こんにちは!

今回はどの様な話題で始めようか。。。
そういえば、私がアメブロで使っているデザインは、Dodger Blue(正確に言うと異なる※)です。右差し 大谷選手の移籍前からです。(笑)

特に意識しているつもりはございませんが、Blueという色のこと嫌いじゃないです。

※ dodgerblue
  #1e90ff
  Red:30 Gleen:144 Blue:255


上差しPython IDEのTurtle Demo/Examples/colormixerで似せてみました。
 

さてと最近手に入れたアイラ(アイラ島で醸造されるウイスキーの略称)のシングルモルトですが、

スキャラバスといいます。ハンターレイン社が提供するウイスキーなのですが、蒸留所が不詳なんです。

案外、ハンターレイン社がオーナーであるアード・ナッホー蒸留所だったりしてね。 右差し だったら嬉しいです。

とっても薄い琥珀色(琥珀色ではないってことか)ですが、今までに試した何れのアイラ(Islay)とも異なります。
 

それから自分へのクリスマスプレゼントとして、アードベッグのウーガダールをポチってしまいました。。。(笑)
ワクワクニコニコ

 

前置きはこの程度にして本題に入ります。

ROCK 5BのI2C I/F(インタフェース)をアクティベートする方法の紹介です。

からの続きになります。

ROCK 5Bには、UbuntuサーバーとPython3、Thonnyが既にインストールされているものとします。


PC上のX terminalからROCK 5Bにログイン後、

~$ sudo i2cdetect -l
とします。

以下は、コンソールに表示される。
i2c-0   i2c             rk3x-i2c                                I2C adapter
i2c-1   i2c             rk3x-i2c                                I2C adapter
i2c-4   i2c             rk3x-i2c                                I2C adapter
i2c-6   i2c             rk3x-i2c                                I2C adapter
i2c-7   i2c             rk3x-i2c                                I2C adapter
i2c-9   i2c             fde50000.dp                         I2C adapter
i2c-10  i2c            ddc                                      I2C adapter
i2c-11  i2c            ddc   

I2Cのデバイス属性を変更します。
~$ sudo chmod a+rw /dev/i2c-*

ROCK 5BとI2CのI/F結線は、
===
GND=Pin9,
VCC=Pin1 (3.3V) or Pin3 (5V)
SDA=Pin27 (I2C0_SDA_M1)
SCL=Pin28 (I2C0_SCL_M1)
===
です。

【Before】
~$ sudo i2cdetect -y 0 右差し 0は、 i2c-0のことです。
とします。
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- UU UU -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
結果は↑なので、
Open the ubuntuEnv.txt file: Usually in the /boot/firmware/ directory. 
You can use a text editor like nano to open the file with root permission.
You will see the bottom line says overlays=
Add the device tree overlay to the previously mentioned line as follows: overlays=rk3588-i2c0-m1
Save the file and reboot your system, I2C should be enabled.

~$ sudo nano /boot/firmware/ubuntuEnv.txt
として、ubuntuEnv.txtに追記します。

bootargs=root=UUID=078cb313-24f7-482e-8551-96bedc4124d3 rootfstype=ext4 rootwait rw console=ttyS2,1500000 console=tty1 cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory swapaccount=1 systemd.unified_cgroup_hierarchy=0 

fdtfile=rk3588-rock-5b.dtb
overlay_prefix=rock-5b
overlays=rk3588-i2c0-m1 右差し この行を追記します。

編集を終了してから
~$ sudo reboot
します。

因みにROCK 5BでI2Cで使用するGPIOをRapberry Piのと互換にするのは、
SDA=Pin3 (I2C7_SDA_M3)
SCL=Pin5 (I2C7_SCL_M3)

の様です。

【After】
~$ sudo i2cdetect -y 0
とすると、以下の様にSSD1306とBME280が認識されます。 グッ
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- 76 --

OLEDモジュール(SDD1306) → アドレス:0x3c
SSD1306用のドライバーをインストールします。


~$ sudo apt-get -y install python3-luma.oled

その後Thonnyからサンプルスクリプト:
#coding: utf-8
#made by jk1brk
import time

############# SSD1306 #############
from luma.core.interface.serial import i2c
from luma.oled.device import ssd1306
from luma.core.render import canvas

#/dev/i2c-0 for Rock 5B
serial = i2c(port = 0, address = 0x3C)
device = ssd1306(serial)

def hello_world(x,y,fill):
    with canvas(device) as draw:
        draw.text((x, y), "hello world!", fill)

################################
if __name__ == '__main__':
    try:
        while True:
            hello_world(8,0,"white")
            time.sleep(1)
            hello_world(8,0,"black")
            time.sleep(1)
           
    except KeyboardInterrupt:
        pass
を実行すると、SSD1306で"hello world!"が点滅を繰り返します。

次に
BME280 → アドレス:0x76
ですが、
~$ sudo pip install smbus2
をインストールしてから、
Thonnyを起動して以下のスクリプトで動作チェックを行います。
https://github.com/SWITCHSCIENCE/samplecodes/blob/master/BME280/Python27/bme280_sample.py
BME280から読みだされた温度、湿度、気圧のデータがThonnyのコンソール画面に表示されます。

多くを語りませんが、
Thonnyのコンソール画面に表示される文字をSSD1306に表示すれば、Weather Stationの完成です。

def weather_stats(temp, hum, pres):
    with canvas(device) as draw:
        draw.rectangle((0,0,127,62), outline="white", fill="black")
        # Temperature
        draw.text((8, 0), temp, fill="white")
        # Humidity
        draw.text((8, 22), hum, fill="white")
        # Pressure
        draw.text((8, 44), pres, fill="white")

TBC グッ