6 years, 3 months ago.

How to return two equal string of character

  1. include "mbed.h"
  2. include "TextLCD.h"
  3. include <string.h>

TextLCD lcd(PTC17, PTA16, PTA17, PTE31, PTD6, PTD7, TextLCD::LCD16x2); rs, e, d4-d7, LCDType

char ce[16], cl[] = "Amplificateur 1 ";

int main() {main ce[] = cl[];

while(1) {General lcd.locate(0,0); lcd.printf(" Amps-Select 8 "); lcd.locate(0,1); lcd.printf("%s",cl); }General }main

Hello Antoine,

You can make your code presented here more readable. Just click on the Edit button and enclose it in tags as below:

<<code>>
Your code
<</code>>
posted by Zoltan Hudak 30 Dec 2017

1 Answer

6 years, 3 months ago.

Hello Antoine,

  • When you define a char array initialized with string literal (like in your case cl[]) the compiler silently appends a null charater ('\0') to it. Then such char array can be used as string in functions like printf. So if you would like the ce array to hold the same string as cl and to be used in functions like printf you must account for that and declare it as at least ce[17] or longer.
  • A name of array in C++ represents a const pointer so an assignement like ce[] = cl[]; or ce = cl; is not allowed. Instead you have to copy the elements one by one:

#include "mbed.h"
#include "TextLCD.h"
#include <string.h>

TextLCD lcd(PTC17, PTA16, PTA17, PTE31, PTD6, PTD7, TextLCD::LCD16x2);  //  rs, e, d4-d7, LCDType
char    ce[30], cl[] = "Amplificateur 1 ";

int main(void) {
    //ce[] = cl[];
    int size_of_cl = sizeof(cl) / sizeof(cl[0]);  // calculate the number of elements in cl array
    int i = 0;
    
    for (i = 0; i < size_of_cl; i++)
        ce[i] = cl[i];  // copy an element to element
    
    ce[i] = '\0';  // in case of a char array to be used in printf append the termination null character 

    while (1) {
        lcd.locate(0, 0);
        lcd.printf(" Amps-Select 8 ");
        lcd.locate(0, 1);
        lcd.printf("%s", ce);
    }
}

Accepted Answer

Thank you!!

I have another question about the "gets" I wish to read a string char of tank on the serial port of the KL25Z, but the function(office) " gets " blocks(surrounds) my program, the buckle(loop) stops: here is the code:

#include "mbed.h"
#include "TextLCD.h"


Serial pc(PTC4,PTC3); // TX = violet, TX = Blanc

TextLCD lcd(PTE5, PTE4, PTE3, PTE2, PTB11, PTB10, TextLCD::LCD16x2); // rs, e, d4-d7, LCDType

DigitalIn preset1(PTC11);
DigitalIn preset2(PTC10);

DigitalOut aff1(PTC9);
DigitalOut aff2(PTC8);

char texte[128]; // tableau de 128 caractère


int main() 

{ 
     pc.baud (115200);
    while(1)
        {
            pc.gets(texte,6);
            lcd.locate(0,0);
            lcd.printf("%s",texte);

            if(preset1 == 1){aff1 = 1; aff2 = 0;}// Bouton 1, led 1
            if(preset2 == 1){aff2 = 1; aff1 = 0;}// Bouton 2, led 2

        }
}
posted by Antoine Desmet 03 Jan 2018
  • The signature of gets function in mbed doesn't fully match the standard (see http://www.cplusplus.com/reference/cstdio/gets/. Actually, it's more like the "fgets" function (see http://www.cplusplus.com/reference/cstdio/fgets/) but the FILE* stream parameter is silently passed by mbed.
  • The behavior you observed is correct. The mbed gets function reads characters from stream and stores them in the texte char array until 5 characters have been read or either a newline or the end-of-file is reached, whichever happens first. This means that it’s waiting (at line #24) and blocking your program until you send 5 characters or either a newline or the end-of-file over the pc serial connection to your mbed board.

You can try something as below to avoid blocking and implement simple button debouncing:

#include "mbed.h"
#include "TextLCD.h"

Serial      pc(PTC4, PTC3); // TX = violet, TX = Blanc
TextLCD     lcd(PTE5, PTE4, PTE3, PTE2, PTB11, PTB10, TextLCD::LCD16x2);    // rs, e, d4-d7, LCDType
InterruptIn btn1(PTC11);    // button 1
InterruptIn btn2(PTC10);    // button 2
Timeout     debounceBtn;    // button bouncing Timeout
DigitalOut  aff1(PTC9);
DigitalOut  aff2(PTC8);

char            texte[128]; // tableau de 128 caractère
volatile bool   texteReceived = false; // 'volatile' prevents optimization for global variables used in ISR
volatile bool   btnEnabled = true;
volatile bool   btn1Pressed = false;
volatile bool   btn2Pressed = false;

// ISR (Interrupt Service Routine) handling pc 'serial received' interrupts
void onPcReadable(void) {
    int i = 0;
    
    while (pc.readable() && (i < (128 - 1))) 
        texte[i++] = pc.getc();
    
    texte[i] = '\0';    // terminate the string
    texteReceived = true;
}

// ISR to enable buttons when bouncing is over
void enableBtn(void) {
    btnEnabled = true;
}
 
// ISR handling button1 pressed event
void onBtn1Pressed(void) {
    if (btnEnabled) { // disabled while a button is bouncing
        btnEnabled = false;
        btn1Pressed = true;
        debounceBtn.attach(callback(enableBtn), 0.3);  // debounce time = 0.3s
    }
}

// ISR handling button2 pressed event
void onBtn2Pressed(void) {
    if (btnEnabled) { // disabled while a button is bouncing
        btnEnabled = false;
        btn2Pressed = true;
        debounceBtn.attach(callback(enableBtn), 0.3);  // debounce time = 0.3s
    }
}

int main(void) {
    pc.baud(115200);
    pc.attach(callback(onPcReadable), Serial::RxIrq);   // attach ISR to handle Rx interrupts
    
    btn1.mode(PullUp);
    btn1.fall(callback(onBtn1Pressed)); // attach ISR to handle 'button 1 pressed' interrupts
    
    btn2.mode(PullUp);
    btn2.fall(callback(onBtn2Pressed)); // attach ISR to handle 'button 2 pressed' interrupts

    while (1) {
        if (texteReceived) {
           texteReceived = false;  // reset the flag for next use in ISR
           lcd.locate(0, 0);
           lcd.printf("%s", texte);
        }
        
        if (btn1Pressed) {
            btn1Pressed = false;   // reset the flag for next use in ISR
            aff1 = 1;
            aff2 = 0;
        }   // Bouton 1, led 1
              
        if (btn2Pressed) {
            btn2Pressed = false;   // reset the flag for next use in ISR
            aff2 = 1;
            aff1 = 0;
        }   // Bouton 2, led 2
    }
}
posted by Zoltan Hudak 03 Jan 2018