2014年2月28日 星期五

Arduino LCD 功能表使用範例

上一篇發表了我的 LCD_MENU 物件的源始碼,以下就是這個 LCD_MENU 使用範例:

//------------------------------------------------------------------------------
//檔案: examples.ino
//
//作者:Anthony Lee (Dephi.Ktop: skcc)
//
//日期:二零一四年二月一日
//
//備註: 關於 LCD 連接,請參照相關LCD廠商連接方法。本例子是選用 I2C 的 LCD 和 UNO
// 版子,所以需要使用 A4 (SDA)和 A5(SDL)。
// 有關 I2C 可參閱:http://arduino.cc/en/reference/wire
// 如需轉載請列明出處,請尊重知識產權。
//
//按鈕設定:
// A0 - 上 (上一個選項)
// A1 - 下 (下一個選項)
// A2 - 左 (返回鍵)
// A3 - 右 (選擇鍵)
//------------------------------------------------------------------------------
#include  
#include

#include "LCD_MENU.h"

#define HIGH_VALUE 700
#define DELAY_TIME 250

LiquidCrystal_I2C lcd(0x27, MAX_SCREEN_CHAR, MAX_ROW);

CONTENT *MainContent = new CONTENT();

  
LCD_MENU a_lcd_menu = LCD_MENU("Main Menu");
#define Main_Item_Num  7
char * MenuItemArray[]= {   "Item 1", //0
                            "Sub Menu 1", //1
                            "Item 3", //2
                            "Item 4", //3
                            "Item 5", //4
                            "Item 6", //5
                            "Item 7" //6                            
};


LCD_MENU a_sub_menu_1 = LCD_MENU("Sub Menu 1"); 
#define Sub_Menu_1_Num 5                   
char * SubMenuItemArray_1[]={ "Sub Item 1-1", //100
       "Sub Item 1-2", //101
       "Sub Menu 2", //102
       "Sub Item 1-4", //103
       "Analog 0-3"}; //104
    

LCD_MENU a_sub_menu_2 = LCD_MENU("Sub Menu 2");                            
#define Sub_Menu_2_Num 5
char * SubMenuItemArray_2[]={ "Sub Item 2-1",   //200
         "Sub Item 2-2", //201
         "Sub Item 2-3", //202
         "Sub Item 2-4", //203
         "Sub Item 2-5"}; //204

               
LCD_MENU *CurrentMenu;
LCD_MENU *EnterMenu;

char buff_temp[ MAX_SCREEN_CHAR + 1 ];

int btn_Up;
int btn_Down;
int btn_Left;
int btn_Right;

int LiveUpdateItem;


void setup()

  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  
  Build_MainMenu();
  

  LiveUpdateItem = -1;
  CurrentMenu = &a_lcd_menu; 
  LCD_Show( &lcd, CurrentMenu );
  
}
  
void loop()
{
  //Key Capture...
  btn_Up = analogRead(A0);
  btn_Down = analogRead(A1);
  btn_Left = analogRead(A2);
  btn_Right = analogRead(A3);
    
  memset(buff_temp, 0, MAX_SCREEN_CHAR + 1 );
  
  if( btn_Up > HIGH_VALUE || btn_Down > HIGH_VALUE || btn_Left > HIGH_VALUE || btn_Right > HIGH_VALUE )
  {
    if( btn_Up > HIGH_VALUE && btn_Down < HIGH_VALUE && btn_Left < HIGH_VALUE && btn_Right < HIGH_VALUE )
    {  
      Button_UP();
    }
    else
    if( btn_Down > HIGH_VALUE && btn_Up < HIGH_VALUE && btn_Left < HIGH_VALUE && btn_Right < HIGH_VALUE )
    {
      Button_DOWN();
    }
    else
    if( btn_Down < HIGH_VALUE && btn_Up < HIGH_VALUE && btn_Left > HIGH_VALUE && btn_Right < HIGH_VALUE )
    {
      Button_LEFT();
    }
    else
    if( btn_Down < HIGH_VALUE && btn_Up < HIGH_VALUE && btn_Left < HIGH_VALUE && btn_Right > HIGH_VALUE )
    {
  Button_RIGHT();
    } 
    
    LCD_Show( &lcd, CurrentMenu );      
  }
  else
  {  
 switch( LiveUpdateItem )
 {
  case 104:
   memset(buff_temp, 0, MAX_SCREEN_CHAR + 1 );
  sprintf(buff_temp, "%d %d %d %d", btn_Up, btn_Down, btn_Left, btn_Right);
   MainContent->Update_Line( 1, buff_temp );
   LCD_Show( &lcd, CurrentMenu );
   delay( 1000 - DELAY_TIME );
  break;
 }
  }    
  delay( DELAY_TIME );
}

void Build_MainMenu()
{
  MENU_ITEM* a_Item;
  for(int i=0; i
  {
  if( i == 1 )
    {
  Build_Sub_Menu_1();   
    a_Item = new MENU_ITEM( i, MenuItemArray[i], &a_lcd_menu, NULL, &a_sub_menu_1, NULL );
    }
    else
  a_Item = new MENU_ITEM( i, MenuItemArray[i], &a_lcd_menu, NULL, NULL, NULL );
  a_lcd_menu.Add_Item( a_Item );
 
  }
  a_lcd_menu.init();
}

void Build_Sub_Menu_1()
{
  MENU_ITEM* a_Item;
  for(int j=0; j
  {    
  if( j == 2 )
    {
  Build_Sub_Menu_2();   
    a_Item = new MENU_ITEM( j + 100, SubMenuItemArray_1[j], &a_sub_menu_1, &a_lcd_menu, &a_sub_menu_2, NULL );
    }
    else    
  a_Item = new MENU_ITEM( j + 100, SubMenuItemArray_1[j], &a_sub_menu_1, &a_lcd_menu, NULL, NULL );
  a_sub_menu_1.Add_Item( a_Item );
 
  }
  a_sub_menu_1.init();
}

void Build_Sub_Menu_2()
{
  MENU_ITEM* a_Item;
  for(int k=0; k
  {
  a_Item = new MENU_ITEM( k + 200, SubMenuItemArray_2[k], &a_sub_menu_2, &a_sub_menu_1, NULL, NULL );
  a_sub_menu_2.Add_Item( a_Item );
 
  }
  a_sub_menu_2.init();
}


void Button_UP()
{
  CurrentMenu->Action_Up();
  Serial.println( "UP" );
}

void Button_DOWN()
{
  CurrentMenu->Action_Down();
  Serial.println( "DOWN" );
}

void Button_LEFT()
{
CurrentMenu = (LCD_MENU *) CurrentMenu->Action_Back();
LiveUpdateItem = -1;
Serial.println( "LEFT" ); 
}

void Button_RIGHT()
{
  EnterMenu = (LCD_MENU *) CurrentMenu->Action_Enter();
  if( EnterMenu == NULL )
  {
sprintf(buff_temp, "Item ID: %d", CurrentMenu->Selected->Item_ID );   
 Serial.println( buff_temp );
   
    switch( CurrentMenu->Selected->Item_ID )
    {
   
    case 104:    
    MainContent->Clear_All();
    MainContent->Add_Line("Analog Read");
   memset(buff_temp, 0, MAX_SCREEN_CHAR + 1 );
  sprintf(buff_temp, "%d %d %d %d", btn_Up, btn_Down, btn_Left, btn_Right);    
    MainContent->Add_Line( buff_temp );
    MainContent->init();
sprintf(buff_temp, "LineCount: %d", MainContent->LineCount );   
  Serial.println( buff_temp );    
    CurrentMenu->Content_Page = MainContent;
    break;
   
    default:
     MainContent->Clear_All();      
MainContent->Add_Line( CurrentMenu->Selected->Item_Name );
MainContent->Add_Line("This is a default");
MainContent->Add_Line("page.");
MainContent->Add_Line("Hello 1");
MainContent->Add_Line("Hello 2");
MainContent->Add_Line("Hello 3");
MainContent->Add_Line("Hello 4");
MainContent->Add_Line("Hello 5");
MainContent->init();
sprintf(buff_temp, "LineCount: %d", MainContent->LineCount );   
  Serial.println( buff_temp );    
     CurrentMenu->Content_Page = MainContent;      
    break;
    }
LiveUpdateItem = CurrentMenu->Selected->Item_ID;
  }
  else
  {
  CurrentMenu = EnterMenu;
  LiveUpdateItem = -1;
  }
  Serial.println( "RIGHT" );

}



2 則留言:

  1. 您好,我使用你的程式進行執行的動作,
    但發現在
    void Build_MainMenu()
    {
    MENU_ITEM* a_Item;
    for(int i=0; i
    {
    出現了錯誤,不知道是不是程式的部分有漏掉的地方?

    回覆刪除
    回覆
    1. 咦…為什麼有些 code 沒顯示出來呢?應該是因為code 裡有這個字符:〈
      所以該 function call 完整該是:

      void Build_MainMenu()
      {
      MENU_ITEM* a_Item;
      for(int i=0; i〈Main_Item_Num; i++ )
      {
      if( i == 1 )
      {
      Build_Sub_Menu_1();
      a_Item = new MENU_ITEM( i, MenuItemArray[i], &a_lcd_menu, NULL, &a_sub_menu_1, NULL );
      }
      else
      a_Item = new MENU_ITEM( i, MenuItemArray[i], &a_lcd_menu, NULL, NULL, NULL );
      a_lcd_menu.Add_Item( a_Item );

      }
      a_lcd_menu.init();
      }

      刪除