Thursday, March 3, 2022

Reading HEX from SD card and updating the neopixel display

 /*

  the following is modified from SD card file dump


  This sketch shows how to read some data from a file in the SD card using the 

  SD library and update an array of ledMat.


  The circuit:

   SD card attached to SPI bus as follows:

 ** MOSI - pin 11

 ** MISO - pin 12

 ** CLK - pin 13

 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)


  created  22 December 2010

  by Limor Fried

  modified 9 Apr 2012

  by Tom Igoe

  modified 3 Mar 2022

  by Jeszki

  */

#include <SPI.h>

#include <SD.h>

#include <FastLED.h>


#define DATA_PIN    6

#define LED_TYPE    NEOPIXEL

#define COLOR_ORDER GRB

#define NUM_LEDS    128


CRGB ledMat[NUM_LEDS];

     

const int chipSelect = 4;


void setup() {

  // Open serial communications and wait for port to open:


 FastLED.addLeds<WS2812, DATA_PIN, COLOR_ORDER>(ledMat, NUM_LEDS);


  Serial.begin(9600);

  while (!Serial) {

    ; // wait for serial port to connect. Needed for native USB port only

  }

  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:

  if (!SD.begin(chipSelect)) {

    Serial.println("Card failed, or not present");

    // don't do anything more:

    while (1);

  }

  Serial.println("card initialized.");

  // open the file. note that only one file can be open at a time,

  // so you have to close this one before opening another.

  File dataFile = SD.open("test.bin");  //just a text file


  // if the file is available, write to it:

  if (dataFile) {

    int x = 0;

    while (dataFile.available()) {

      char x1 = dataFile.read();

      char x2 = dataFile.read();

      char x3 = dataFile.read();

      Serial.print(x1,HEX);

      Serial.print(x2,HEX);

      Serial.print(x3,HEX);

 if (((x+1)%16)==0) { Serial.println();}     // print 16 char on 1 line

        ledMat[x++]= CRGB(x1,x2,x3);

    }  

    dataFile.close();

  }

  // if the file isn't open, pop up an error:

  else {

    Serial.println("error opening datalog.txt");

  }

   FastLED.show();

   delay(100);

}

void loop() {

}


Wednesday, March 2, 2022

Use union to convert long to bytes

// reference: https://forum.arduino.cc/t/solved-splitting-and-rebuilding-of-long-data-type-numbers/606975/3

//..\Union Example\union_example.ino

// doesn't work on Leonardo but could work on Nano 

//

typedef union u_conv

{

    long    lValue;

    byte    lByte[4];

};


u_conv

    strt,

    finish;

     

void setup() 

{

    Serial.begin(9600);

    strt.lValue = 2147483643;

    Serial.println("Original values:");

    Serial.println(strt.lValue, DEC);

    Serial.println(strt.lValue, BIN);

    //send start.lByte[0..3] to storage medium

    //

    //here, the retrieval is simulated by copying the byte values from the "strt" union

    //to the corresponding byet values in the "finish" union

    //

    //in use, you'd retrieve the bytes from storage and put them in a destination union


    for( int i=0; i<4; i++ )

        finish.lByte[i] = strt.lByte[i];


    Serial.println("Processed values:");

    Serial.println(finish.lValue, DEC);

    Serial.println(finish.lValue, BIN);

}//setup


void loop() 

{

}//loop