Skip to content

Commit e8e85a8

Browse files
author
doncov.eugene
committed
init
1 parent 5f124d9 commit e8e85a8

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed

examples/multi_desktop_serial.cpp

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include "Arduino.h"
2+
#include "stdlib.h"
3+
4+
#include "multi_desktop.h"
5+
6+
#define LED_WORK 3
7+
#define BUTTON_1 4
8+
#define BUTTON_2 5
9+
10+
11+
void print(const char *buffer, int ms)
12+
{
13+
Serial.println(buffer); delay(ms);
14+
}
15+
16+
void up() { print("up pressed", 1000); }
17+
18+
void down() { print("down pressed", 1000); }
19+
20+
void led_button() { digitalWrite(LED_WORK, !digitalRead(LED_WORK)); }
21+
22+
void menu_button() { print("MENU", 1000); }
23+
24+
void show_1_screen()
25+
{
26+
print("MAIN DESKTOP", 0);
27+
print("[ + ] [ - ] [ * D2]", 0);
28+
}
29+
30+
void show_2_screen()
31+
{
32+
digitalRead(LED_WORK) ? print("LED on", 0) : print("LED off", 0);
33+
print("[ + LED] [ * D3]", 0);
34+
}
35+
36+
void show_3_screen()
37+
{
38+
print("LAST DESKTOP", 0);
39+
print("[ + MENU] [ * D1]", 0);
40+
}
41+
42+
43+
#define BUTTONS 2
44+
int8_t codes[] = { '+' , '-' };
45+
46+
func desktop1cb[] = { &up, &down };
47+
func desktop2cb[] = { &led_button, 0 };
48+
func desktop3cb[] = { &menu_button, 0 };
49+
50+
MultiDesktop<BUTTONS> multi_desktop( '*' , codes);
51+
Desktop<BUTTONS> mainDesktop(&show_1_screen, desktop1cb);
52+
Desktop<BUTTONS> secondDesktop(&show_2_screen, desktop2cb);
53+
Desktop<BUTTONS> thirdDesktop(&show_3_screen, desktop3cb);
54+
55+
56+
void setup()
57+
{
58+
pinMode(LED_WORK, OUTPUT);
59+
pinMode(BUTTON_1,INPUT);
60+
pinMode(BUTTON_2,INPUT);
61+
62+
digitalWrite(LED_WORK, LOW);
63+
64+
Serial.begin(9600);
65+
66+
print("HELLO", 500);
67+
68+
multi_desktop.add_desktop(&mainDesktop);
69+
multi_desktop.add_desktop(&secondDesktop);
70+
multi_desktop.add_desktop(&thirdDesktop);
71+
72+
multi_desktop.show();
73+
}
74+
75+
void loop()
76+
{
77+
if (Serial.available())
78+
{
79+
char ch = Serial.read();
80+
81+
multi_desktop.button_pressed(ch);
82+
83+
multi_desktop.show(); // comfort if Serial print
84+
}
85+
86+
//multi_desktop.show(); // if using LCD or show changed data
87+
88+
delay(1); // delay in between reads for stability
89+
}

img/main.png

42.2 KB
Loading

multidesktop.h

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
2+
#ifndef MULTI_DESKTOP_H
3+
#define MULTI_DESKTOP_H
4+
5+
extern "C" {
6+
typedef void (*func)(void);
7+
}
8+
9+
/**
10+
@brief Desktop class
11+
@param show_ - callback function when this Desktop is active
12+
@param cb - callback functions for all N buttons. i function called when user press i button.
13+
*/
14+
template <int8_t N> //keys
15+
class Desktop
16+
{
17+
public:
18+
Desktop<N>(func show_, func cb[])
19+
: show(show_)
20+
, buttons_callbacks(cb)
21+
, next(0)
22+
{ }
23+
24+
/**
25+
@brief callback function when this Desktop is active
26+
*/
27+
func show;
28+
29+
/**
30+
@brief link to next Desktop in MultiDesktop configuration
31+
*/
32+
Desktop *next;
33+
34+
/**
35+
@brief callback functions for all N buttons. i function called when user press i button.
36+
*/
37+
func *buttons_callbacks;
38+
private:
39+
};
40+
41+
42+
/**
43+
@brief MultiDesktop class
44+
You can change Desktops for show different info and call different
45+
functions in each of them by pressing same buttons.
46+
47+
@param N - number of keys you want to use except 'next desktop' key.
48+
@param next_code - key code for go to next Desktop
49+
@param codes - N key codes you want to press
50+
*/
51+
template <int8_t N>
52+
class MultiDesktop
53+
{
54+
public:
55+
MultiDesktop<N>(int8_t next_code, int8_t codes[])
56+
: current(0)
57+
, first(0)
58+
, key_codes(codes)
59+
, next_desktop_code(next_code)
60+
{}
61+
62+
/**
63+
@brief add_desktop add new Desktop. Desktops are 1-direction looped list.
64+
@param new_d - link to new Desktop
65+
*/
66+
void add_desktop(Desktop<N> *new_d)
67+
{
68+
if (!first)
69+
{
70+
first = new_d;
71+
current = new_d;
72+
}
73+
else
74+
{
75+
Desktop<N> *d = first;
76+
while (d->next && d->next != first)
77+
d = d->next;
78+
79+
d->next = new_d;
80+
new_d->next = first;
81+
}
82+
}
83+
84+
/**
85+
@brief call this function when user press button. call show() to redraw screen.
86+
@param code - button code
87+
*/
88+
void button_pressed(int8_t code)
89+
{
90+
if (next_desktop_code == code)
91+
{
92+
if (current->next)
93+
current = current->next;
94+
}
95+
else
96+
{
97+
for (int8_t i=0; i < N; i++)
98+
{
99+
if (key_codes[i] == code)
100+
{
101+
if (current->buttons_callbacks[i])
102+
current->buttons_callbacks[i]();
103+
}
104+
}
105+
}
106+
}
107+
108+
/**
109+
@brief call show function of current desktop
110+
*/
111+
void show()
112+
{
113+
if (current->show)
114+
current->show();
115+
}
116+
117+
private:
118+
Desktop<N> *current;
119+
Desktop<N> *first;
120+
121+
int8_t next_desktop_code;
122+
int8_t *key_codes;
123+
};
124+
125+
#endif

0 commit comments

Comments
 (0)