SSD1306 OLED display driver  1.4.4
This library is developed to control SSD1306/SSD1331 RGB i2c/spi OLED displays and spi PCD8544 LED display
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()
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  {
68  }
69  else
70  {
72  }
73  ssd1306_charF6x8(8, index - menu->scrollPosition + 1, menu->items[index], STYLE_NORMAL );
75 }
76 
78 {
80  menu->scrollPosition = calculateScrollPosition( menu, menu->selection );
81  for (uint8_t i = menu->scrollPosition; i < min(menu->count, menu->scrollPosition + getMaxScreenItems()); i++)
82  {
83  drawMenuItem(menu, i);
84  }
85  menu->oldSelection = menu->selection;
86 }
87 
89 {
90  if (menu->selection != menu->oldSelection)
91  {
92  uint8_t scrollPosition = calculateScrollPosition( menu, menu->selection );
93  if ( scrollPosition != menu->scrollPosition )
94  {
96  ssd1306_showMenu(menu);
97  }
98  else
99  {
100  drawMenuItem(menu, menu->oldSelection);
101  drawMenuItem(menu, menu->selection);
102  menu->oldSelection = menu->selection;
103  }
104  }
105 }
106 
108 {
109  return menu->selection;
110 }
111 
113 {
114  if (menu->selection < menu->count - 1)
115  {
116  menu->selection++;
117  }
118  else
119  {
120  menu->selection = 0;
121  }
122 }
123 
125 {
126  if (menu->selection > 0)
127  {
128  menu->selection--;
129  }
130  else
131  {
132  menu->selection = menu->count - 1;
133  }
134 }
void ssd1306_showMenu(SAppMenu *menu)
Definition: ssd1306_menu.c:77
void ssd1306_negativeMode()
Definition: ssd1306.c:649
uint8_t ssd1306_displayHeight()
Definition: ssd1306.c:43
void ssd1306_positiveMode()
Definition: ssd1306.c:654
void ssd1306_updateMenu(SAppMenu *menu)
Definition: ssd1306_menu.c:88
void ssd1306_drawRect(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2)
Definition: ssd1306.c:458
uint8_t selection
currently selected item. Internally updated.
Definition: ssd1306.h:412
void ssd1306_createMenu(SAppMenu *menu, const char **items, uint8_t count)
Definition: ssd1306_menu.c:41
void ssd1306_clearScreen()
Definition: ssd1306.c:69
uint8_t scrollPosition
position of menu scrolling. Internally updated
Definition: ssd1306.h:416
#define min(a, b)
uint8_t count
count of menu items in the menu
Definition: ssd1306.h:410
uint8_t ssd1306_displayWidth()
Definition: ssd1306.c:48
uint8_t ssd1306_menuSelection(SAppMenu *menu)
Definition: ssd1306_menu.c:107
void ssd1306_menuDown(SAppMenu *menu)
Definition: ssd1306_menu.c:112
void ssd1306_menuUp(SAppMenu *menu)
Definition: ssd1306_menu.c:124
uint8_t ssd1306_charF6x8(uint8_t x, uint8_t y, const char ch[], EFontStyle style)
Definition: ssd1306.c:257
uint8_t oldSelection
selected item, when last redraw operation was performed. Internally updated.
Definition: ssd1306.h:414
const char ** items
list of menu items of the menu
Definition: ssd1306.h:408