I'm new to Arduino and currently learn to use PROGMEM to store variables so that I can save dynamic memory. I have 13 variables including these three below that I store using PROGMEM.
Here are some of example of variables that I store and use it in my functions :-
const unsigned int raw_0[62] PROGMEM = {2600,850,400,500,400,500,450,850,450,850,1350,850,450,450,400,500,400,450,450,400,450,450,450,450,400,450,900,850,900,850,900,450,450,850,900,850,900,850,450,450,900,450,400,450,400,900,450,450,450,400,450,450,450,450,400,450,450,450,450,400,450,};
const unsigned int raw_1[60] PROGMEM = {2600,850,450,450,450,450,450,850,450,850,1350,850,500,400,450,400,450,450,450,450,400,450,450,450,400,450,900,850,900,900,850,450,450,850,850,900,900,900,400,450,900,450,450,400,450,850,450,450,450,450,400,450,450,450,450,400,450,450,850,};
const unsigned int raw_a[100] PROGMEM = {3500,1700,400,450,450,1250,450,400,450,400,450,400,500,400,450,400,450,400,450,400,450,450,400,400,500,400,450,400,450,1300,400,450,450,400,450,400,450,400,450,400,450,400,500,350,500,400,450,400,450,1300,400,400,500,400,450,400,450,400,450,450,400,450,450,400,450,400,450,400,450,400,450,450,400,450,450,400,450,1250,450,400,450,400,500,400,450,400,450,400,450,400,450,400,450,1300,450,400,450,1250,450,};
Here is the table that store the variables. I learn this approach from Arduino website; https://www.arduino.cc/en/Reference/PROGMEM .
const unsigned int* const myTable[13] PROGMEM = {
raw_0,
raw_1,
raw_2,
raw_3,
raw_4,
raw_5,
raw_6,
raw_7,
raw_8,
raw_9,
raw_a,
raw_b,
raw_c};
My problem is, how do I retrieve these variables using PROGMEM such as raw_1 and raw_a ?
This is what I did but it did not work :-
unsigned int * ptr = (unsigned int *) pgm_read_word (&myTable [1]);
irsend.sendRaw(ptr,62,38);
Most of examples that I found, they use String or char datatype but in my case, I use array integer.
The ptr is also pointer to PROGMEM, so you have to read the value (or values in this case) by pgm_read_word. The IR library doesn't support that at all (I hope it's the correct one).
Anyway sendRaw implementation is this:
void IRsend::sendRaw (const unsigned int buf[], unsigned int len, unsigned int hz)
{
// Set IR carrier frequency
enableIROut(hz);
for (unsigned int i = 0; i < len; i++) {
if (i & 1) space(buf[i]) ;
else mark (buf[i]) ;
}
space(0); // Always end with the LED off
}
And all used methods are public, so you can implement your own function to do the same:
void mySendRaw (IRsend & dev, const unsigned int buf[], unsigned int len, unsigned int khz)
{
// Set IR carrier frequency
dev.devenableIROut(khz);
for (unsigned int i = 0; i < len; i++) {
if (i & 1) dev.space(pgm_read_word(buf+i));
else dev.mark (pgm_read_word(buf+i));
}
dev.space(0); // Always end with the LED off
}
// And usage:
mySendRaw(irsend, (const uint16_t*)pgm_read_word(myTable+1), 62, 38);
However the size of arrays should be stored somewhere too, so you can use something like:
byte cmd = 1;
mySendRaw(irsend, (const uint16_t*)pgm_read_word(myTable+cmd), pgm_read_word(myTableLenghts+cmd), 38);
Hi can anyone help me to convert LPTSTR to QString
You will see in the docs that Qstring provides static function to convert from both ascii and Unicode strings:
QString fromAscii ( const char *
ascii, int len = -1 )
QString fromLatin1 ( const char *
chars, int len = -1 )
QString fromUtf8 ( const char * utf8,
int len = -1 )
QString fromLocal8Bit ( const char *
local8Bit, int len = -1 )
QString fromUcs2 ( const unsigned
short * str )
Check whether you are using ascii or unicode and pick your poison.
QString::fromWCharArray is what worked for me.
To convert QString to LPTSTR or LPCTSTR:
QString src;
LPTSTR dest=(LPTSTR)src.utf16();
to convert from LPTSTR or LPCTSTR to QString :
src=QString::fromUtf16(dest);
The solution expects that your LPTSTR array is null-terminated.
STRRET str; // it had been filled by a winapi function
// str.pOleStr is LPWSTR, i.e. wchar_t* field of the union STRRET
// LPWSTR is the same as LPTSTR (see further)
QString result{QString::fromWCharArray(str.pOleStr)};
As shown below, STRRET is a union, one of representations of which (named pOleStr) has type wchar_t*.
As far as I understand, when one gets this STRRET filled by a Winapi function (like e.g. IShellFolder::GetDisplayNameOf()), it's a normal null-terminated wide string. So it can be supplied to QString::fromWCharArray(const wchar_t *string). The function will preserve the input wchar_t-array as it is and will just create a new copy inside a new QString. So overall, I think the method in my answer is quite safe as far as you know that your const wchar_t * is ended with a \0-wide-character.
// shtypes.h
typedef struct _STRRET
{
UINT uType;
/* [switch_is][switch_type] */ union
{
/* [case()][string] */ LPWSTR pOleStr;
/* [case()] */ UINT uOffset;
/* [case()] */ char cStr[ 260 ];
} DUMMYUNIONNAME;
} STRRET;
// winnt.h
typedef _Null_terminated_ WCHAR *NWPSTR, *LPWSTR, *PWSTR;
typedef LPWSTR PTSTR, LPTSTR;
Use QString::fromUcs2 to convert strings.
This is woking fine
QString str("ddddd");
LPCTSTR lstr = (LPCTSTR)str.data();