Hello, I know there are many convert 'this' to 'this' questions, and I believe I have gone through all of them and yet cant fix my problem, so please excuse yet another post.
I bought this LCD that has I2C to communicate 20 x 4 LCD Module With I2C & Keypad Controller
However their Library is not very complete and as to printing only handles strings and chars.
// **************************************************************
// sends char
// **************************************************************
void BV4618_I::putch(char c)
{
Wire.beginTransmission(_i2adr);
Wire.send(c);
Wire.endTransmission();
}
// **************************************************************
// sends string
// **************************************************************
void BV4618_I::puts(char *s)
{
char *sp=s;
Wire.beginTransmission(_i2adr);
// send *s as bytes of date
while(*sp)
Wire.send(*(sp++));
Wire.endTransmission();
}
I would like to create a function to send a float directly, I have tried this but my program freezes.
void BV4618_I::putf(float st, int decimales)
{
static char * sp = " ";
sp = dtostrf( st, 4, decimales, sp );
Wire.beginTransmission(_i2adr);
// send *s as bytes of date
while(*sp)
Wire.send(*(sp++));
Wire.endTransmission();
}
Any help would be great, I have spend more hours I can count with problems of my program freezing because of this.
plus it would bevery useful for others with the same display.
Why is sp static? Pointers and arrays are very similar, but there are times when one should be used in preference to another. This is a case where an array is better, since the allocated space can be written to.
The dtostrf function writes to the array in the 4th argument AND returns a pointer to where it wrote. I have no idea why this is so, but you can just ignore the return value from dtostrf.
I did look into that [PRINT CLASS] before but I use Wire library so i dont know how to integrate it.
You could start with copying the code from the print class to see if it fits your need. If that does work you should read something about inheritance.
( look at the LCD class, it uses the print class as base IIRC )
Thanks avenue but i dont need to take care of negative floats so thanks alot for that code but I used Paul's suggestion.
Paul thank you so much for your help, I have not answered earlier cause I was testing thoroughly the code before posting it here as "it's working code".
// **************************************************************
// sends string
// **************************************************************
void BV4618_I::puts(char *s)
{
char *sp=s;
Wire.beginTransmission(_i2adr);
// send *s as bytes of date
while(*sp)
Wire.send(*(sp++));
Wire.endTransmission();
}
void BV4618_I::putf(float st, int decimales)
{
char sp[16];
dtostrf( st, 4, decimales, sp );
Wire.beginTransmission(_i2adr);
// send *s as bytes of date
int len = strlen(sp);
for(int i=0; i<len; i++)
{
Wire.send(sp[i]);
}
Wire.endTransmission();
}
// other way of float
void BV4618_I::putff(float st, int decimales)
{
char sp[16];
char *p = &sp[0]; // points to start of array.
dtostrf( st, 4, decimales, sp );
Wire.beginTransmission(_i2adr);
// send *p as bytes of date
while(*p)
Wire.send(*(p++));
Wire.endTransmission();
}
void BV4618_I::puti(int st)
{
char hour_str[4]; // Define as a string (4 bytes)
itoa (st, hour_str, 10); //convert integer to string
//di.puts(hour_str); //print string on LCD
char *sp=hour_str;
Wire.beginTransmission(_i2adr);
// send *s as bytes of date
while(*sp)
Wire.send(*(sp++));
Wire.endTransmission();
}
void BV4618_I::putl(long st)
{
char hour_str[4]; // Define as a string (4 bytes)
ltoa (st, hour_str, 10); //convert integer to string
//di.puts(hour_str); //print string on LCD
char *sp=hour_str;
Wire.beginTransmission(_i2adr);
// send *s as bytes of date
while(*sp)
Wire.send(*(sp++));
Wire.endTransmission();
}
remember to modify bv_4618_I.h and add:
void putf(float st, int decimales);
void putff(float st, int decimales);
void puti(int st);
void putl(long st);
This is working, I still have some times where program freezes but I think its cause I am doing some conversion wrong before sending it to the LCD.
I will do more testing and when I have everything clear I will upload the libraries modified, for others to use.