Lucas Borges
/
MenuLCD_Copy
this locks like shit
Fork of MenuLCD_copy by
menbed/menbedDisplayer.cpp@0:92357d1220f3, 2017-05-19 (annotated)
- Committer:
- ViniR
- Date:
- Fri May 19 13:07:52 2017 +0000
- Revision:
- 0:92357d1220f3
Ent?o PARA...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ViniR | 0:92357d1220f3 | 1 | #include "mbed.h" |
ViniR | 0:92357d1220f3 | 2 | #include "include/menbedMenuMessage.h" |
ViniR | 0:92357d1220f3 | 3 | #include "include/menbedDisplayer.h" |
ViniR | 0:92357d1220f3 | 4 | |
ViniR | 0:92357d1220f3 | 5 | MenbedDisplayer::MenbedDisplayer (MenbedDisplay *display) : |
ViniR | 0:92357d1220f3 | 6 | display(display) |
ViniR | 0:92357d1220f3 | 7 | { |
ViniR | 0:92357d1220f3 | 8 | } |
ViniR | 0:92357d1220f3 | 9 | |
ViniR | 0:92357d1220f3 | 10 | |
ViniR | 0:92357d1220f3 | 11 | void MenbedDisplayer::update (MenbedMenuMessage *menuMessage) |
ViniR | 0:92357d1220f3 | 12 | { |
ViniR | 0:92357d1220f3 | 13 | char *text = new char[display->getLineLength()+1]; |
ViniR | 0:92357d1220f3 | 14 | |
ViniR | 0:92357d1220f3 | 15 | for (int i=0; i<display->getLines(); i++) |
ViniR | 0:92357d1220f3 | 16 | { |
ViniR | 0:92357d1220f3 | 17 | extractLine (menuMessage->text, i, text); |
ViniR | 0:92357d1220f3 | 18 | display->writeLine (text, i); |
ViniR | 0:92357d1220f3 | 19 | } |
ViniR | 0:92357d1220f3 | 20 | |
ViniR | 0:92357d1220f3 | 21 | // Print the up and down arrows if requested |
ViniR | 0:92357d1220f3 | 22 | display->showUpArrow (menuMessage->showUpArrow); |
ViniR | 0:92357d1220f3 | 23 | display->showDownArrow (menuMessage->showDownArrow); |
ViniR | 0:92357d1220f3 | 24 | |
ViniR | 0:92357d1220f3 | 25 | delete[] text; |
ViniR | 0:92357d1220f3 | 26 | } |
ViniR | 0:92357d1220f3 | 27 | |
ViniR | 0:92357d1220f3 | 28 | |
ViniR | 0:92357d1220f3 | 29 | void MenbedDisplayer::extractLine (char *catenatedText, uint8_t lineNum, |
ViniR | 0:92357d1220f3 | 30 | char *extractedText) |
ViniR | 0:92357d1220f3 | 31 | { |
ViniR | 0:92357d1220f3 | 32 | char *subText, *endText; |
ViniR | 0:92357d1220f3 | 33 | size_t bytesToCopy; |
ViniR | 0:92357d1220f3 | 34 | |
ViniR | 0:92357d1220f3 | 35 | extractedText[0] = '\0'; |
ViniR | 0:92357d1220f3 | 36 | |
ViniR | 0:92357d1220f3 | 37 | // Return with just a blank line if the line number to be extracted exceeds |
ViniR | 0:92357d1220f3 | 38 | // the number of lines in the menu |
ViniR | 0:92357d1220f3 | 39 | if (lineNum > display->getLines() - 1) |
ViniR | 0:92357d1220f3 | 40 | return; |
ViniR | 0:92357d1220f3 | 41 | |
ViniR | 0:92357d1220f3 | 42 | // We loop through the catenatedString finding each \n. These \n |
ViniR | 0:92357d1220f3 | 43 | // characters separate the substrings that we are trying to extract. |
ViniR | 0:92357d1220f3 | 44 | subText = catenatedText; |
ViniR | 0:92357d1220f3 | 45 | while (lineNum > 0) |
ViniR | 0:92357d1220f3 | 46 | { |
ViniR | 0:92357d1220f3 | 47 | subText = strchr (subText, '\n'); |
ViniR | 0:92357d1220f3 | 48 | |
ViniR | 0:92357d1220f3 | 49 | // If the \n character was not found, the catenatedText string does not |
ViniR | 0:92357d1220f3 | 50 | // contain enough \n-separated substrings. |
ViniR | 0:92357d1220f3 | 51 | if (subText == (char *)NULL) |
ViniR | 0:92357d1220f3 | 52 | { |
ViniR | 0:92357d1220f3 | 53 | extractedText[0] = '\0'; |
ViniR | 0:92357d1220f3 | 54 | return; |
ViniR | 0:92357d1220f3 | 55 | } |
ViniR | 0:92357d1220f3 | 56 | |
ViniR | 0:92357d1220f3 | 57 | // Increment the subText pointer because the strchr() command returned |
ViniR | 0:92357d1220f3 | 58 | // a pointer to the \n character found, but next time through the loop |
ViniR | 0:92357d1220f3 | 59 | // we want to find the next \n, not the same \n again. |
ViniR | 0:92357d1220f3 | 60 | subText++; |
ViniR | 0:92357d1220f3 | 61 | lineNum--; |
ViniR | 0:92357d1220f3 | 62 | } |
ViniR | 0:92357d1220f3 | 63 | |
ViniR | 0:92357d1220f3 | 64 | // If there are strings following the one we are trying to extract, we |
ViniR | 0:92357d1220f3 | 65 | // change the \n terminating the string to be extracted into a \0 so that |
ViniR | 0:92357d1220f3 | 66 | // we can use the strlen function to determine the length of the string |
ViniR | 0:92357d1220f3 | 67 | // we are trying to extract. If there are no additional \n-separated |
ViniR | 0:92357d1220f3 | 68 | // strings following the one we are attempting to extract, strlen should |
ViniR | 0:92357d1220f3 | 69 | // work on subText without modification. |
ViniR | 0:92357d1220f3 | 70 | if ((endText = strchr (subText, '\n')) != (char *)NULL) |
ViniR | 0:92357d1220f3 | 71 | bytesToCopy = endText - subText + 1; |
ViniR | 0:92357d1220f3 | 72 | else |
ViniR | 0:92357d1220f3 | 73 | bytesToCopy = strlen(subText); |
ViniR | 0:92357d1220f3 | 74 | |
ViniR | 0:92357d1220f3 | 75 | // Copy the string found in the \n-separated substring number specified by |
ViniR | 0:92357d1220f3 | 76 | // the lineNum parameter to the extracted string. |
ViniR | 0:92357d1220f3 | 77 | strncpy (extractedText, subText, bytesToCopy); |
ViniR | 0:92357d1220f3 | 78 | |
ViniR | 0:92357d1220f3 | 79 | // Replace the \n at the end of extractedText string with a \0 |
ViniR | 0:92357d1220f3 | 80 | extractedText[bytesToCopy-1] = '\0'; |
ViniR | 0:92357d1220f3 | 81 | } |