AngelNicolas
AngelNicolas

Reputation: 1

char pointer in freertos esp32

Good evening, I'm having a small problem with the argument that happened to the thread and I would like to know how to solve it since I have searched all the internet forums and I can't find an answer, the code must print the variable in an array of de leds, but a problem is generated and it is that the argument when I try to read it as a char pointer gives a value of zero, but when I print the value as such of the char if it has the corresponding value, I would appreciate a small solution for this problem.

//Librerias
#include <Arduino.h>
#include "Wire.h"
#include "RTClib.h"
#include <MD_MAX72xx.h>
#include <SPI.h>
//Blinking rate in milliseconds
const portTickType TIMESTOP=1;
const portTickType TIMETAX=1;
const portTickType TIMERTC=1;
const portTickType TIMEREFREHS=1;
//Defines
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define CHAR_SPACING 1
//Pines
#define CLK_PIN   18
#define DATA_PIN  23
#define CS_PIN    5
MD_MAX72XX mx=MD_MAX72XX(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
RTC_DS1307 DS1307_RTC;
//Variables externas
char mess[4];
char Week_days[7][12]={"Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado"};
char numbers[10]={48, 49, 50, 51, 52, 53, 54, 55, 56, 57};
bool flagDorN;
bool flagTorD;
bool banner;
int time_24s;
int distance=0;
int unitsNT=28;
int priceNT=2912;
int unitsT=52;
int priceT=5408;
int timeSeconds=0;
int cent;
int dec;
int unit;
int DMALHMS[7];
String dayT;
String hourT;
void setup(){
  Serial.begin(9600);
  mx.begin();
  if(!DS1307_RTC.begin()){
    while(1);
  }
  DS1307_RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
  //Variables internas
  banner=1;
  flagTorD=0;
  //Hilos
  xTaskCreate(Time24s, "Time24s", 1024, NULL, 1, NULL);
  xTaskCreate(Taximeter, "Taximeter", 1024, NULL, 1, NULL);
  xTaskCreate(Fun_DMALHMS, "Fun_DMALHMS", 2048, NULL, 1, NULL);
  xTaskCreate(printTextUnitsT, "printTextUnitsT", 2048, (void*)&mess, 1, NULL);
}
void loop(){
  delay(1);
}
void Time24s(void* args){
  while(true){
    if(flagTorD==0){
      time_24s=time_24s+1;
    }
    vTaskDelay(TIMESTOP);
  }
}
void Taximeter(void* args){
  while(true){
    if(banner==1){
      if(DMALHMS[3]==0 || 20<=DMALHMS[4] || DMALHMS[4]<=5){
        cent=unitsT/100;
        dec=(unitsT-(cent*100))/10;
        unit=(unitsT-(cent*100+dec*10));
        for(int i=0; i<=9; i++){
          if(i==cent){
            mess[0]=char(numbers[i]);
          }
          if(i==dec){
            mess[1]=char(numbers[i]);
          }
          if(i==unit){
            mess[2]=char(numbers[i]);
          }
          mess[3]='\0';
        }
      }
    }
    if(time_24s==24000){
      time_24s=0;
      Serial.println(dayT);
      Serial.println(hourT);
    }
    vTaskDelay(TIMETAX);
  }
}
void Fun_DMALHMS(void* args){
  while(true){
    DateTime now=DS1307_RTC.now();
    DMALHMS[0]=now.day();
    DMALHMS[1]=now.month();
    DMALHMS[2]=now.year();
    DMALHMS[3]=now.dayOfTheWeek();
    DMALHMS[4]=now.hour();
    DMALHMS[5]=now.minute();
    DMALHMS[6]=now.second();
    dayT=String(DMALHMS[0])+"/"+String(DMALHMS[1])+"/"+String(DMALHMS[2]);
    hourT=String(DMALHMS[4])+":"+String(DMALHMS[5])+":"+String(DMALHMS[6]);
    vTaskDelay(TIMERTC);
  }
}
void printTextUnitsT(void *args){
  while(true){
    char *vars=(char*)args;
    Serial.print("(char*)args ");
    Serial.println((char*)args);
    Serial.print("*(char*)args ");
    Serial.println(*(char*)args);
    uint8_t state=0;
    uint8_t curLen;
    uint16_t showLen;
    uint8_t cBuf[8];
    int16_t col=(((uint8_t(MAX_DEVICES-1) + 1) * COL_SIZE)-1)-7;
    mx.control(uint8_t(0), uint8_t(MAX_DEVICES-1), MD_MAX72XX::UPDATE, MD_MAX72XX::OFF);
    do{
      switch(state){
        case 0:
          if((*vars)=='\0'){
            showLen=col-(uint8_t(MAX_DEVICES-1)*COL_SIZE);
            state=2;
            break;
          }
          showLen=mx.getChar((*vars), sizeof(cBuf)/sizeof(cBuf[0]), cBuf);
          curLen=0;
          state++;
        case 1:
          mx.setColumn(col--, cBuf[curLen++]);
          if (curLen==showLen){
            showLen=CHAR_SPACING;
            state=2;
          }
          break;
        case 2:
          curLen=0;
          state++;
        case 3:
          mx.setColumn(col--, 0);
          curLen++;
          if(curLen==showLen)
            state=0;
          break;
        default:
          col=-1;
      }
    }while(col>=(uint8_t(0)*COL_SIZE));
    mx.control(uint8_t(0), uint8_t(MAX_DEVICES-1), MD_MAX72XX::UPDATE, MD_MAX72XX::ON);
    vTaskDelay(TIMEREFREHS);
  }
}

enter image description here

Try a way of converting a void* to a char*

char vars=malloc(sizeof(char *args))

Upvotes: 0

Views: 158

Answers (2)

AngelNicolas
AngelNicolas

Reputation: 1

Since I'm new to this forum and it's the first time I've asked something that has to do with programming, I don't know how to tag you, but thank you very much for answering my question @tarmo, I tried to follow your indications that helped me understand what my question was. problem, but now I get an error with CORE 1, I don't know if you agree with me telling the thread which core to use, but in this case you would know which is the correct core for the ESP32. I await your prompt response and I reiterate thank you very much to all (for the corrections or for trying to give me a solution). And for the implementation of the code I modified it like this, according to what I understood so that it reads each character that is inside mess.

void printTextUnitsT(void *args){
while(true){
    uint8_t state=0;
    uint8_t curLen;
    uint16_t showLen;
    uint8_t cBuf[8];
    int16_t col=(((uint8_t(MAX_DEVICES-1) + 1) * COL_SIZE)-1);
    mx.control(uint8_t(0), uint8_t(MAX_DEVICES-1), MD_MAX72XX::UPDATE, MD_MAX72XX::OFF);
    do{
      switch(state){
        case 0:
          if((**(char**)args)=='\0'){
            showLen=col-(uint8_t(MAX_DEVICES-1)*COL_SIZE);
            state=2;
            break;
          }
          showLen=mx.getChar((**(char**)args), sizeof(cBuf)/sizeof(cBuf[0]), cBuf);
          curLen=0;
          state++;
        case 1:
          mx.setColumn(col--, cBuf[curLen++]);
          if (curLen==showLen){
            showLen=CHAR_SPACING;
            state=2;
          }
          break;
        case 2:
          curLen=0;
          state++;
        case 3:
          mx.setColumn(col--, 0);
          curLen++;
          if(curLen==showLen)
            state=0;
          break;
        default:
          col=-1;
      }
    }while(col>=(uint8_t(0)*COL_SIZE));
    mx.control(uint8_t(0), uint8_t(MAX_DEVICES-1), MD_MAX72XX::UPDATE, MD_MAX72XX::ON);
    vTaskDelay(TIMEREFREHS);
  }
}

enter image description here

Upvotes: 0

Tarmo
Tarmo

Reputation: 4762

You define a C array of 4 chars, but note that the variable mess is a pointer to the first character in that array.

char mess[4];

Later you're taking the address of that pointer with &mess and passing it into task printTextUnitsT:

xTaskCreate(printTextUnitsT, "printTextUnitsT", 2048, (void*)&mess, 1, NULL);

So task printTextUnitsT receives a pointer to a pointer to the first character. To see the value of the first character in that array you have to de-reference it twice, once to get the original pointer, then another time to see the value of that first character.

if (**(char**)args == '\0') {
  // do something
}

Upvotes: 0

Related Questions