Hello,
I have interfaced SD module ,128 x 64 GLCD to Arudino Mega 2560, and 3 push-to-on buttons for selection and up/down navigation. It is working well.... I have used File Select example code from M2TK Library...i'm able navigation through file list and select the files. After selection it is displaying as
Selected file :
"filename.txt"
| ok ||
But now i have small task to be include in file select program. i.e after selecting the desired file. The number of lines in the file must be displayed on LCD and The content of the file must be sent through serial com line by line / or character by character. Please suggest me how this could be done.
code
/*
FileSelect.pde
U8glib Example
m2tklib = Mini Interative Interface Toolkit Library
>>> IMPORTANT: Ensure that the chip select line of the SD card is not shared with any pins of the display
>>> Before compiling:
>>> 1) Please remove comment from the constructor of the connected graphics display (see below).
>>> 2) Please remove comment from the mass storage subsystem below
Copyright (C) 2012 olikraus@gmail.com
/*=========================================================================*/
/* generic includes */
#include "U8glib.h"
#include "M2tk.h"
#include "utility/m2ghu8g.h"
#include <string.h>
#include "utility/mas.h"
/* === Arduino SD Library === */
/*
1) Copy "mas_arduino_sd.cpp" from M2tklib/addon/mas_arduino_sd.cpp to M2tklib/utility/mas_arduino_sd.cpp
2) Uncomment the following two lines
*/
#define FS_SD
#include <SD.h>
U8GLIB_KS0108_128 u8g(22, 23, 24, 25, 26, 27, 28, 29, 37, 33, 34, 36, 35);
/*=========================================================================*/
/* forward declarations */
M2_EXTERN_ALIGN(top_el_start);
/*=========================================================================*/
/* M2tk lib start */
M2tk m2(&top_el_start, m2_es_arduino, m2_eh_6bs, m2_gh_u8g_ffs);
/*=========================================================================*/
/* pin numbers of the keypad */
uint8_t uiKeyUpPin = 3;
uint8_t uiKeyDownPin = 4;
uint8_t uiKeySelectPin = 2;
/*=========================================================================*/
/* file selection box */
/* defines the number of additional buttons at the beginning of the STRLIST lines */
#define FS_EXTRA_MENUES 1
/* helper variables for the strlist element */
uint8_t fs_m2tk_first = 0;
uint8_t fs_m2tk_cnt = 0;
/* show selected file */
const char *fs_show_file_label_cb(m2_rom_void_p element) {
return mas_GetFilename();
}
M2_LABEL(el_show_file_label, NULL, "Selected file:");
M2_LABELFN(el_show_filename, NULL, fs_show_file_label_cb);
M2_ROOT(el_show_file_ok, NULL, "ok", &top_el_start);
M2_LIST(list_show_file) = { &el_show_file_label, &el_show_filename, &el_show_file_ok };
M2_VLIST(el_show_file_Vlist, NULL, list_show_file);
M2_ALIGN(top_el_show_file, "-1|1W64H64", &el_show_file_Vlist);
/* callback procedure for the file selection dialog */
const char *fs_strlist_getstr(uint8_t idx, uint8_t msg) {
if (msg == M2_STRLIST_MSG_GET_STR) {
/* Check for the extra button: Return string for this extra button */
if ( idx == 0 )
return "..";
/* Not the extra button: Return file/directory name */
mas_GetDirEntry(idx - FS_EXTRA_MENUES);
return mas_GetFilename();
} else if ( msg == M2_STRLIST_MSG_GET_EXTENDED_STR ) {
/* Check for the extra button: Return icon for this extra button */
if ( idx == 0 )
return "a"; /* arrow left of the m2icon font */
/* Not the extra button: Return file or directory icon */
mas_GetDirEntry(idx - FS_EXTRA_MENUES);
if ( mas_IsDir() )
return "A"; /* folder icon of the m2icon font */
return "B"; /* file icon of the m2icon font */
} else if ( msg == M2_STRLIST_MSG_SELECT ) {
/* Check for the extra button: Execute button action */
if ( idx == 0 ) {
if ( mas_GetPath()[0] == '\0' )
m2_SetRoot(&top_el_start); /* go back to previous menu */
else {
mas_ChDirUp();
m2_SetRoot(m2_GetRoot()); /* reset menu to first element, send NEW_DIALOG and force recount */
}
/* Not the extra button: Goto subdir or return (with selected file) */
} else {
mas_GetDirEntry(idx - FS_EXTRA_MENUES);
if ( mas_IsDir() ) {
mas_ChDir(mas_GetFilename());
m2_SetRoot(m2_GetRoot()); /* reset menu to first element, send NEW_DIALOG and force recount */
} else {
/* File has been selected. Here: Show the file to the user */
m2_SetRoot(&top_el_show_file);
}
}
} else if ( msg == M2_STRLIST_MSG_NEW_DIALOG ) {
/* (re-) calculate number of entries, limit no of entries to 250 */
if ( mas_GetDirEntryCnt() < 250-FS_EXTRA_MENUES )
fs_m2tk_cnt = mas_GetDirEntryCnt()+FS_EXTRA_MENUES;
else
fs_m2tk_cnt = 250;
}
return NULL;
}
M2_STRLIST(el_fs_strlist, "l6F3e15W49", &fs_m2tk_first, &fs_m2tk_cnt, fs_strlist_getstr);
M2_SPACE(el_fs_space, "W1h1");
M2_VSB(el_fs_strlist_vsb, "l6W4r1", &fs_m2tk_first, &fs_m2tk_cnt);
M2_LIST(list_fs_strlist) = { &el_fs_strlist, &el_fs_space, &el_fs_strlist_vsb };
M2_HLIST(el_top_fs, NULL, list_fs_strlist);
/*=========================================================================*/
/* a simple main menu for the file select */
M2_ROOT(el_start, NULL, "File Selection", &el_top_fs);
M2_ALIGN(top_el_start, "-1|1W64H64", &el_start);
/*=========================================================================*/
/* u8glib draw procedure: Just call the M2tklib draw procedure */
void draw(void) {
m2.draw();
}
/*=========================================================================*/
/* arduino entry points */
void setup() {
// Connect u8glib with m2tklib
m2_SetU8g(u8g.getU8g(), m2_u8g_box_icon);
// Assign u8g font to index 0
m2.setFont(0, u8g_font_5x7);
// Assign icon font to index 3
m2.setFont(3, u8g_font_m2icon_7);
// Setup keys
m2.setPin(M2_KEY_SELECT, uiKeySelectPin);
m2.setPin(M2_KEY_NEXT, uiKeyUpPin);
m2.setPin(M2_KEY_PREV, uiKeyDownPin);
// SPI backup (avoids conflict between u8g and other SPI libs)
u8g.setHardwareBackup(u8g_backup_avr_spi);
// setup storage library and mas subsystem
// CS=23 Botmat Project www.botmat.cc
// CS=10 Seeedstudio Shield http://www.seeedstudio.com/wiki/SD_Card_Shield
#if defined(FS_SdFat)
pinMode(SS, OUTPUT); // force the hardware chip select to output sdfat.begin(53, SPI_HALF_SPEED
if ( sdfat.begin(53, SPI_HALF_SPEED) ) {
mas_Init(mas_device_sdfat, (void *)&sdfat);
}
#elif defined(FS_SD)
pinMode(SS, OUTPUT); // force the hardware chip select to output
if (SD.begin(53)) { // use the global SD object
mas_Init(mas_device_sd, NULL);
}
#elif defined(FS_PFF)
//pff_arduino_chip_select_pin = 23; /* Botmat Project www.botmat.cc */
pff_arduino_chip_select_pin = 10; /* Seeedstudio Shield http://www.seeedstudio.com/wiki/SD_Card_Shield */
if ( pf_mount(&pff_fs) == FR_OK ) {
mas_Init(mas_device_pff, &pff_fs);
}
#elif defined(FS_SIM)
mas_Init(mas_device_sim, NULL);
#endif
}
void loop() {
m2.checkKey();
m2.checkKey();
if ( m2.handleKey() || m2.getRoot() == &m2_null_element ) {
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
}
m2.checkKey();
}
