SpresenseでNeoPixelとRTCの干渉に関して調べてみました。

前回[Spresenseで フルカラーLED用の Adafruit NeoPixel ライブラリが使えました](https://qiita.com/kaz19610303/items/a42db788ce87b753943e)という記事を投稿しました。

SpresenseでNeoPixelをコントロールする際にRTCを一緒に使うと表示が乱れる現象が確認されているとの情報を頂きましたので検証してみました。

**「表示が乱れる現象」** が発生する環境がわからなかったので以下の環境で検証しました。

1. ハードウェア
- Spresense Mainボード(CXD5602PWBMAIN1)
- Spresense用NeoPixel用ボード(Nest Step NS-SPR-0350)
- LEDチューブ 50cm 30LED 2本
2. ソフトウェア
- Arduino IDE
- adafruit / Adafruit_NeoPixel.
- KotaMeiwa / nepils  

次の2つのライブラリを使いますので、Arduino iDEにインストールしてください  
[adafruit / Adafruit_NeoPixel](https://github.com/adafruit/Adafruit_NeoPixel)
[KotaMeiwa / nepils](https://github.com/KotaMeiwa/nepils)  

検証に使ったプログラムです
```Spresense_NeoPixel_strandtest_RTC.ino
#include <RTC.h>

#define INTERVAL 50000          // Intereapt Interval 50000μs
#define TIME_HEADER 'T'         // Header tag for serial time sync message

// #include <Adafruit_NeoPixel.h>
#include <Adafruit_NeoPixel_Spresense.h>
#ifdef __AVR__
 #include <avr/power.h>         // Required for 16 MHz Adafruit Trinket
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
// #define LED_PIN    6
#define LED_PIN    18       // LEDチューブ 1
#define LED_PIN_2  19       // LEDチューブ 2

// How many NeoPixels are attached to the Arduino?
// #define LED_COUNT 60
#define LED_COUNT 30

// Declare our NeoPixel strip object:
// Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel_Spresense strip(LED_COUNT, LED_PIN);
Adafruit_NeoPixel_Spresense strip_2(LED_COUNT, LED_PIN_2);

// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
static unsigned long counter = 0;

unsigned int callback_func() {
  RtcTime now = RTC.getTime();

  Serial.print("callback_func called ");
  Serial.print(++counter);
  Serial.println(" times");
  printClock(now);
  return INTERVAL;
}

void printClock(RtcTime &rtc)
{
  printf("%04d/%02d/%02d %02d:%02d:%02d\n",
         rtc.year(), rtc.month(), rtc.day(),
         rtc.hour(), rtc.minute(), rtc.second());
}

// setup() function -- runs once at startup --------------------------------

void setup() {
  // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.
  Serial.begin(115200);
  RTC.begin();

  // Set the temporary RTC time
  RtcTime compiledDateTime(__DATE__, __TIME__);
  RTC.setTime(compiledDateTime);
  attachTimerInterrupt(callback_func, INTERVAL);

  strip.begin();                // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();                 // Turn OFF all pixels ASAP
  strip.setBrightness(50);      // Set BRIGHTNESS to about 1/5 (max = 255)
  strip_2.begin();              // INITIALIZE NeoPixel strip object (REQUIRED)
  strip_2.show();               // Turn OFF all pixels ASAP
  strip_2.setBrightness(50);    // Set BRIGHTNESS to about 1/5 (max = 255)
}


// loop() function -- runs repeatedly as long as board is on ---------------

void loop() {

  // Fill along the length of the strip in various colors...
  colorWipe(strip.Color(255,   0,   0),strip.Color(255,   255,   0), 50);        // Red
  colorWipe(strip.Color(  0, 255,   0),strip.Color(0,   255,   255), 50);        // Green
  colorWipe(strip.Color(  0,   0, 255),strip.Color(255,   0,   255), 50);        // Blue
  // Do a theater marquee effect in various colors...
  theaterChase(strip.Color(127, 127, 127),strip.Color(127, 127, 0), 50);     // White, half brightness
  theaterChase(strip.Color(127,   0,   0),strip.Color(0, 127, 127), 50);     // Red, half brightness
  theaterChase(strip.Color(  0,   0, 127),strip.Color(127, 0, 127), 50);     // Blue, half brightness
  rainbow(10);                                      // Flowing rainbow cycle along the whole strip
  theaterChaseRainbow(50);                          // Rainbow-enhanced theaterChase variant

}


// Some functions of our own for creating animated effects -----------------

// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color,uint32_t color2, int wait) {
  for(int i=0; i<strip.numPixels(); i++) {          // For each pixel in strip...
    strip.setPixelColor(i, color);                  //  Set pixel's color (in RAM)
    strip.show();                                   //  Update strip to match
    strip_2.setPixelColor(i, color2);                //  Set pixel's color (in RAM)
    strip_2.show();                                 //  Update strip to match
    delay(wait);                                    //  Pause for a moment
  }
}

// Theater-marquee-style chasing lights. Pass in a color (32-bit value,
// a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
// between frames.
void theaterChase(uint32_t color,uint32_t color2, int wait) {
  for(int a=0; a<10; a++) {                         // Repeat 10 times...
    for(int b=0; b<3; b++) {                        //  'b' counts from 0 to 2...
      strip.clear();                                //   Set all pixels in RAM to 0 (off)
      strip_2.clear();                              //   Set all pixels in RAM to 0 (off)
      // 'c' counts up from 'b' to end of strip in steps of 3...
      for(int c=b; c<strip.numPixels(); c += 3) {
        strip.setPixelColor(c, color);              // Set pixel 'c' to value 'color'
        strip_2.setPixelColor(c, color2);            // Set pixel 'c' to value 'color'
      }
      strip.show();                                 // Update strip with new contents
      strip_2.show();                               // Update strip with new contents
      delay(wait);                                  // Pause for a moment
    }
  }
}

// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
void rainbow(int wait) {
  for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
    long firstPixelHue_2 = 5*65536 - firstPixelHue;
    strip.rainbow(firstPixelHue);
    strip.show();                                   // Update strip with new contents
    strip_2.rainbow(firstPixelHue_2);
    strip_2.show();                                 // Update strip with new contents
    delay(wait);                                    // Pause for a moment
  }
}

// Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
void theaterChaseRainbow(int wait) {
  int firstPixelHue = 0;                            // First pixel starts at red (hue 0)
  for(int a=0; a<30; a++) {                         // Repeat 30 times...
    for(int b=0; b<3; b++) {                        //  'b' counts from 0 to 2...
      strip.clear();                                //   Set all pixels in RAM to 0 (off)
      strip_2.clear();                              //   Set all pixels in RAM to 0 (off)
      for(int c=b; c<strip.numPixels(); c += 3) {
        int      hue   = firstPixelHue + c * 65536L / strip.numPixels();
        uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
        strip.setPixelColor(c, color);              // Set pixel 'c' to value 'color'
        strip_2.setPixelColor(c, 255 - color);            // Set pixel 'c' to value 'color'
      }
      strip.show();                                 // Update strip with new contents
      strip_2.show();                               // Update strip with new contents
      delay(wait);                                  // Pause for a moment
      firstPixelHue += 65536 / 90;                  // One cycle of color wheel over 90 frames
    }
  }
}

```
Adafruit_NeoPixelで提供されているサンプルプログラム **strandtest** を2系統化しして **割り込み内** でRTCからデータをリードする処理を追加しています。

割り込みの発生はシリアルモニタで確認しています
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/99249/2a51138b-bd18-f52a-3548-d201068ecc5e.png)

こちらが動画になります。
<iframe width="560" height="315" src="https://www.youtube.com/embed/BCmgHa6RBvw" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>

検証結果ですがこの環境とこのプログラムでは目視レベルではありますが、光り方が乱れる現象は確認できませんでした。