Read CSV file (Sample)

From SEGGER Wiki
Jump to: navigation, search
GUI_LANG_ReadCSV.c
GUI LANG ReadCSV.png
File(s) required
  • GUI_LANG_ReadCSV.c
Runs in simulation Yes
Runs on target Yes
Download GUI_LANG_ReadCSV.c

This sample shows how to read different languages from a .csv file that is stored in accessible memory.

Code

/*********************************************************************
*                    SEGGER Microcontroller GmbH                     *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*        (c) 1996 - 2020  SEGGER Microcontroller GmbH                *
*                                                                    *
*        Internet: www.segger.com    Support:  support@segger.com    *
*                                                                    *
**********************************************************************

** emWin V6.10 - Graphical user interface for embedded applications **
emWin is protected by international copyright laws.   Knowledge of the
source code may not be used to write a similar product.  This file may
only  be used  in accordance  with  a license  and should  not be  re-
distributed in any way. We appreciate your understanding and fairness.
----------------------------------------------------------------------
File        : GUI_LANG_ReadCSV.c
Purpose     : Sample that demonstrates how to read different languages
              from a CSV file that has been converted to an array.
              For reference on the CSV format, see the manual chapter
              "32.2.4 Rules for CSV files".
Requirements: WindowManager - ( )
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

#include "DIALOG.h"
#include <stdio.h>
#include <string.h>

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/
/*********************************************************************
*
*       _acSampleCSV
*
*  Contents:
*    "rot","red","rouge"
*    "gruen","green","vert"
*    "blau","blue","bleu"
*    "gelb","yellow","jaune"
*    "weiss","white","blanc"
*    "lila","purple","lilas"
*    "orange","orange","orange"
*    "schwarz","black","noir"
*/
static const unsigned char _acSampleCSV[194UL + 1] = {
  0x22, 0x72, 0x6F, 0x74, 0x22, 0x2C, 0x22, 0x72, 0x65, 0x64, 0x22, 0x2C, 0x22, 0x72, 0x6F, 0x75, 0x67, 0x65, 0x22, 0x0D, 0x0A, 0x22, 0x67, 0x72, 0x75, 0x65, 0x6E, 0x22, 0x2C, 0x22, 0x67, 0x72, 0x65, 0x65, 0x6E, 0x22, 0x2C, 0x22, 0x76, 0x65,
  0x72, 0x74, 0x22, 0x0D, 0x0A, 0x22, 0x62, 0x6C, 0x61, 0x75, 0x22, 0x2C, 0x22, 0x62, 0x6C, 0x75, 0x65, 0x22, 0x2C, 0x22, 0x62, 0x6C, 0x65, 0x75, 0x22, 0x0D, 0x0A, 0x22, 0x67, 0x65, 0x6C, 0x62, 0x22, 0x2C, 0x22, 0x79, 0x65, 0x6C, 0x6C, 0x6F,
  0x77, 0x22, 0x2C, 0x22, 0x6A, 0x61, 0x75, 0x6E, 0x65, 0x22, 0x0D, 0x0A, 0x22, 0x77, 0x65, 0x69, 0x73, 0x73, 0x22, 0x2C, 0x22, 0x77, 0x68, 0x69, 0x74, 0x65, 0x22, 0x2C, 0x22, 0x62, 0x6C, 0x61, 0x6E, 0x63, 0x22, 0x0D, 0x0A, 0x22, 0x6C, 0x69,
  0x6C, 0x61, 0x22, 0x2C, 0x22, 0x70, 0x75, 0x72, 0x70, 0x6C, 0x65, 0x22, 0x2C, 0x22, 0x6C, 0x69, 0x6C, 0x61, 0x73, 0x22, 0x0D, 0x0A, 0x22, 0x6F, 0x72, 0x61, 0x6E, 0x67, 0x65, 0x22, 0x2C, 0x22, 0x6F, 0x72, 0x61, 0x6E, 0x67, 0x65, 0x22, 0x2C,
  0x22, 0x6F, 0x72, 0x61, 0x6E, 0x67, 0x65, 0x22, 0x0D, 0x0A, 0x22, 0x73, 0x63, 0x68, 0x77, 0x61, 0x72, 0x7A, 0x22, 0x2C, 0x22, 0x62, 0x6C, 0x61, 0x63, 0x6B, 0x22, 0x2C, 0x22, 0x6E, 0x6F, 0x69, 0x72, 0x22, 0x00
};

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       _GetData
*/
static int _GetData(void * pVoid, const U8 ** ppData, unsigned NumBytes, U32 Off) {
  U8 * pData;
  U8 * pCSV;

  pData = (U8 *)*ppData; // Pointer to be used for getting data
  pCSV  = (U8 *)pVoid;   // Use custom void pointer for getting a pointer to the file data
  memcpy(pData, pCSV + Off, NumBytes);
  return NumBytes;
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
        int    NumLanguages;
        char   acBuffer[64];
        int    i, j;
  const char * pText;

  //
  // Init emWin.
  //
  GUI_Init();
  //
  // Read the CSV file and save the number of languages.
  //
  NumLanguages = GUI_LANG_LoadCSVEx(_GetData, _acSampleCSV);
  //
  // Print out some data.
  //
  sprintf(acBuffer, "Number of available languages: %i", NumLanguages);
  GUI_DispStringAt(acBuffer, 10, 10);
  //
  for(i = 0; i < NumLanguages; i++) {
    j = 0;
    //
    // Set index of language.
    //
    GUI_LANG_SetLang(i);
    //
    // Get pointer to text from current set language.
    //
    while(pText = GUI_LANG_GetText(j)) {
      //
      // Print the text.
      //
      GUI_DispStringAt(pText, 10 + (i * 60), 35 + (j * 12));
      j++;
    }
  }
  //
  // Free all allocated resources.
  //
  GUI_LANG_Clear();

  while (1) {
    GUI_Delay(100);
  }
}

/*************************** End of file ****************************/