this locks like shit
Fork of MenuLCD_copy by
Embed:
(wiki syntax)
Show/hide line numbers
menbedDisplayer.cpp
00001 #include "mbed.h" 00002 #include "include/menbedMenuMessage.h" 00003 #include "include/menbedDisplayer.h" 00004 00005 MenbedDisplayer::MenbedDisplayer (MenbedDisplay *display) : 00006 display(display) 00007 { 00008 } 00009 00010 00011 void MenbedDisplayer::update (MenbedMenuMessage *menuMessage) 00012 { 00013 char *text = new char[display->getLineLength()+1]; 00014 00015 for (int i=0; i<display->getLines(); i++) 00016 { 00017 extractLine (menuMessage->text, i, text); 00018 display->writeLine (text, i); 00019 } 00020 00021 // Print the up and down arrows if requested 00022 display->showUpArrow (menuMessage->showUpArrow); 00023 display->showDownArrow (menuMessage->showDownArrow); 00024 00025 delete[] text; 00026 } 00027 00028 00029 void MenbedDisplayer::extractLine (char *catenatedText, uint8_t lineNum, 00030 char *extractedText) 00031 { 00032 char *subText, *endText; 00033 size_t bytesToCopy; 00034 00035 extractedText[0] = '\0'; 00036 00037 // Return with just a blank line if the line number to be extracted exceeds 00038 // the number of lines in the menu 00039 if (lineNum > display->getLines() - 1) 00040 return; 00041 00042 // We loop through the catenatedString finding each \n. These \n 00043 // characters separate the substrings that we are trying to extract. 00044 subText = catenatedText; 00045 while (lineNum > 0) 00046 { 00047 subText = strchr (subText, '\n'); 00048 00049 // If the \n character was not found, the catenatedText string does not 00050 // contain enough \n-separated substrings. 00051 if (subText == (char *)NULL) 00052 { 00053 extractedText[0] = '\0'; 00054 return; 00055 } 00056 00057 // Increment the subText pointer because the strchr() command returned 00058 // a pointer to the \n character found, but next time through the loop 00059 // we want to find the next \n, not the same \n again. 00060 subText++; 00061 lineNum--; 00062 } 00063 00064 // If there are strings following the one we are trying to extract, we 00065 // change the \n terminating the string to be extracted into a \0 so that 00066 // we can use the strlen function to determine the length of the string 00067 // we are trying to extract. If there are no additional \n-separated 00068 // strings following the one we are attempting to extract, strlen should 00069 // work on subText without modification. 00070 if ((endText = strchr (subText, '\n')) != (char *)NULL) 00071 bytesToCopy = endText - subText + 1; 00072 else 00073 bytesToCopy = strlen(subText); 00074 00075 // Copy the string found in the \n-separated substring number specified by 00076 // the lineNum parameter to the extracted string. 00077 strncpy (extractedText, subText, bytesToCopy); 00078 00079 // Replace the \n at the end of extractedText string with a \0 00080 extractedText[bytesToCopy-1] = '\0'; 00081 }
Generated on Tue Jul 12 2022 22:15:04 by
1.7.2
