Saturday, July 21, 2012

Menu and New Function

Counter Function Description

I'd like to add a "Counter" function into the watch.

  1.    Press "Up" key, counter add 1.  
  2.    Long press "*" key, counter reset to zero.
I'd like to add counter function after Time function.

File "counter.h"


#ifndef COUNTER_H_
#define COUNTER_H_

// **************************************************************
// Include section


//***************************************************************
// Prototypes section

// internal functions
extern void reset_counter(void);

// menu functions
extern void mx_counter(u8 line);
extern void sx_counter(u8 line);
extern void display_counter(u8 line, u8 update);


//***************************************************************
// Defines section


//***************************************************************
// Global Variable section
struct counter
{
// MENU_ITEM_NOT_VISIBLE, MENU_ITEM_VISIBLE
menu_t state;
s16 count;
};
extern struct counter sCounter;


#endif /*COUNTER_H_*/

File "counter.c"


// system
#include "project.h"
// driver
#include "counter.h"
#include "ports.h"
#include "display.h"
//#include "adc12.h"
//#include "timer.h"

// logic
#include "user.h"
// Global Variable section
struct counter sCounter;
void reset_counter(void)
{
  sCounter.state = MENU_ITEM_NOT_VISIBLE; 
sCounter.count = 0;
}

void mx_counter(u8 line)
{
sCounter.count = 0;
display.flag.update_counter = 1;
}
void sx_counter(u8 line)
{
sCounter.count ++;
display.flag.update_counter = 1;
}
void display_counter(u8 line, u8 update)
{
u8 * str;

// Redraw line
if (update == DISPLAY_LINE_UPDATE_FULL)
{
// Set battery and V icon
display_symbol(LCD_SYMB_ARROW_UP, SEG_ON);

// Menu item is visible
sCounter.state = MENU_ITEM_VISIBLE; 

// Display result in xxxx format
str = itoa(sCounter.count, 4, 0);

display_chars(LCD_SEG_L1_3_0, str, SEG_ON);
}
else if (update == DISPLAY_LINE_UPDATE_PARTIAL)
{
// Display result in xxxx format
str = itoa(sCounter.count, 4, 0);

display_chars(LCD_SEG_L1_3_0, str, SEG_ON);

display.flag.update_counter = 0;
}
else if (update == DISPLAY_LINE_CLEAR)
{
// Menu item is not visible
sCounter.state = MENU_ITEM_NOT_VISIBLE;
// Clear function-specific symbols
display_symbol(LCD_SYMB_ARROW_UP, SEG_OFF);
}
}

In file "main.c"



// logic
#include "menu.h"
#include "date.h"
#include "alarm.h"
#include "stopwatch.h"
#include "battery.h"
#include "temperature.h"
#include "altitude.h"
#include "battery.h"
#include "acceleration.h"
#include "bluerobin.h"
#include "rfsimpliciti.h"
#include "simpliciti.h"
#include "test.h"
#include "counter.h"
....
void init_global_variables(void)
{
.....
// Read calibration values from info memory
read_calibration_values();
// reset counter
reset_counter();
}

In file "display.h", add another flag "update_counter"

typedef union
{
  struct
  {
  // Line1 + Line2 + Icons
    u16 full_update       : 1;    // 1 = Redraw all content
    u16 partial_update       : 1;    // 1 = Update changes
 
  // Line only
    u16 line1_full_update     : 1;    // 1 = Redraw Line1 content
    u16 line2_full_update     : 1;    // 1 = Redraw Line2 content

// Logic module data update flags
    u16 update_time       : 1;    // 1 = Time was updated 
    u16 update_stopwatch     : 1;    // 1 = Stopwatch was updated
    u16 update_temperature   : 1;    // 1 = Temperature was updated
    u16 update_battery_voltage : 1;    // 1 = Battery voltage was updated
    u16 update_date       : 1;    // 1 = Date was updated
    u16 update_alarm       : 1;    // 1 = Alarm time was updated
    u16 update_acceleration : 1; // 1 = Acceleration data was updated
    u16 update_counter : 1;    // 1 = counter was update
  } flag;
  u16 all_flags;            // Shortcut to all display flags (for reset)
} s_display_flags;


Menu Manipulation 

Add a menu after "Time" function.

File "menu.c"
In file menu.c, add a function first,


u8 update_counter(void)
{
return (display.flag.update_counter);
}

Then add counter menu

// Line 1 - Counter
const struct menu menu_L1_Counter = 
{
   FUNCTION( sx_counter ),
   FUNCTION( mx_counter ),
   FUNCTION( display_counter ),
   FUNCTION( update_counter ),
   &menu_L1_Alarm,
};

Change the Time menu next menu pointer,
// Line1 - Time

const struct menu menu_L1_Time =
{
FUNCTION(sx_time), // direct function
FUNCTION(mx_time), // sub menu function
FUNCTION(display_time), // display function
FUNCTION(update_time), // new display data
// &menu_L1_Alarm,
&menu_L1_Counter,
};

That's it.


2 comments:

  1. I did everything in the article, but I have an error:
    "../logic/menu.c", line 180: error #20: identifier "menu_L1_Counter" is undefined

    ReplyDelete
    Replies
    1. Search your file menu.c for menu_L1_Counter. If it is defined, move it's definition before the definition of menu_L1_Time.

      Delete

Note: Only a member of this blog may post a comment.