Arduino Beispielprogramme
AVR Arduino Menü Template
Original: AVR Butterfly Menüsteuerung
Ein einfaches Menüsystem für das Arduino-LCD Modul; die Tasten steuern einen Spannungsteiler, der über einen analogen Eingang ausgewertet wird.
Zu Beachten: die Menütexte werden in menu.cpp implementiert und in menu.h importiert (Workaround für die Fehlermeldung "warning: only initialized variables can be placed into program memory area" wenn man einfacher char x[] PROGMEM = "abc"; schreibt).
Die Statemachine liegt zur Gänze im Flash.
1 1
Tasten: 4 3 0 reset ==> OK PREV NEXT
2 2
1
//example use of LCD4Bit_mod library
2
//-gdwarf-2
3
extern
"C"
void
__cxa_pure_virtual
(
void
);
//for C++ defines
4
5
void
__cxa_pure_virtual
(
void
) {};
6
#include
7
#include
8
9
#include
10
#include "LCD4Bit_mod.h"
11
#include "menu.h"
12
//create object to control an LCD.
13
//number of lines in display=1
14 LCD4Bit_mod lcd
=
LCD4Bit_mod
(
2
);
15
16
int
adc_key_val
[
5
] ={
30
,
150
,
360
,
535
,
760
};
23
int
NUM_KEYS
=
5
;
24
unsigned int
adc_key_in
;
25
int
key
=-
1
;
26
int
oldkey
=-
1
;
27
int
get_key
(
unsigned int
input
);
28
29
uint8_t
present_state
=
START
;
30
31
32
33
void
setup
() {
34 Serial
.
begin
(
9600
);
35
36 lcd
.
init
();
37 lcd
.
clear
();
40
42
uint8_t
present_state
=
START
;
43 stateAction
(
present_state
);
44
45
}
46
47
void
loop
() {
48 adc_key_in
=
analogRead
(
0
);
// read the value from the sensor
49 key
=
get_key
(
adc_key_in
);
// convert into key press
50
51
if
(
key
!=
oldkey
)
// if keypress is detected
52
{
53 delay
(
50
);
// wait for debounce time
54 adc_key_in
=
analogRead
(
0
);
// read the value from the sensor
55 key
=
get_key
(
adc_key_in
);
// convert into key press
56
if
(
key
!=
oldkey
)
57
{
58 oldkey
=
key
;
59
if
(
key
>=
0
){
60
61 present_state = nextstate(present_state, key);
62 stateAction(present_state);
63 }
64 }
65 }
66 }
67
68 // Convert ADC value to key number
69 int get_key(unsigned int input)
70 {
71 int k;
72
73 for (k = 0; k < NUM_KEYS; k++)
74 {
75 if (input < (unsigned int)adc_key_val[k])
76 {
77 return k;
79 }
80 }
81
82 if (k >= NUM_KEYS)
83 k = -1; // No valid key pressed
84
85 return k;
86 }
=========================== menu.cpp ===============================================
1 #include "menu.h"
2
#include
3
#include
4
#include "LCD4Bit_mod.h"
5
6
const char
startText
[] =
"Hauptmenu"
;
7
const char
z1Text
[] =
"Menue1"
;
8
const char
z2Text
[] =
"Menu2"
;
9
const char
z3Text
[] =
"Menu3"
;
10
const char
z4Text
[] =
"Startmeldung4"
;
11
const char
errorText
[] =
"Startmeldung5"
;
12
13
extern
LCD4Bit_mod lcd
;
14
15
uint8_t
nextstate
(
uint8_t
s
,
uint8_t
in
){
16
17
uint8_t
s1
;
18
for
(
s1
=
0
;
s1
<
sizeof
(
transitions
)/
sizeof
(
transitions
[
0
]);
s1
++){
19
if
((
pgm_read_byte
(&
transitions
[
s1
].
state
)==
s
)&&(
pgm_read_byte
(&
transitions
[
s1
].
input
)==
in
))
20
21
return
(
uint8_t
)
pgm_read_byte
(&
transitions
[
s1
].
nextState
);
22
}
23
return
s
;
//not found
24
}
25 STATE_INFO getStateInfo
(
uint8_t
s
){
//search for state info
26
uint8_t
s1
;
27 STATE_INFO si
;
28
for
(
s1
=
0
;
s1
<
sizeof
(
states
)/
sizeof
(
states
[
0
]);
s1
++){
29
if
((
pgm_read_byte
(&
states
[
s1
].
state
)==
s
)){
30
31 si
.
state
=
s
;
32 si
.
text
= (
PGM_P
)
pgm_read_word
(&
states
[
s1
].
text
);
33 si
.
p
= (
char
(*)(
char
))
pgm_read_word
(&
states
[
s1
].
p
);
34
return
si
;
35
}
36
37
}
38 si
.
state
=
ERROR
;
//nothing found
39 si
.
text
=
""
;
40 si
.
p
=
NULL
;
41
return
si
;
42
}
43
44
45
char
test1
(
char
s
){
46 lcd
.
printIn
(
"test1"
);
47
return
'c'
;
48
}
49
char
test2
(
char
s
){
50 lcd
.
printIn
(
"test2"
);
51
return
'h'
;
52
}
53
54
void
stateAction
(
uint8_t
state
){
55
char
buf
[
20
];
56 STATE_INFO si
=
getStateInfo
(
state
);
57 strcpy_P
(
buf
,
si
.
text
);
58 lcd
.
clear
();
59
if
(
si
.
text
!=
NULL
)
lcd
.
printIn
(
buf
);
60
if
(
si
.
p
!=
NULL
)
si
.
p
(
0
);
//call state action
61
//Serial.println(buf);
62
}
=========================== menu.cpp ===============================================
1
#ifndef MENU
2
#define MENU
3
#include
4
#define PS __attribute__((section (".progmem.data")))
5
6
#include "x.h"
7
#include
8
9
#define START 0
10
#define Z1 1
11
#define Z2 2
12
#define Z3 3
13
#define SubZ1 4
14
#define Z5 5
15
#define ERROR 99
16
17
#define OK 4
18
#define PREV 3
19
#define NEXT 0
20
#define PREV 3
21
22
23
typedef struct
PROGMEM
{
24
uint8_t
state
;
25
uint8_t
nextState
;
26
uint8_t
input
;
27
}
TRANSITION_INFO
;
28
29
typedef struct
PROGMEM
{
30
uint8_t
state
;
31 PGM_P text
;
32
char
(*
p
)(
char
) ;
33
}
STATE_INFO
;
34
35
uint8_t
nextstate
(
uint8_t
s
,
uint8_t
in
);
36
void
stateAction
(
uint8_t
state
);
37 STATE_INFO getStateInfo
(
uint8_t
s
);
38
39
extern
PROGMEM
const char
startText
[];
40
extern
PROGMEM
const char
z1Text
[];
41
extern
PROGMEM
const char
z2Text
[];
42
extern
PROGMEM
const char
z3Text
[];
43
extern
PROGMEM
const char
z4Text
[];
44
extern
PROGMEM
const char
errorText
[];
45
46
47
48
const
TRANSITION_INFO transitions
[]
PS
= {
49
{
START
,
Z1
,
NEXT
},
50
{
START
,
Z2
,
PREV
},
51
{
Z1
,
Z2
,
NEXT
},
52
{
Z1
,
START
,
PREV
},
53
{
Z1
,
SubZ1
,
OK
},
54
{
SubZ1
,
Z1
,
OK
},
55
{
SubZ1
,
SubZ1
,
NEXT
},
56 { SubZ1, SubZ1 ,PREV },
57 { Z2, START ,NEXT },
58 { Z2, Z1 ,PREV },
59 { ERROR, ERROR ,NEXT }
60 };
61 const STATE_INFO states [] PS = {
62 { START, startText, NULL},
63 { Z1, z1Text, NULL},
64 { Z2, z2Text, NULL},
65 { SubZ1, NULL, test2},
66 { ERROR, errorText, NULL}
67 };
68
69
70 #endif