Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with printf and strings
07-11-2015, 09:57 PM,
#1
Problem with printf and strings
Hi,
I am facing a problem with the printf function on my Pinguino2550.
I would like to create a context menu to be shown on an LCD 16x2.
I created and array containing the text to be displayed but when I recall it on the printf function, it will print bad characters on LCD.
Here is my code:

Code:
#define BTN_OK    4
#define BTN_DN    5
#define BTN_UP    6
#define BTN_BK    7
#define LED_01    8
#define LED_02    9
#define LED_03    10
#define LED_04    11

int led_handle = 0;
int led_handle1 = 0;
int led_handle2 = 0;
u8 incG = 0;
u8 btn_status_current = 0;
u8 btn_status = 0;
u8 flag_btn_changed = 1;

char menu[3] = {'Main', 'Settings', 'About'};

// Functions

void TMR3_Tick() {
   led_handle = (led_handle ^ 1);
   digitalWrite(USERLED, led_handle);
}

void btnPressed() {
   delay(2);
   btn_status_current  = (digitalRead(BTN_BK)*8) + (digitalRead(BTN_UP)*4) + (digitalRead(BTN_DN)*2) + (digitalRead(BTN_OK));  
   if (btn_status != btn_status_current) {
       btn_status = (btn_status_current & 0xF);        
       switch (btn_status) {
           // Pin7 pressed
           case 0b00001000:            
               incG = 0;
           break;
           // Pin6 pressed
           case 0b00000100:
               incG +=1;    
           break;
           // Pin5 pressed
           case 0b00000010:
               incG -=1;
           break;
           // Pin4 pressed
           case 0b00000001:
           break;
           // None of above        
           default:
           break;
        }
       flag_btn_changed = 1;
   }    
}

void setup() {
   pinMode(BTN_BK, INPUT);
   pinMode(BTN_UP, INPUT);
   pinMode(BTN_DN, INPUT);
   pinMode(BTN_OK, INPUT);
   pinMode(LED_01, OUTPUT);
   pinMode(LED_02, OUTPUT);
   pinMode(LED_03, OUTPUT);
   pinMode(LED_04, OUTPUT);
   pinMode(USERLED, OUTPUT);
   
   digitalWrite(USERLED, HIGH);
   digitalWrite(LED_01, LOW);
   digitalWrite(LED_02, LOW);
   digitalWrite(LED_03, LOW);
   digitalWrite(LED_04, LOW);

   OnChangePin4to7(btnPressed, BTN_BK, 0);
   OnTimer3(TMR3_Tick, INT_MILLISEC, 500);    
   //btn_status = (digitalRead(BTN_BK)*8) + (digitalRead(BTN_UP)*4) + (digitalRead(BTN_DN)*2) + (digitalRead(BTN_OK));
   
   I2C.master(I2C_100KHZ);    
   lcdi2c.init(16, 2, 0x20);           // display is 2x16, ic2 address is 0100001 (see above)
   lcdi2c.backlight();
   lcdi2c.clear();                     // clear screen
   lcdi2c.home();                      // set cursor at (0,0)
}

void loop() {
   if (flag_btn_changed == 1) {
   lcdi2c.clear();                     // clear screen
   lcdi2c.setCursor(0, 0);
   //lcdi2c.printf("%s", "Settings");         //This WORKS
   lcdi2c.printf("%s", menu[incG]);         // This NOT WORK
   lcdi2c.setCursor(0, 1);
   lcdi2c.printf("%04b  %d", btn_status, incG);
   flag_btn_changed = 0;  
   }
}


If I pass to the %s a real string it will work, but though and array it will not.
Where I mistake?
Help please!!
Thanks Luca
Reply
07-11-2015, 11:14 PM,
#2
RE: Problem with printf and strings
Hello Brikker,
Your definition for char menu[3] is not correct because such as written it is a single array of 3 characters and not an array of 3 strings.
Here is an example which works with GCC compiler and prints Settings :

#include <stdio.h> // necessary for use of printf

void main(void) {
const char *menu[3] ={"Main","Settings","About"}; // array of 3 pointers on strings
int myselection;
...
myselection=1; 
printf("%s",menu[myselection]);}
Adapt it to your need and Pinguino syntax.
Bye
André
Reply
07-11-2015, 11:51 PM,
#3
RE: Problem with printf and strings
Thanks Andrè!
It work great now, I fell on string array declaration!
Thanks again!
Luca
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)