Tuesday, December 20, 2022

Adafruit Accessories Quad Level-Shifter and NeoPixel Ring

Today I'll be using a 74AHCT125N  quad level-shifter to use a 5V 24 neoPixel ring with my Arduino Nano Connect. It is on pin 4.

That part was pretty easy. Wiring followed the adafruit reference. The only problem was sending the programmable input/output (PIO) state machine commands from one of the cores. This would cause animations to stall while running CPU-heavy instructions. I could run it on core0 since it will just be handling BLE communications. I would like it to be able to run the lights in some preset animations. Ideally independently while taking some FIFO interrupts from the main core. PIOs are capable of looping small snippets. I've only used PIOs once before using a Pi Pico and micropython.
Time for a dive into some assembly language with pioasm. The pico c sdk datasheet has great info on this at page 33.

Here is what I have so far:
#include <NeoPixelConnect.h>
#define MAXIMUM_NUM_NEOPIXELS 24

// Create an instance of NeoPixelConnect and initialize it
// to use GPIO pin 4 (D12) as the control pin, for a string
// of 8 neopixels. Name the instance p
NeoPixelConnect p(4, MAXIMUM_NUM_NEOPIXELS, pio0, 0);

// this array will hold a pixel number and the rgb values
uint8_t random_pixel_setting[4];
uint8_t waiting_pixel_setting[4];

int adjDelay = 0;
uint16_t runner = 0;
uint16_t leadPixel = 0;
void waiting(){
  for( int i = MAXIMUM_NUM_NEOPIXELS-1; i >= 0; i--){//leadPixel update
    //leadPixel = i;
    for(int j = 0; j < MAXIMUM_NUM_NEOPIXELS; j++){//pixel update
      //Lead pixel will be i + j
      uint8_t pixel = i + j;
      while(pixel >= MAXIMUM_NUM_NEOPIXELS){
        pixel -= MAXIMUM_NUM_NEOPIXELS;
      }
      waiting_pixel_setting[0] = pixel;
      // Serial.print("Pixel:");
      // Serial.println(pixel);
     
      waiting_pixel_setting[3] = random( 255 - ((255 / MAXIMUM_NUM_NEOPIXELS) * j) - (255 / MAXIMUM_NUM_NEOPIXELS) ,255 - ((255 / MAXIMUM_NUM_NEOPIXELS) * j));
      // Serial.print("B:");
      // Serial.println(waiting_pixel_setting[3]);
     
      waiting_pixel_setting[1] = waiting_pixel_setting[3] / random(3, 5);
      // Serial.print("R:");
      // Serial.println(waiting_pixel_setting[1]);
     
      waiting_pixel_setting[2] = waiting_pixel_setting[3] / random(5, 10);
      // Serial.print("G:");
      // Serial.println(waiting_pixel_setting[2]);
     
      p.neoPixelSetValue(pixel, waiting_pixel_setting[1], waiting_pixel_setting[2], waiting_pixel_setting[3], false);
    }
    p.neoPixelShow();
    delay(adjDelay++);
    if(adjDelay == 120){
      adjDelay = 0;
      Serial.println("Reset!");
    }
  }
  Serial.println(urgb_u32(waiting_pixel_setting[1], waiting_pixel_setting[2], waiting_pixel_setting[3]) << 8u);
}
uint32_t urgb_u32(uint8_t r, uint8_t g, uint8_t b) {
    return
            ((uint32_t)(r) << 8) |
            ((uint32_t)(g) << 16) |
            (uint32_t)(b);
}
// select a random pixel number in the string
uint8_t get_pixel_number(){
  if(runner == MAXIMUM_NUM_NEOPIXELS){
    runner = 0;
  }
  return (uint16_t)runner++;
}

// select a random intensity
uint8_t get_pixel_intensity(){
    return((uint8_t)random(25,100));
}

void get_random_pixel_and_color(){
    random_pixel_setting[0] = get_pixel_number();
    random_pixel_setting[1] = get_pixel_intensity();
    random_pixel_setting[2] = get_pixel_intensity();
    random_pixel_setting[3] = get_pixel_intensity();
}

void setup(){
    Serial.begin(115200);
    // while(!Serial){//Wait for serial to come up
    // sleep_ms(100);  
    // }
    Serial.println("In setup");
    //waiting();
}

void loop(){

  waiting();
  //delay(1000);
}



References:

https://datasheets.raspberrypi.com/pico/raspberry-pi-pico-c-sdk.pdf

https://raspberrypi.github.io/pico-sdk-doxygen/group__hardware__pio.html

https://learn.adafruit.com/neopixels-on-raspberry-pi/raspberry-pi-wiring

https://github.com/MrYsLab/NeoPixelConnect

https://forum.arduino.cc/t/programming-pio-with-pico/704290

https://stackoverflow.com/questions/795286/what-does-the-question-mark-character-mean-in-c

No comments:

Post a Comment