M5Stack Core2: シリアルで LED を制御

Raspberry Pi で M5StackCore2 を制御

![IMG_20220924_191713.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/b4704dee-f86a-074c-78f0-bb22c7c8f7d9.jpeg)

プログラム

```text:serial_led.ino
// ---------------------------------------------------------------
/*
	serial_led.ino

						Sep/24/2022
*/
// ---------------------------------------------------------------
#include <M5Core2.h>
#include <Adafruit_NeoPixel.h>
#define PIN 25 
#define NUMPIXELS 9 //LEDの数を指定
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int icount = 0;
// ---------------------------------------------------------------
void setup()
{
	Serial.println("*** setup *** start ***");

	pixels.begin(); //NeoPixelの初期化

	M5.begin();
	M5.Lcd.setTextSize(3);
}

// ---------------------------------------------------------------
void loop() {
	if ((icount % 100) == 0)
	{  
	Serial.println("*** loop *** " + String(icount));
	}

	pixels.clear(); // NeoPixelのリセット
 

	for(int jt=0; jt<200; jt++)
	{
	String aStr = serial_receive_proc();

	if (2 < aStr.length())
		{
		led_proc (aStr);
		}

	delay(100);
	icount++;
	}
}

// --------------------------------------------------------------
void led_proc (String aStr)
{
	M5.Lcd.setCursor(0, 0);
	M5.Lcd.printf("LED Sample %s     \n",aStr);
	M5.Lcd.printf("aStr.length =  %d\n",aStr.length());

	for (int it=0; it<4; it++)
		{
		int jt = it + 5;
		pixels.setPixelColor(it, pixels.Color(0, 0, 0));
		pixels.setPixelColor(jt, pixels.Color(0, 0, 0));    
		}

	if (0 <= aStr.indexOf("red"))
		{
		M5.Lcd.println("LED Red   ");
		M5.Lcd.printf("check: %s     \n",aStr);
		pixels.setPixelColor(5, pixels.Color(150, 0, 0));
		pixels.setPixelColor(6, pixels.Color(150, 0, 0));
		}
	else if (0 <= aStr.indexOf("blue"))
		{
		M5.Lcd.println("LED Blue  ");
		M5.Lcd.printf("check: %s     \n",aStr);
		pixels.setPixelColor(0, pixels.Color(0, 0, 150));
		pixels.setPixelColor(1, pixels.Color(0, 0, 150));
		}	
	else if (0 <= aStr.indexOf("green"))
		{
		M5.Lcd.println("LED Green");
		M5.Lcd.printf("check: %s     \n",aStr);
		pixels.setPixelColor(2, pixels.Color(0, 150, 0));
		pixels.setPixelColor(3, pixels.Color(0, 150, 0));
		}
	else if (0 <= aStr.indexOf("yellow"))
		{
		M5.Lcd.println("LED Yellow");
		M5.Lcd.printf("check: %s     \n",aStr);
		pixels.setPixelColor(7, pixels.Color(150, 150, 0));
		pixels.setPixelColor(8, pixels.Color(150, 150, 0));
		}

	pixels.show();	 //LEDに色を反映
}

// ---------------------------------------------------------------
String serial_receive_proc()
{
	String aStr = "";  
	if (0 <  Serial.available() )
	{
  
	// 終了文字まで取得
	aStr = Serial.readStringUntil('\n');
	Serial.print("Input:");
	Serial.println(aStr);

	M5.Lcd.setCursor(0, 150);
	M5.Lcd.println("Input:");
	M5.Lcd.setCursor(0, 200);
	M5.Lcd.print(aStr);
	M5.Lcd.println("                         ");
	}

	return aStr;
}

// ---------------------------------------------------------------
```

Arduino IDE

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/4099d5cf-b029-ecdd-6a2b-e6ee645d0b3f.png)

テスト用プログラム

```python:serial_test.py
#! /usr/bin/python
#
import serial
import sys
import time

sys.stderr.write("*** start ***\n")
ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=0.1)
#
while True:
	for color in [b"red",b"blue",b"green",b"yellow"]:
		ser.write(color)
		print(color)
		for it in range(30):
			string_data = ser.read_all()
			if (0 < len(string_data)):
				print(string_data)
			time.sleep(0.1)
#
ser.close()
#
```

## cu でテスト

```text
cu -l /dev/ttyUSB0 -s 115200
Connected.
Input:red
Input:green
```

Ubuntu でのソフトのインストール

```bash
sudo apt install cu
```

ユーザーを dialout グループに加える

```bash
sudo gpasswd -a uchida dialout
```

一度、ログアウトして確認

```bash
groups uchida
```

```bash
id
```

実行結果

```text
$ groups uchida
uchida : uchida adm dialout cdrom sudo dip plugdev lpadmin lxd sambashare

$ id
uid=1000(uchida) gid=1000(uchida) groups=1000(uchida),4(adm),20(dialout),24(cdrom),27(sudo),30(dip),46(plugdev),122(lpadmin),134(lxd),135(sambashare)
```