PrintF problems!

26 May 2013

This is my following code, the problem I'm facing is that with the code I'm able to get that which protocol the remote im using has like NEC or SONY or AEHA but not able to get the code of the actual value that is received by the mBed on press of a button (that is in the buf variable) kindly tell me where could be the possible problem?

ON press of a button I get "got>>SONY" and no value in the buf variable?

Where can I be wrong?

#include "mbed.h"
#include "KS0108.h"
#include "Arial12.h"
#include "SystemFont5x7.h"
#include "image.h"
#include "rtos.h"
#include "loading.h"
#include "ReceiverIR.h"

// KS0108 (PinName _RST,PinName _DI, PinName _RW, PinName _E, PinName _CS1, PinName _CS2, PinName DB0, PinName DB1, PinName DB2, PinName DB3, PinName DB4, PinName DB5, PinName DB6, PinName DB7);

KS0108  display(PTC3,PTD6, PTE31, PTA17, PTC5, PTC4, PTA16, PTC17, PTC16, PTC13, PTC12, PTC11, PTC10, PTC6);
ReceiverIR ir_rx(PTA13);
Serial pc(USBTX, USBRX); // tx, rx

void LCD_thread(void const *args)
{
    while (true) {
        display.FullScreenBMP(load);
        //Thread::wait(0.5);
    }
}

void led3_thread(void const *args)
{
    while (true) {

        Thread::wait(0.5);
    }
}
int receive(RemoteIR::Format *format, uint8_t *buf, int bufsiz, int timeout = 1000) {
    int cnt = 0;
    while (ir_rx.getState() != ReceiverIR::Received) {
        wait_ms(1);
        cnt++;
        if (timeout < cnt) {
            return -1;
        }
    }
    return ir_rx.getData(format, buf, bufsiz * 8);
}
int main()
{
    display.ClearScreen();
    display.FullScreenBMP(pic);
    wait(2);
    display.ClearScreen();
    Thread thread(LCD_thread);
    Thread thread1(led3_thread);
    //display.DrawBitmap(mikro_test,5,5,BLACK);
    /*display.GotoXY(5,0);
    display.SelectFont(System5x7,BLACK,ReadData);

    display.GotoXY(1,16);
    display.PrintString("System5x7");
    wait(3);

    display.GotoXY(2,32);
    display.SelectFont(Arial12,BLACK,ReadData);
    display.PrintString("Arial 14");
    wait(3);*/
    RemoteIR::Format format;
    uint8_t buf[32];
    memset(buf, 0x00, sizeof(buf));
    while(1) {
        int bitcount;
        if (ir_rx.getState() == ReceiverIR::Received) {
            bitcount = receive(&format, buf, sizeof(buf));
            pc.printf("got>>",buf);
            switch (format) {
                case RemoteIR::UNKNOWN:
                    pc.printf("????");
                    break;
                case RemoteIR::NEC:
                    pc.printf(" NEC");
                    break;
                case RemoteIR::AEHA:
                    pc.printf("AEHA");
                    break;
                case RemoteIR::SONY:
                    pc.printf("SONY");
                    break;
            }
        }
    }

}

26 May 2013

Any problems understanding my problem?

26 May 2013

I guess you want to print the content of the receive buffer. You currently use:

            pc.printf("got>>",buf);

            switch (format) {
                case RemoteIR::UNKNOWN:
                    pc.printf("????");
                    break;
                case RemoteIR::NEC:
                    pc.printf(" NEC");
                    break;
                case RemoteIR::AEHA:
                    pc.printf("AEHA");
                    break;
                case RemoteIR::SONY:
                    pc.printf("SONY");
                    break;
            }

This code does not have a format specifier for 'buf'. Therefor, nothing gets printed. Try something like this:

            switch (format) {
                case RemoteIR::UNKNOWN:
                    pc.printf("????");
                    break;
                case RemoteIR::NEC:
                    pc.printf(" NEC");
                    break;
                case RemoteIR::AEHA:
                    pc.printf("AEHA");
                    break;
                case RemoteIR::SONY:
                    pc.printf("SONY");
                    break;
            }

            pc.printf("\n\rgot>> ");

            for (int i=0; i<sizeof(buf); i++) {
              pc.printf("0x%02X, ", buf[i]); 
            }

26 May 2013

IN the library the developer of the library uses the TEXT LCD and prints like >> lcd.printf("%02X", buf[i]); So I think here the format of buffer would be "%02X" SO iI should use pc.printf("%02X",buf[i]);

Also, I was thinking I should print without using the for() loop like this >> pc.printf("%02X", buf); is there any problem in this approach?

26 May 2013

lcd.printf("%02X", buf[i]);

The format specifier %02X means that you want hex code(X), with capital letters (X instead of x), not suppress the leading zeros (0) and use a maximum of 2 digits (2). So a '02' would be better than just '0' for the 8bit values stored in buf.

You have to use the for-loop to get the buffer contents printed. Otherwise you will just get a single value: the address of the buffer. Remember that buf is an array of values.