#include "segment.pio.h"

// Character mappings for H, E, L, O (assuming active-low logic)
uint8_t chars[5] = {
  0b11101111, // H: Segments A, B, C, E, F, G
  0b00111001, // E: Segments A, D, E, F, G
  0b00110100, // L: Segments D, E, F (L)
  0b00101110, // O: Segments A, B, C, D, E, F
  0b00000000  // Blank: All segments off
};

const uint8_t first_segment_pin = 2;
const uint8_t first_digit_pin = 10;

void setup() {
  Serial1.begin(115200);
  Serial1.println("Raspberry Pi Pico PIO 7-Segment Example");

  // Load the PIO program and initialize the machine
  auto offset = pio_add_program(pio0, &segment_program);
  segment_program_init(pio0, 0, offset, first_segment_pin, first_digit_pin);
}

void displayChars(uint8_t char1, uint8_t char2, uint8_t char3, uint8_t char4) {
  pio_sm_put(pio0, 0, 
    (chars[char1] << 24) | // First character
    (chars[char2] << 16) | // Second character
    (chars[char3] << 8) |  // Third character
    chars[char4]           // Fourth character
  );
}

void loop() {
  const char* message = "HELLO"; // Message to display
  int length = 5; // Length of the message

  // Initial empty state
  displayChars(4, 4, 4, 4); // Display four blank characters
  delay(1000); // Wait for 1 second

  // Loop to create the scrolling effect
  for (int i = 0; i <= length; i++) {
    // Determine which characters to display
    uint8_t char1 = (i < 1) ? 4 : (message[i - 1] - 'H'); // Blank if index < 1
    uint8_t char2 = (i < 2) ? 4 : (message[i - 2] - 'H'); // Blank if index < 2
    uint8_t char3 = (i < 3) ? 4 : (message[i - 3] - 'H'); // Blank if index < 3
    uint8_t char4 = (i < 4) ? 4 : (message[i - 4] - 'H'); // Blank if index < 4
    
    // Display the characters
    displayChars(char1, char2, char3, char4);
    delay(1000); // Adjust the delay for speed of scrolling
  }

  // Clear the display at the end
  displayChars(4, 4, 4, 4); // Display four blank characters
  delay(1000); // Wait for 1 second before restarting
}
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT