]>
git.gir.st - hardpass.git/blob - oled/oled_menu.cpp
1 #include "ArduiPi_OLED_lib.h"
2 #include "Adafruit_GFX.h"
3 #include "ArduiPi_OLED.h"
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
18 // default options values
24 void testdrawchar(void) {
25 display
.setTextSize(1);
26 display
.setTextColor(WHITE
);
27 display
.setCursor(0,0);
29 for (uint8_t i
=0; i
< 168; i
++) {
30 if (i
== '\n') continue;
32 //if ((i > 0) && (i % 21 == 0))
33 //display.print("\n");
38 void print_box (char* text
, int active
) {
40 display
.setTextColor(BLACK
, WHITE
); // 'inverted' text
41 display
.printf ("%-21s", text
);
42 display
.setTextColor(WHITE
, BLACK
);
44 display
.setTextColor(WHITE
, BLACK
); // 'normal' text
45 display
.printf ("%-21s", text
);
46 display
.setTextColor(BLACK
, WHITE
);
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
);
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);
68 //display a scrollbar if bar is set >=0
71 int scroll_max
= display
.height()-1 - bar_length
/2;
72 int scroll_min
= 10 + bar_length
/2;
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
79 int the_menu (char* title
, char** items
, int max_items
) {
84 system ("/bin/stty raw");//send keystrokes immediately, not just after enter key
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) {
94 if (offset
< 0) offset
= 0; // for when <6 elements available
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
105 "", //offset+6 won't exist
106 active_item - offset, scroll_p);
116 active_item
- offset
, scroll_p
);
119 old_item
= active_item
;
123 active_item
= (active_item
+1) % max_items
;
126 active_item
= (active_item
-1+max_items
) % max_items
;
129 system ("/bin/stty cooked");//return to normal mode
134 if (getchar () == 'j') { //down
135 active_item = (active_item+1) % max_items;
138 if (getchar () == 'k') { //up
139 active_item = (active_item-1+max_items) % max_items;
142 if (getchar () == ' ') { //ok
144 printf ("%d\n", active_item);
145 return (active_item);
152 int main(int argc
, char **argv
)
154 if ( !display
.init(OLED_I2C_RESET
,opts
.oled
) ) {
160 //display.clearDisplay(); // clears the screen buffer
161 //display.display(); // display it (clear display)
163 display
.clearDisplay();
165 // draw the first ~12 characters in the font
169 //display.clearDisplay();
174 return the_menu ("Hardpass", argv
, argc
-1) + 1;
177 display
.close(); // Free PI GPIO ports