0

I have Arduino program that use IRsend Library to send IR code, this library need a raw uint16_t array like:

uint16_t rawData[229] = {3114, 2996,  3114, 4374,  598, 486,  598, 1596,  598, 486,  596, 1596,  598, 1596,  598, 486,  596, 486,  596, 1596,  598, 486,  596, 486,  596, 486,  596, 486,  596, 486,  596, 1596,  598, 1596,  600, 486,  600, 484,  596, 486,  598, 486,  598, 486,  598, 486,  596, 486,  598, 486,  598, 1596,  598, 486,  598, 486,  596, 486,  598, 486,  596, 486,  598, 486,  596, 1596,  598, 486,  596, 486,  596, 1594,  600, 1594,  600, 1596,  598, 486,  596, 1596,  598, 488,  596, 1596,  598, 486,  598, 1594,  600, 1596,  598, 486,  598, 486,  596, 486,  596, 486,  596, 486,  596, 486,  598, 1596,  598, 486,  596, 488,  596, 486,  598, 486,  596, 486,  596, 486,  598, 486,  596, 486,  596, 1596,  598, 486,  598, 486,  596, 486,  596, 486,  598, 486,  596, 486,  596, 486,  598, 486,  598, 486,  598, 486,  596, 486,  598, 486,  598, 486,  596, 486,  638, 444,  596, 486,  598, 486,  596, 486,  598, 486,  598, 486,  596, 486,  598, 486,  598, 486,  596, 486,  598, 486,  598, 486,  596, 486,  598, 486,  598, 486,  596, 486,  598, 486,  598, 486,  596, 486,  596, 486,  598, 486,  598, 486,  598, 486,  598, 486,  598, 486,  598, 486,  598, 1596,  598, 486,  598, 1596,  598, 486,  598, 1596,  600, 1596,  598, 486,  598, 1596,  598, 486,  598, 1596,  598, 1596,  598, 486,  596, 486,  596};  // UNKNOWN 80C9BEC5

I stored this uint16_t array to file called (Turbo_swing_middle) inside data folder then upload it to esp32.

I tried the following code to read the file from SPIFFS:

#include <Arduino.h>
#include "SPIFFS.h"
#include <IRremote.hpp>


#define LENGTH 2048
uint16_t rawData[LENGTH];
String st;

void parseCharToUint16WithIrSend(char* textData){
    Serial.printf("Parsing '%s':\n", textData);
    char *end;
    int sizeArray = 0;

    for (unsigned long i = strtoul(textData, &end, 10);
         textData != end;
         i = strtoul(textData, &end, 10))
    {
        textData = end;
        rawData[sizeArray] = i;
        Serial.printf("Value = %d\n", rawData[sizeArray]);
        sizeArray++;
    }
    
    printf("Actual array data size: %d\n", sizeArray);
   // IrSender.sendRaw(rawData,sizeArray,38); // here in rawData we have converted all values from char* to uint16_t
    Serial.println("=> DATA WAS SEND BY IR LED");
    
    sizeArray=0; // set index to 0
    memset(rawData, 0, LENGTH); // clear array
}


void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  IrSender.begin(2);
   if(!SPIFFS.begin(true)){
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }
  
  File myFile = SPIFFS.open("/Turbo_swing_middle","r");
  
  while (myFile.available()) {

      st = myFile.readStringUntil('\n');

  Serial.println("Content of file:");
  //Serial.println(st);
  
  char buf[2048];
  st.toCharArray(buf,2048);
  Serial.println(buf);
  Serial.println("\n\n");

  parseCharToUint16WithIrSend(buf);


  myFile.close();
}


}

void loop() {

}

I Successfully read the file content but the problem how to place that array to uint16_t?

the output of the code is :

Content of file:
3146,2964,3144,4342,630,454,628,1564,630,454,628,1564,630,1564,630,454,628,454,628,1564,630,454,628,454,628,454,628,454,630,454,628,1562,630,1564,630,454,630,454,628,454,630,454,630,454,628,454,628,456,628,456,628,1564,630,454,628,454,628,454,630,454,628,454,630,454,628,1562,630,454,628,454,630,1564,630,1564,630,1564,630,456,628,1564,630,454,630,454,628,454,630,1564,630,1562,630,454,628,454,628,454,628,454,628,454,628,454,628,1564,630,454,628,454,628,454,628,454,628,454,628,456,628,454,628,454,628,1566,628,454,628,454,628,454,628,454,630,456,628,454,628,454,630,454,628,454,628,456,628,454,630,454,628,454,628,454,628,454,628,454,628,454,628,454,628,454,628,454,628,454,630,454,628,456,628,454,628,454,628,454,630,454,628,454,628,454,630,456,628,454,628,456,628,454,630,454,630,454,628,454,630,454,630,454,628,456,628,454,628,454,628,454,628,454,628,454,628,1564,630,1564,630,456,628,454,628,1564,630,454,630,1562,632,1564,630,1564,630



Parsing '3146,2964,3144,4342,630,454,628,1564,630,454,628,1564,630,1564,630,454,628,454,628,1564,630,454,628,454,628,454,628,454,630,454,628,1562,630,1564,630,454,630,454,628,454,630,454,630,454,628,454,628,456,628,456,628,1564,630,454,628,454,628,454,630,454,628,454,630,454,628,1562,630,454,628,454,630,1564,630,1564,630,1564,630,456,628,1564,630,454,630,454,628,454,630,1564,630,1562,630,454,628,454,628,454,628,454,628,454,628,454,628,1564,630,454,628,454,628,454,628,454,628,454,628,456,628,454,628,454,628,1566,628,454,628,454,628,454,628,454,630,456,628,454,628,454,630,454,628,454,628,456,628,454,630,454,628,454,628,454,628,454,628,454,628,454,628,454,628,454,628,454,628,454,630,454,628,456,628,454,628,454,628,454,630,454,628,454,628,454,630,456,628,454,628,456,628,454,630,454,630,454,628,454,630,454,630,454,628,456,628,454,628,454,628,454,628,454,628,454,628,1564,630,1564,630,456,628,454,628,1564,630,454,630,1562,632,1564,630,1564,630':
Value = 3146
Actual array data size: 1
=> DATA WAS SEND BY IR LED

I used parseCharToUint16WithIrSend() function from this thread , but it read only one value and not the whole file contents.

What i want is to read the raw data from the SPIFFS file then place the content inside uint16_t array so i can use it in IRsender like this:

uint16_t rawData[229] = {3114, 2996,  3114, 4374,  598, 486,  598, 1596,  598, 486,  596, 1596,  598, 1596,  598, 486,  596, 486,  596, 1596,  598, 486,  596, 486,  596, 486,  596, 486,  596, 486,  596, 1596,  598, 1596,  600, 486,  600, 484,  596, 486,  598, 486,  598, 486,  598, 486,  596, 486,  598, 486,  598, 1596,  598, 486,  598, 486,  596, 486,  598, 486,  596, 486,  598, 486,  596, 1596,  598, 486,  596, 486,  596, 1594,  600, 1594,  600, 1596,  598, 486,  596, 1596,  598, 488,  596, 1596,  598, 486,  598, 1594,  600, 1596,  598, 486,  598, 486,  596, 486,  596, 486,  596, 486,  596, 486,  598, 1596,  598, 486,  596, 488,  596, 486,  598, 486,  596, 486,  596, 486,  598, 486,  596, 486,  596, 1596,  598, 486,  598, 486,  596, 486,  596, 486,  598, 486,  596, 486,  596, 486,  598, 486,  598, 486,  598, 486,  596, 486,  598, 486,  598, 486,  596, 486,  638, 444,  596, 486,  598, 486,  596, 486,  598, 486,  598, 486,  596, 486,  598, 486,  598, 486,  596, 486,  598, 486,  598, 486,  596, 486,  598, 486,  598, 486,  596, 486,  598, 486,  598, 486,  596, 486,  596, 486,  598, 486,  598, 486,  598, 486,  598, 486,  598, 486,  598, 486,  598, 1596,  598, 486,  598, 1596,  598, 486,  598, 1596,  600, 1596,  598, 486,  598, 1596,  598, 486,  598, 1596,  598, 1596,  598, 486,  596, 486,  596};  // UNKNOWN 80C9BEC5

IrSender.sendRaw(rawData, 229, 38);  // Send a raw data capture at 38kHz.

array size is fixed at 229, but i prefer dynamic size if possible!

  • 1
    you can't feed a comma-separated list of numbers to `strtol`, you'll need a method to isolate each number first. For "pure C" you would use [`strtok`](https://man7.org/linux/man-pages/man3/strtok_r.3.html), not sure if the Arduino world has some other preferred method. – yano Aug 21 '22 at 21:26
  • Minor: `%u` makes more sense in `Serial.printf("Value = %d\n", rawData[sizeArray]);` if code later ported to 16-bit `int/unsigned` and still needs to work on 32-bit. – chux - Reinstate Monica Aug 22 '22 at 04:01
  • looks interested, any way the above function worked fine when i removed the `,` and replaced it by spaces – hisham2630 Aug 22 '22 at 20:53

1 Answers1

0

Did you try to read the string until the delimeter like so?

void readArray(){
    // example read : '3146,2964,3144,4342,630,454,628,1564,630,454'
    File myFile = LittleFS.open("/Turbo_swing_middle","r");
    if( !myFile ){ return; }

    uint16_t rawData[2048];
    String readNum;
    for(int i = 0; i < 2048; i++){
        if( myFile.available() ){
            readNum = myFile.readStringUntil(',');
            rawData[i] = readNum.toInt();
        }
    }

    // print raw array:
    for(int i = 0; i < 2048; i++){
        Serial.printf("%d,", rawData[i]);
    }
    Serial.println();
}

This function is untested, but it should read the file until it finds a comma character, puts everything it found to a String buffer and make that buffer to int into rawData array. It should work.

EDIT: I used LittleFs instead of SPIFFS and you should too because it is faster and reliable.

Dr.Random
  • 430
  • 3
  • 16