LoginSignup
11
8

More than 3 years have passed since last update.

M5StackのLCDディスプレイの色をRGBで指定する。

Last updated at Posted at 2018-07-03

目的

M5StackのLCDディスプレイでは、16Bit Colorで色を指定するようになっている。
直感的にわかりやすい、RGBの24Bit color(8Bit color x 3channel)で、色を指定する。

従来

M5Stackで、LCDディスプレイに赤を表示するサンプルコードは
例えば以下のようになる。

#include <M5Stack.h>

void setup() {
   M5.begin();
   M5.Lcd.fillScreen(TFT_RED);
   //M5.Lcd.fillScreen(0xF800);
}

void loop(void) {
  delay(100);
}

色を指定する場合は、LCDのディスプレイであらかじめ定義されている変数を使うか、
16Bit Colorを直接入力する必要がある。

                                     /*   R,   G,   B */ 
 #define TFT_BLACK       0x0000      /*   0,   0,   0 */ 
 #define TFT_BLUE        0x001F      /*   0,   0, 255 */ 
 #define TFT_GREEN       0x07E0      /*   0, 255,   0 */ 
 #define TFT_RED         0xF800      /* 255,   0,   0 */ 
 #define TFT_WHITE       0xFFFF      /* 255, 255, 255 */ 

実装1

16Bit Colorと24Bit Colorとは、以下の式の関係がある。

uint16_t color = ((red>>3)<<11) | ((green>>2)<<5) | (blue>>3);

この式を組み入れた関数を用意すれば

直感的にわかりやすい、24Bit color(8Bit color x 3channel)で、色を指定できる。~~

#include <M5Stack.h>

uint16_t getColor(uint8_t red, uint8_t green, uint8_t blue){
  return ((red>>3)<<11) | ((green>>2)<<5) | (blue>>3);
}

void setup() {
   M5.begin();
   M5.Lcd.fillScreen(getColor(255,0,0));
   //M5.Lcd.fillScreen(0xF800);
   //M5.Lcd.fillScreen(TFT_RED);
}

void loop(void) {
  delay(100);
}

参考

C++ defined 16bit (high) color
https://stackoverflow.com/questions/13720937/c-defined-16bit-high-color

実装2

M5.Lcd.color565()関数を使う

構文:

color565(uint8_t red, uint8_t green, uint8_t blue);
 関数で使用する色コード(rgb565)に変更します。
引数:
    red 赤 int8_t
    green 緑 int8_t
    blue 青 int8_t

使用例:

#include <M5Stack.h>
    uint16_t colorvalue=0;
    colorvalue= M5.Lcd.color565(255,0,0);

11
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
8