/**
 * Pi Pico PIO driving a 4-digit seven segment display example.
 *
 * Copyright (C) 2021, Uri Shaked
 */
#include "hardware/pll.h"
#include "hardware/clocks.h"
#include "hardware/structs/pll.h"
#include "hardware/structs/clocks.h"
#include "segment.pio.h"
#include "display.h"

uint8_t digits[] = {
  0b11000000, // 0
  0b11111001, // 1
  0b10100100, // 2 
  0b10110000, // 3
  0b10011001, // 4
  0b10010010, // 5
  0b10000010, // 6
  0b11111000, // 7
  0b10000000, // 8
  0b10011000, // 9
};

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

void setup() {
      clock_configure(clk_sys,
                    CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLKSRC_CLK_SYS_AUX,
                    CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
                    48 * MHZ,
                    48 * MHZ);
 
    // Turn off PLL sys for good measure
    pll_deinit(pll_sys);
 
    // CLK peri is clocked from clk_sys so need to change clk_peri's freq
    clock_configure(clk_peri,
                    0,
                    CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLK_SYS,
                    48 * MHZ,
                    48 * MHZ);
 
    // Re init uart now that clk_peri has changed
    //stdio_init_all();
  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);
  uint8_t buff[4];
  memset(buff, 0xff, 4);
  /*buff[0] = decode_char('a');
  pio_sm_put(pio0, 0, ARRAY_TO_U32(buff));
  buff[0] = decode_char('e');
  pio_sm_put(pio0, 0, ARRAY_TO_U32(buff));*/
  for (uint8_t i = 0; i < SIZE_symbol; i++) {
    buff[0] = decode_char(symbol[i]);
    pio_sm_put(pio0, 0, ARRAY_TO_U32(buff));
    delay(800);
  }
  /*uint32_t all = 0xffffff00;
  uint8_t c = decode_char('a');
  all = 0xffffff00 | c;
  //Serial1.println(all, HEX);
  pio_sm_put(pio0, 0, all);
  c = decode_char('b');
  all = 0xffffff00 | c;
  pio_sm_put(pio0, 0, all);
  c = decode_char('c');
  all =  0xffffff00 | c;
  pio_sm_put(pio0, 0, all);*/
}

void displayNumber(uint value) {
  /*uint32_t out = digits[value / 1000 % 10] << 24
  | digits[value % 10]; */
  uint32_t out = digits[value % 10];
  /*|
    digits[value / 100 % 10] << 16 |
    digits[value / 10 % 10] << 8 |
    digits[value % 10];*/
  Serial1.println(out, HEX); 
  pio_sm_put(pio0, 0, out);
  /*pio_sm_put(pio0, 0, 
    digits[value / 1000 % 10] << 24 |
    digits[value / 100 % 10] << 16 |
    digits[value / 10 % 10] << 8 |
    digits[value % 10]
  );*/
}

int i = 0;
void loop() {
  /*if (i < 10) {
    displayNumber(i++);
    delay(500);
  }*/
  delay(1);
}
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT
D0D1D2D3D4D5D6D7GNDLOGIC
D0D1D2D3D4D5D6D7GNDLOGIC