dialog.c

Go to the documentation of this file.
00001 // ////////////////////////////////////////////////////////////////////////////
00002 // ////////////////////////////////////////////////////////////////////////////
00015     #define     DIALOG_C
00016 
00017 
00018 // ////////////////////////////////////////////////////////////////////////////
00019 // Includes
00020 // ////////////////////////////////////////////////////////////////////////////
00021 
00022     #include    "dialog.h"
00023     #include    "sralloc.h"
00024 
00025 
00026 
00027 // ////////////////////////////////////////////////////////////////////////////
00028 // Local Defines
00029 // ////////////////////////////////////////////////////////////////////////////
00030 
00031 #define DIALOG_LETTERS_COUNT        sizeof(_LetterList)
00032 #define DIALOG_CAPITALS_COUNT       sizeof(_CapitalList)
00033 #define DIALOG_WHITESPACE_COUNT     1
00034 #define DIALOG_NUMBERS_COUNT        sizeof(_NumberList)
00035 #define DIALOG_DASH_COUNT           1
00036 #define DIALOG_SPECIAL_COUNT        10
00037 
00038 
00039 // ////////////////////////////////////////////////////////////////////////////
00040 // Local Typedefs
00041 // ////////////////////////////////////////////////////////////////////////////
00042 
00043 typedef struct
00044 {
00045     BYTE * pDispRow;
00046     BYTE MaxCursorPos;
00047     BYTE CursorPos;
00048     BYTE DispRow;
00049     BYTE ControlFlags;
00050     BOOL DisplayCursor;
00051     DialogRetValue_t RetValue;
00052 //  BYTE [] CharSetArray;
00053 } sDialogState;
00054 
00055 
00056 // ////////////////////////////////////////////////////////////////////////////
00057 // Local variables
00058 // ////////////////////////////////////////////////////////////////////////////
00059 
00060 ROM BYTE _LetterList[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
00061      'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
00062      'y', 'z' };
00063 
00064 ROM BYTE _CapitalList[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
00065      'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
00066      'Y', 'Z' };
00067 
00068 ROM BYTE _NumberList[] = {'0','1','2','3','4','5','6','7','8','9'};
00069 
00070 static sDialogState * _psState = NULL;
00071 
00072 
00073 // ////////////////////////////////////////////////////////////////////////////
00074 // Local function prototypes
00075 // ////////////////////////////////////////////////////////////////////////////
00076 
00077 // ////////////////////////////////////////////////////////////////////////////
00078 // ////////////////////////////////////////////////////////////////////////////
00079 
00080 BOOL bInitInputDialog(BYTE * pu8DefaultString, BYTE u8AllowedCharacters,
00081                       BYTE u8DispRow, BYTE u8MaxCursorPos, BYTE u8ControlFlags)
00082 {
00083     BYTE i;
00084     BYTE charSetCount;
00085     BYTE * pCharsList = NULL;
00086 
00087 inputDialogGetCharacters:
00088     // Determine the count of maximum possible input characters:
00089 
00090     charSetCount = 0;
00091     if (u8AllowedCharacters & DIALOG_LETTERS_FLAG)
00092     {
00093         charSetCount = DIALOG_LETTERS_COUNT;
00094         if (pCharsList)
00095             memcpypgm2ram(pCharsList, _LetterList, DIALOG_LETTERS_COUNT);
00096     }
00097     if (u8AllowedCharacters & DIALOG_CAPITALS_FLAG)
00098     {
00099         if (pCharsList)
00100             memcpypgm2ram(&pCharsList[charSetCount], _CapitalList, DIALOG_CAPITALS_COUNT);
00101         charSetCount += DIALOG_CAPITALS_COUNT;
00102     }
00103     if (u8AllowedCharacters & DIALOG_WHITESPACE_FLAG)
00104     {
00105         if (pCharsList)
00106             pCharsList[charSetCount] = ' ';
00107         charSetCount++;
00108     }
00109     if (u8AllowedCharacters & DIALOG_DASH_FLAG)
00110     {
00111         if (pCharsList)
00112             pCharsList[charSetCount] = '-';
00113         charSetCount++;
00114     }
00115     if (u8AllowedCharacters & DIALOG_NUMBERS_FLAG)
00116     {
00117         if (pCharsList)
00118             memcpypgm2ram(&pCharsList[charSetCount], _NumberList, DIALOG_NUMBERS_COUNT);
00119         charSetCount += DIALOG_NUMBERS_COUNT;
00120     }
00121     if (u8AllowedCharacters & DIALOG_SPECIAL_FLAG)
00122     {
00123         if (pCharsList)
00124             pCharsList[charSetCount] = '.';
00125         charSetCount++;
00126     }
00127 
00128     // Obtaining a heap array and prepare the state and character set then:
00129     if (_psState == NULL)
00130     {
00131         _psState = (sDialogState*)SRAMalloc(sizeof(sDialogState)+charSetCount);
00132 
00133         if (_psState==NULL)
00134             return FALSE;
00135 
00136         pCharsList = (BYTE *)_psState + sizeof(sDialogState);
00137         goto inputDialogGetCharacters;
00138     }
00139 
00140 
00141     _psState->CursorPos = strlen(pu8DefaultString);
00142     if (_psState->CursorPos)
00143     {
00144         // Trying to find the last character in the corresponding character     
00145         // list.
00146         _psState->CursorPos--;
00147         for(i = 0; i < charSetCount; i++)
00148         {
00149             if (pu8DefaultString[_psState->CursorPos] == pCharsList[i])
00150                 break;
00151         }
00152 
00153         if (i == charSetCount)
00154             i = 0;
00155     }   
00156     else
00157     {
00158         // Start with the first character.
00159         i = 0;
00160     }
00161 
00162 
00164     // Preparing the buttons:
00165     InputEncoderInit(i, 0, charSetCount-1, 0);
00166     InputButtonReset(ALL_BTN);
00167 
00168 
00170     // Preparing the display:
00171 
00172     _psState->MaxCursorPos = u8MaxCursorPos;
00173     _psState->DispRow = u8DispRow;
00174 
00175     if (u8DispRow==0)
00176     {
00177         _psState->pDispRow = lcdram_row1;
00178     }
00179     else
00180     {
00181         _psState->pDispRow = lcdram_row2;
00182     }
00183 
00184     strncpy(_psState->pDispRow, pu8DefaultString, LCD_CHARS_IN_A_ROW);
00185     _psState->pDispRow[LCD_CHARS_IN_A_ROW] = 0;
00186     lcd_addr1 = 0;
00187     lcd_addr2 = 0;
00188     lcd_status = LCD_CLR_MASK | LCD_ROW1_MASK | LCD_ROW2_MASK;
00189 
00190     if (u8ControlFlags & DIALOG_DESCRIPTION_FLAG)
00191     {
00192         memset(lcdram_row2, ' ', LCD_CHARS_IN_A_ROW);
00193         lcdram_row2[LCD_CHARS_IN_A_ROW] = 0;
00194         
00195         if (u8ControlFlags & DIALOG_PREV_FLAG)
00196         {
00197             lcdram_row2[0] = 0xFB;  // <<
00198         }
00199         if (u8ControlFlags & DIALOG_ESC_FLAG)
00200         {
00201             memcpypgm2ram(&lcdram_row2[4], (ROM char *) "ESC", 3);
00202         }
00203         if (u8ControlFlags & DIALOG_BACKSPACE_FLAG)
00204         {
00205             lcdram_row2[10] = 0x7F; // <-
00206         }
00207         if (u8ControlFlags & DIALOG_NEXT_FLAG)
00208         {
00209             lcdram_row2[15] = 0xFC; // >>
00210         }
00211     }
00212     _psState->ControlFlags = u8ControlFlags;
00213 
00214     OutEnableCursor(TRUE);
00215     _psState->DisplayCursor = TRUE;
00216 
00217     return TRUE;
00218 }
00219 
00220 // ////////////////////////////////////////////////////////////////////////////
00221 // ////////////////////////////////////////////////////////////////////////////
00222 
00223 BOOL bHandleInputDialog(void)
00224 {
00225     if (OutIsLCDBusy()==TRUE)
00226     {
00227         return FALSE;
00228     }
00229     else if (_psState->DisplayCursor == TRUE)
00230     {
00231         OutSetCursor(_psState->CursorPos, _psState->DispRow);
00232         _psState->DisplayCursor = FALSE;
00233         return FALSE;
00234     }
00235 
00236     if ((bInputEncoderValueChanged() == TRUE) && (_psState->CursorPos < _psState->MaxCursorPos))
00237     {
00238         BYTE*pChar=(BYTE*)_psState+sizeof(sDialogState)+s16InputEncoderGetValue();
00239         _psState->pDispRow[_psState->CursorPos] = *pChar;
00240     }
00241     else if (bInputButtonPushed(ENC_BTN) == TRUE)
00242     {
00243         // Next position is selected.
00244         InputButtonReset(ENC_BTN);
00245 
00246         if ((_psState->pDispRow[_psState->CursorPos]) && 
00247             (_psState->CursorPos < (_psState->MaxCursorPos-1)))
00248         {
00249             InputEncoderSetValue(0, TRUE);
00250             _psState->CursorPos++;
00251         }
00252     }
00253     else if ((_psState->ControlFlags & DIALOG_BACKSPACE_FLAG) &&
00254              (bInputButtonPushed(F3_BTN) == TRUE))
00255     {
00256         BYTE i = 0;
00257 
00258         // Backspace emulation
00259         InputButtonReset(F3_BTN);
00260         _psState->pDispRow[_psState->CursorPos] = ' ';
00261 
00262         if (_psState->CursorPos)
00263         {
00264             BYTE*pCharSet = (BYTE*)_psState+sizeof(sDialogState);
00265             _psState->CursorPos--;
00266             while(_psState->pDispRow[_psState->CursorPos] != pCharSet[i++]);
00267         }
00268         else
00269         {
00270             // Display the first character.
00271             i = 1;
00272         }
00273         InputEncoderSetValue(i-1, TRUE);
00274     }
00275     else if ((_psState->ControlFlags & DIALOG_NEXT_FLAG) && (bInputButtonPushed(F4_BTN) == TRUE))
00276     {
00277         // Dialog is finished with OK!
00278         _psState->RetValue = DIALOG_FINI_OK;
00279         return TRUE;
00280     }
00281     else if ((_psState->ControlFlags & DIALOG_ESC_FLAG) && (bInputButtonPushed(F2_BTN) == TRUE))
00282     {
00283         // Dialog is finished with escape!
00284         _psState->RetValue = DIALOG_FINI_ESC;
00285         return TRUE;
00286     }
00287     else if ((_psState->ControlFlags & DIALOG_PREV_FLAG) && (bInputButtonPushed(F1_BTN) == TRUE))
00288     {
00289         // Dialog is finished with cancel!
00290         _psState->RetValue = DIALOG_FINI_CANCEL;
00291         return TRUE;
00292     }
00293     else
00294     {
00295         return FALSE;
00296     }
00297 
00298     lcd_status = LCD_ROW1_MASK | LCD_ROW2_MASK;
00299     _psState->DisplayCursor = TRUE;
00300 
00301     return FALSE;
00302 }
00303 
00304 // ////////////////////////////////////////////////////////////////////////////
00305 // ////////////////////////////////////////////////////////////////////////////
00306 
00307 DialogRetValue_t FiniDialog(BYTE * pResultString)
00308 {
00309     DialogRetValue_t ret;
00310     ret = _psState->RetValue;
00311 
00312     if (_psState == NULL)
00313     {
00314         return DIALOG_FINI_CANCEL;
00315     }
00316 
00317     if ((ret == DIALOG_FINI_OK) && (pResultString))
00318     {
00319         strncpy(pResultString, _psState->pDispRow, _psState->MaxCursorPos);
00320         pResultString[_psState->MaxCursorPos] = 0;
00321     }
00322 
00323     OutEnableCursor(FALSE);
00324     InputButtonReset(ALL_BTN);
00325 
00326 
00327     SRAMfree((BYTE *)_psState);
00328     _psState = NULL;
00329 
00330     return ret;
00331 }
00332 
00333 // ////////////////////////////////////////////////////////////////////////////
00334 // ////////////////////////////////////////////////////////////////////////////

Generated on Sun Nov 27 20:02:38 2011 for eWicht by  doxygen 1.5.5