]> git.gir.st - hardpass.git/blob - oled/oled_menu.cpp
delete unnecessary files
[hardpass.git] / oled / oled_menu.cpp
1 #include "ArduiPi_OLED_lib.h"
2 #include "Adafruit_GFX.h"
3 #include "ArduiPi_OLED.h"
4
5 #include <getopt.h>
6 #include <stdio.h>
7
8 #define ERR_FAIL 0
9 //yes, this is irritating to unix users, but a return val >= 1 will mean nth item selected
10 ArduiPi_OLED display; // Instantiate the display
11
12 // Config Option
13 struct s_opts {
14 int oled;
15 int verbose;
16 } ;
17
18 // default options values
19 s_opts opts = {
20 3, // my .96" oled
21 false // Not verbose
22 };
23
24 void testdrawchar(void) {
25 display.setTextSize(1);
26 display.setTextColor(WHITE);
27 display.setCursor(0,0);
28
29 for (uint8_t i=0; i < 168; i++) {
30 if (i == '\n') continue;
31 display.write(i);
32 //if ((i > 0) && (i % 21 == 0))
33 //display.print("\n");
34 }
35 display.display();
36 }
37
38 void print_box (char* text, int active) {
39 if (active) {
40 display.setTextColor(BLACK, WHITE); // 'inverted' text
41 display.printf ("%-21s", text);
42 display.setTextColor(WHITE, BLACK);
43 } else {
44 display.setTextColor(WHITE, BLACK); // 'normal' text
45 display.printf ("%-21s", text);
46 display.setTextColor(BLACK, WHITE);
47 }
48 }
49
50 void disp_menu (char* title, char* i1, char* i2, char* i3, char* i4, char* i5, char* i6, char* i7, int active, float bar) {
51 display.clearDisplay();
52 display.setTextSize(1);
53 display.setTextColor(WHITE);
54 display.setCursor(0,0);
55 display.print (title);
56 display.drawLine(0, 8, display.width()-1, 8, WHITE);
57
58 display.setCursor(0,10);
59 print_box (i1, active==0); if (i2 == NULL) goto no_more_elems;
60 print_box (i2, active==1); if (i3 == NULL) goto no_more_elems;
61 print_box (i3, active==2); if (i4 == NULL) goto no_more_elems;
62 print_box (i4, active==3); if (i5 == NULL) goto no_more_elems;
63 print_box (i5, active==4); if (i6 == NULL) goto no_more_elems;
64 print_box (i6, active==5); if (i7 == NULL) goto no_more_elems;
65 print_box (i7, active==6);
66 no_more_elems:
67
68 //display a scrollbar if bar is set >=0
69 if (bar >= 0) {
70 int bar_length = 6;
71 int scroll_max = display.height()-1 - bar_length/2;
72 int scroll_min = 10 + bar_length/2;
73 float scroll_p = bar;
74 display.drawLine (display.width()-1, scroll_min+scroll_p*(scroll_max-scroll_min)-bar_length/2, display.width()-1, scroll_min+scroll_p*(scroll_max-scroll_min)+bar_length/2, WHITE); //draw a scrollbar
75 }
76 display.display();
77 }
78
79 int the_menu (char* title, char** items, int max_items) {
80 int active_item = 0;
81 int old_item = -1;
82 int offset = 0;
83
84 system ("/bin/stty raw");//send keystrokes immediately, not just after enter key
85 while (1) {
86 if (old_item != active_item) {
87 if (active_item == 0) offset = 0;
88 if (active_item == max_items-1) offset = max_items-6;
89 if (active_item > offset+3 && active_item < max_items-2) { //end of menu
90 offset++; //scroll down
91 } else if (active_item < offset+3 && active_item > 2) {
92 offset--; //scroll up
93 }
94 if (offset < 0) offset = 0; // for when <6 elements available
95
96 float scroll_p = (float)active_item / (float)(max_items-1);
97 /*if (active_item >= max_items - 3) { //scrolled to the bottom - don't display another half item as there is none
98 disp_menu (title,
99 items [offset + 0],
100 items [offset + 1],
101 items [offset + 2],
102 items [offset + 3],
103 items [offset + 4],
104 items [offset + 5],
105 "", //offset+6 won't exist
106 active_item - offset, scroll_p);
107 } else {*/
108 disp_menu (title,
109 items [offset + 0],
110 items [offset + 1],
111 items [offset + 2],
112 items [offset + 3],
113 items [offset + 4],
114 items [offset + 5],
115 items [offset + 6],
116 active_item - offset, scroll_p);
117 //}
118 //display.display();
119 old_item = active_item;
120 }
121 switch (getchar()) {
122 case 'j':
123 active_item = (active_item+1) % max_items;
124 break;
125 case 'k':
126 active_item = (active_item-1+max_items) % max_items;
127 break;
128 case ' ':
129 system ("/bin/stty cooked");//return to normal mode
130 return active_item;
131 break;
132 }
133 /*
134 if (getchar () == 'j') { //down
135 active_item = (active_item+1) % max_items;
136 // debounce TODO
137 }
138 if (getchar () == 'k') { //up
139 active_item = (active_item-1+max_items) % max_items;
140 // debounce TODO
141 }
142 if (getchar () == ' ') { //ok
143 //debounce TODO
144 printf ("%d\n", active_item);
145 return (active_item);
146 }
147 */
148 }
149 }
150
151
152 int main(int argc, char **argv)
153 {
154 if ( !display.init(OLED_I2C_RESET,opts.oled) ) {
155 return ERR_FAIL;
156 }
157
158 display.begin();
159
160 //display.clearDisplay(); // clears the screen buffer
161 //display.display(); // display it (clear display)
162
163 display.clearDisplay();
164
165 // draw the first ~12 characters in the font
166 //testdrawchar();
167 //display.display();
168 //sleep(2);
169 //display.clearDisplay();
170
171 //#####mystuff
172
173 argv = &(argv[1]);
174 return the_menu ("Hardpass", argv, argc-1) + 1;
175 //############
176
177 display.close(); // Free PI GPIO ports
178 }
179
180
Imprint / Impressum