I have a requirement where an event has char *newvalue
.
void event(char *newValue) {
//Code here
}
newValue
is 2 digits. If it's 1-9 it is " 1" " 2" and 10+ is "10" "11" etc. It also can contain alpha characters as well not just numbers.
I am passing this to a function void function(uint8_t ascii)
to display on a single digit display. If I pass newValue
it doesn't register less than 10 as it picks up the space.
So what I am trying to achieve is if it's a single character, strip the space somehow into a uint8_t
and if it's 2 digits strip just the right digit as a uint_8
and have a bool where I will turn on the decimal point.
I've googled like crazy and can't seem to get something workable. If somebody can help it would be appreciated. If the above is not clear please let me know what more info you need.
The code below, 'value here' is where I want to put the rightmost value from *newvalue
.
void onUfcComm2DisplayChange(char *newValue) {
comdisp.setBrightness(9);
comdisp.writeDigitAscii(0, 'valuehere');
comdisp.writeDisplay();
}
void Adafruit_AlphaNum4::writeDigitAscii(uint8_t n, uint8_t a, boolean d, boolean w) {
uint16_t font;
if (w == false) {
font = pgm_read_word(alphafonttable + a);
} else {
font = pgm_read_word(alphafonttableWD + a);
}
displaybuffer[n] = font;
if (d) displaybuffer[n] |= (1 << 14);
}
-
Please show us the sketch you're using.VE7JRO– VE7JRO2020年03月29日 04:40:18 +00:00Commented Mar 29, 2020 at 4:40
-
1C++ function strtok() can be used for spliting a str into a token, as long as there is a delimiter in the str. cplusplus.com/reference/cstring/strtok.hcheung– hcheung2020年03月29日 05:22:44 +00:00Commented Mar 29, 2020 at 5:22
-
Thats where I have been unstuck. there is no delimiter.Tekadept– Tekadept2020年03月29日 06:12:27 +00:00Commented Mar 29, 2020 at 6:12
-
I amended the basic code in original postTekadept– Tekadept2020年03月29日 06:14:30 +00:00Commented Mar 29, 2020 at 6:14
1 Answer 1
I'm not real clear on exactly what you want to have there, but here are a few ideas that may help:
If you know what character you want, then just get it. Like if you always want the second character (I think this is what you need):
void someFunc(char *p)
{
char secondChar = p[1];
Serial.print(secondChar);
}
If you just want the last character in the string no matter how long, get the length of it and then pull that character:
void someFunc(char *p)
{
int len = strlen(p);
char lastChar = p[len-1];
Serial.print(lastChar);
}
If you want the last digit in the string no matter how long and no matter how many random characters that aren't digits after it:
void someFunc(char *p)
{
int len = strlen(p);
char lastDigitChar = 0;
// Cycle backwards til we get the first digit
for(int i=len-1; i>=0; i--){
if(isDigit(p[i]){
lastDigitChar = p[i];
break;
}
}
Serial.print(lastDigitChar);
}
-
Thanks that is exactly what I was after..... I think I was just trying to overengineer the whole thing and that is just so simple it works perfectly thankyou!Tekadept– Tekadept2020年03月29日 07:18:28 +00:00Commented Mar 29, 2020 at 7:18