SSD1306 OLED display driver 1.7.8 This library is developed to control SSD1306/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays

ssd1306_menu.c
1 /*
2 MIT License
3
4 Copyright (c) 2017-2018, Alexey Dynda
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24
25 #include "font6x8.h"
26 #include "ssd1306.h"
27
28 #ifndef min
29 #define min(x,y) ((x)<(y)?(x):(y))
30 #endif
31
32 #ifndef max
33 #define max(x,y) ((x)>(y)?(x):(y))
34 #endif
35
36 static uint8_t getMaxScreenItems(void)
37 {
38 return (ssd1306_displayHeight() >> 3) - 2;
39 }
40
41 void ssd1306_createMenu(SAppMenu *menu, const char **items, uint8_t count)
42 {
43 menu->items = items;
44 menu->count = count;
45 menu->selection = 0;
46 menu->oldSelection = 0;
47 menu->scrollPosition = 0;
48 }
49
50 static uint8_t calculateScrollPosition(SAppMenu *menu, uint8_t selection)
51 {
52 if ( selection < menu->scrollPosition )
53 {
54 return selection;
55 }
56 else if ( selection - menu->scrollPosition > getMaxScreenItems() - 1)
57 {
58 return selection - getMaxScreenItems() + 1;
59 }
60 return menu->scrollPosition;
61 }
62
63 static void drawMenuItem(SAppMenu *menu, uint8_t index)
64 {
65 if (index == menu->selection)
66 {
67 ssd1306_negativeMode();
68 }
69 else
70 {
71 ssd1306_positiveMode();
72 }
73 ssd1306_printFixed(8, (index - menu->scrollPosition + 1)*8, menu->items[index], STYLE_NORMAL );
74 ssd1306_positiveMode();
75 }
76
77 static void drawMenuItem8(SAppMenu *menu, uint8_t index)
78 {
79 if (index == menu->selection)
80 {
81 ssd1306_negativeMode();
82 }
83 else
84 {
85 ssd1306_positiveMode();
86 }
87 ssd1306_printFixed8(8, (index - menu->scrollPosition + 1)*8, menu->items[index], STYLE_NORMAL );
88 ssd1306_positiveMode();
89 }
90
91 void ssd1306_showMenu(SAppMenu *menu)
92 {
93 ssd1306_drawRect(4, 4, ssd1306_displayWidth() - 5, ssd1306_displayHeight() - 5);
94 menu->scrollPosition = calculateScrollPosition( menu, menu->selection );
95 for (uint8_t i = menu->scrollPosition; i < min(menu->count, menu->scrollPosition + getMaxScreenItems()); i++)
96 {
97 drawMenuItem(menu, i);
98 }
99 menu->oldSelection = menu->selection;
100 }
101
102 void ssd1306_showMenu8(SAppMenu *menu)
103 {
104 ssd1306_drawRect8(4, 4, ssd1306_displayWidth() - 5, ssd1306_displayHeight() - 5);
105 menu->scrollPosition = calculateScrollPosition( menu, menu->selection );
106 for (uint8_t i = menu->scrollPosition; i < min(menu->count, menu->scrollPosition + getMaxScreenItems()); i++)
107 {
108 drawMenuItem8(menu, i);
109 }
110 menu->oldSelection = menu->selection;
111 }
112
113 void ssd1306_updateMenu(SAppMenu *menu)
114 {
115 if (menu->selection != menu->oldSelection)
116 {
117 uint8_t scrollPosition = calculateScrollPosition( menu, menu->selection );
118 if ( scrollPosition != menu->scrollPosition )
119 {
120 ssd1306_clearScreen();
121 ssd1306_showMenu(menu);
122 }
123 else
124 {
125 drawMenuItem(menu, menu->oldSelection);
126 drawMenuItem(menu, menu->selection);
127 menu->oldSelection = menu->selection;
128 }
129 }
130 }
131
132 void ssd1306_updateMenu8(SAppMenu *menu)
133 {
134 if (menu->selection != menu->oldSelection)
135 {
136 uint8_t scrollPosition = calculateScrollPosition( menu, menu->selection );
137 if ( scrollPosition != menu->scrollPosition )
138 {
139 ssd1306_clearScreen8();
140 ssd1306_showMenu8(menu);
141 }
142 else
143 {
144 drawMenuItem8(menu, menu->oldSelection);
145 drawMenuItem8(menu, menu->selection);
146 menu->oldSelection = menu->selection;
147 }
148 }
149 }
150
151 uint8_t ssd1306_menuSelection(SAppMenu *menu)
152 {
153 return menu->selection;
154 }
155
156 void ssd1306_menuDown(SAppMenu *menu)
157 {
158 if (menu->selection < menu->count - 1)
159 {
160 menu->selection++;
161 }
162 else
163 {
164 menu->selection = 0;
165 }
166 }
167
168 void ssd1306_menuUp(SAppMenu *menu)
169 {
170 if (menu->selection > 0)
171 {
172 menu->selection--;
173 }
174 else
175 {
176 menu->selection = menu->count - 1;
177 }
178 }
ssd1306_drawRect8
void ssd1306_drawRect8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
Definition: ssd1306_8bit.c:184
ssd1306_printFixed8
uint8_t ssd1306_printFixed8(lcdint_t x, lcdint_t y, const char *ch, EFontStyle style)
Definition: ssd1306_8bit.c:329
ssd1306_positiveMode
void ssd1306_positiveMode()
Definition: ssd1306_1bit.c:875
ssd1306_createMenu
void ssd1306_createMenu(SAppMenu *menu, const char **items, uint8_t count)
Definition: ssd1306_menu.c:41
ssd1306.h
font6x8.h
ssd1306_printFixed
uint8_t ssd1306_printFixed(uint8_t xpos, uint8_t y, const char *ch, EFontStyle style)
Definition: ssd1306_1bit.c:78
ssd1306_drawRect
void ssd1306_drawRect(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2)
Definition: ssd1306_1bit.c:636
ssd1306_menuSelection
uint8_t ssd1306_menuSelection(SAppMenu *menu)
Definition: ssd1306_menu.c:151
ssd1306_showMenu
void ssd1306_showMenu(SAppMenu *menu)
Definition: ssd1306_menu.c:91
ssd1306_updateMenu
void ssd1306_updateMenu(SAppMenu *menu)
Definition: ssd1306_menu.c:113
ssd1306_displayWidth
uint8_t ssd1306_displayWidth(void)
Definition: ssd1306_generic.c:63
min
#define min(a, b)
Definition: nano_gfx_types.h:35
ssd1306_menuUp
void ssd1306_menuUp(SAppMenu *menu)
Definition: ssd1306_menu.c:168
ssd1306_updateMenu8
void ssd1306_updateMenu8(SAppMenu *menu)
Definition: ssd1306_menu.c:132
ssd1306_menuDown
void ssd1306_menuDown(SAppMenu *menu)
Definition: ssd1306_menu.c:156
ssd1306_clearScreen8
void ssd1306_clearScreen8(void)
Definition: ssd1306_8bit.c:107
ssd1306_displayHeight
uint8_t ssd1306_displayHeight(void)
Definition: ssd1306_generic.c:58
ssd1306_showMenu8
void ssd1306_showMenu8(SAppMenu *menu)
Definition: ssd1306_menu.c:102
ssd1306_clearScreen
void ssd1306_clearScreen()
Definition: ssd1306_1bit.c:64
ssd1306_negativeMode
void ssd1306_negativeMode()
Definition: ssd1306_1bit.c:870