1 /**
2  * \file
3  *
4  * \brief Simple menu system
5  * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.
6  *
7  * \asf_license_start
8  *
9  * \page License
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  *    this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  *    this list of conditions and the following disclaimer in the documentation
19  *    and/or other materials provided with the distribution.
20  *
21  * 3. The name of Atmel may not be used to endorse or promote products derived
22  *    from this software without specific prior written permission.
23  *
24  * 4. This software may only be redistributed and used in connection with an
25  *    Atmel microcontroller product.
26  *
27  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
28  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
30  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
31  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  *
39  * \asf_license_stop
40  *
41  */
42 /*
43  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
44  */
45 
46 #include "sysfont.h"
47 #include <conf_menu.h>
48 
49 #include "gfx_mono_menu.h"
50 
51 /**
52  * \ingroup asfdoc_common2_gfx_mono_menu
53  * @{
54  */
55 
56 PROGMEM_DECLARE(gfx_mono_color_t, arrow_right_data[]) = {
57 	GFX_MONO_MENU_INDICATOR_BITMAP
58 };
59 
60 struct gfx_mono_bitmap menu_bitmap_indicator = {
61 	.height = GFX_MONO_MENU_INDICATOR_HEIGHT,
62 	.width = GFX_MONO_MENU_INDICATOR_WIDTH,
63 	.type = GFX_MONO_BITMAP_PROGMEM,
64 	.data.progmem = arrow_right_data
65 };
66 
67 /**
68  * \brief Draw menu strings and an icon by the current selection.
69  *
70  * \param[in] menu     a menu struct with menu settings
71  * \param[in] redraw   clear screen before drawing menu
72  */
menu_draw(struct gfx_mono_menu * menu,bool redraw)73 static void menu_draw(struct gfx_mono_menu *menu, bool redraw)
74 {
75 	static bool redraw_state;
76 	uint8_t i;
77 	uint8_t line = 1;
78 	uint8_t menu_page = menu->current_selection /
79 			GFX_MONO_MENU_ELEMENTS_PER_SCREEN;
80 
81 	if (menu->current_page != menu_page || redraw == true) {
82 		/* clear screen if we have changed the page or menu and prepare
83 		 * redraw */
84 		gfx_mono_draw_filled_rect(0, SYSFONT_LINESPACING,
85 				GFX_MONO_LCD_WIDTH,
86 				GFX_MONO_LCD_HEIGHT - SYSFONT_LINESPACING,
87 				GFX_PIXEL_CLR);
88 		redraw_state = true;
89 	}
90 
91 	menu->current_page = menu_page;
92 
93 	/* Clear old indicator icon */
94 	gfx_mono_draw_filled_rect(0, SYSFONT_LINESPACING,
95 			GFX_MONO_MENU_INDICATOR_WIDTH, GFX_MONO_LCD_HEIGHT -
96 			SYSFONT_LINESPACING, GFX_PIXEL_CLR);
97 
98 	/* Put indicator icon on current selection */
99 	gfx_mono_put_bitmap(&menu_bitmap_indicator, 0,
100 			SYSFONT_LINESPACING * ((menu->current_selection %
101 			GFX_MONO_MENU_ELEMENTS_PER_SCREEN) + 1));
102 
103 	/* Print visible options if page or menu has changed */
104 	if (redraw_state == true) {
105 		for (i = menu_page * GFX_MONO_MENU_ELEMENTS_PER_SCREEN;
106 				i < menu_page *
107 				GFX_MONO_MENU_ELEMENTS_PER_SCREEN +
108 				GFX_MONO_MENU_ELEMENTS_PER_SCREEN &&
109 				i < menu->num_elements; i++) {
110 			gfx_mono_draw_progmem_string(
111 					(char PROGMEM_PTR_T)menu->strings[i],
112 					GFX_MONO_MENU_INDICATOR_WIDTH + 1,
113 					line * SYSFONT_LINESPACING, &sysfont);
114 			line++;
115 		}
116 		redraw_state = false;
117 	}
118 }
119 
120 /**
121  * *\brief Initialize the menu handling. Clear screen and draw menu.
122  *
123  * \param[in] menu  menu struct with menu options
124  *
125  */
gfx_mono_menu_init(struct gfx_mono_menu * menu)126 void gfx_mono_menu_init(struct gfx_mono_menu *menu)
127 {
128 	/* Clear screen */
129 	gfx_mono_draw_filled_rect(0, 0,
130 			GFX_MONO_LCD_WIDTH, GFX_MONO_LCD_HEIGHT, GFX_PIXEL_CLR);
131 
132 	/* Draw the menu title on the top of the screen */
133 	gfx_mono_draw_progmem_string((char PROGMEM_PTR_T)menu->title,
134 			0, 0, &sysfont);
135 
136 	/* Draw menu options below */
137 	menu_draw(menu, true);
138 }
139 
140 /**
141  * \brief Update menu depending on input.
142  *
143  * \param[in] menu  menu struct with menu options
144  * \param[in] keycode  keycode to process
145  *
146  * \retval selected menu option or status code
147  */
gfx_mono_menu_process_key(struct gfx_mono_menu * menu,uint8_t keycode)148 uint8_t gfx_mono_menu_process_key(struct gfx_mono_menu *menu, uint8_t keycode)
149 {
150 	switch (keycode) {
151 	case GFX_MONO_MENU_KEYCODE_DOWN:
152 		if (menu->current_selection == menu->num_elements - 1) {
153 			menu->current_selection = 0;
154 		} else {
155 			menu->current_selection++;
156 		}
157 
158 		/* Update menu on display */
159 		menu_draw(menu, false);
160 		/* Nothing selected yet */
161 		return GFX_MONO_MENU_EVENT_IDLE;
162 
163 	case GFX_MONO_MENU_KEYCODE_UP:
164 		if (menu->current_selection) {
165 			menu->current_selection--;
166 		} else {
167 			menu->current_selection = menu->num_elements - 1;
168 		}
169 
170 		/* Update menu on display */
171 		menu_draw(menu, false);
172 		/* Nothing selected yet */
173 		return GFX_MONO_MENU_EVENT_IDLE;
174 
175 	case GFX_MONO_MENU_KEYCODE_ENTER:
176 		/* Got what we want. Return selection. */
177 		return menu->current_selection;
178 
179 	case GFX_MONO_MENU_KEYCODE_BACK:
180 		/* User pressed "back" key, inform user */
181 		return GFX_MONO_MENU_EVENT_EXIT;
182 
183 	default:
184 		/* Unknown key event */
185 		return GFX_MONO_MENU_EVENT_IDLE;
186 	}
187 }
188