first commit
This commit is contained in:
@@ -0,0 +1,546 @@
|
||||
// =====================================================================
|
||||
//
|
||||
// CONTROL
|
||||
//
|
||||
// =====================================================================
|
||||
// *********************************************************************
|
||||
// *********************************************************************
|
||||
// content:
|
||||
// (0) Control over serial interface
|
||||
// (1) Control over one analog input
|
||||
// (2) Control over 4 - 6 digital input pins (internal pullups enabled)
|
||||
// (3) Control over encoder (internal pullups enabled)
|
||||
// (4) Control with Keypad
|
||||
// (5) Control with an ir remote
|
||||
// (6) Control with a youstick
|
||||
// *********************************************************************
|
||||
|
||||
#define _LCDML_CONTROL_cfg 0
|
||||
|
||||
|
||||
// therory:
|
||||
// "#if" is a preprocessor directive and no error, look here:
|
||||
// (english) https://en.wikipedia.org/wiki/C_preprocessor
|
||||
// (german) https://de.wikipedia.org/wiki/C-Pr%C3%A4prozessor
|
||||
|
||||
|
||||
// *********************************************************************
|
||||
// CONTROL TASK, DO NOT CHANGE
|
||||
// *********************************************************************
|
||||
void LCDML_BACK_setup(LCDML_BACKEND_control)
|
||||
// *********************************************************************
|
||||
{
|
||||
// call setup
|
||||
LCDML_CONTROL_setup();
|
||||
}
|
||||
// backend loop
|
||||
boolean LCDML_BACK_loop(LCDML_BACKEND_control)
|
||||
{
|
||||
// call loop
|
||||
LCDML_CONTROL_loop();
|
||||
|
||||
// go to next backend function and do not block it
|
||||
return true;
|
||||
}
|
||||
// backend stop stable
|
||||
void LCDML_BACK_stable(LCDML_BACKEND_control)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// *********************************************************************
|
||||
// *************** (0) CONTROL OVER SERIAL INTERFACE *******************
|
||||
// *********************************************************************
|
||||
#if(_LCDML_CONTROL_cfg == 0)
|
||||
// settings
|
||||
# define _LCDML_CONTROL_serial_enter 'e'
|
||||
# define _LCDML_CONTROL_serial_up 'w'
|
||||
# define _LCDML_CONTROL_serial_down 's'
|
||||
# define _LCDML_CONTROL_serial_left 'a'
|
||||
# define _LCDML_CONTROL_serial_right 'd'
|
||||
# define _LCDML_CONTROL_serial_quit 'q'
|
||||
// *********************************************************************
|
||||
// setup
|
||||
void LCDML_CONTROL_setup()
|
||||
{
|
||||
}
|
||||
// *********************************************************************
|
||||
// loop
|
||||
void LCDML_CONTROL_loop()
|
||||
{
|
||||
// check if new serial input is available
|
||||
if (Serial.available()) {
|
||||
// read one char from input buffer
|
||||
switch (Serial.read())
|
||||
{
|
||||
case _LCDML_CONTROL_serial_enter: LCDML_BUTTON_enter(); break;
|
||||
case _LCDML_CONTROL_serial_up: LCDML_BUTTON_up(); break;
|
||||
case _LCDML_CONTROL_serial_down: LCDML_BUTTON_down(); break;
|
||||
case _LCDML_CONTROL_serial_left: LCDML_BUTTON_left(); break;
|
||||
case _LCDML_CONTROL_serial_right: LCDML_BUTTON_right(); break;
|
||||
case _LCDML_CONTROL_serial_quit: LCDML_BUTTON_quit(); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// *********************************************************************
|
||||
// ******************************* END *********************************
|
||||
// *********************************************************************
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// *********************************************************************
|
||||
// *************** (1) CONTROL OVER ONE ANALOG PIN *********************
|
||||
// *********************************************************************
|
||||
#elif(_LCDML_CONTROL_cfg == 1)
|
||||
// settings
|
||||
#define _LCDML_CONTROL_analog_pin 0
|
||||
// when you did not use a button set the value to zero
|
||||
#define _LCDML_CONTROL_analog_enter_min 850 // Button Enter
|
||||
#define _LCDML_CONTROL_analog_enter_max 920
|
||||
#define _LCDML_CONTROL_analog_up_min 520 // Button Up
|
||||
#define _LCDML_CONTROL_analog_up_max 590
|
||||
#define _LCDML_CONTROL_analog_down_min 700 // Button Down
|
||||
#define _LCDML_CONTROL_analog_down_max 770
|
||||
#define _LCDML_CONTROL_analog_back_min 950 // Button Back
|
||||
#define _LCDML_CONTROL_analog_back_max 1020
|
||||
#define _LCDML_CONTROL_analog_left_min 430 // Button Left
|
||||
#define _LCDML_CONTROL_analog_left_max 500
|
||||
#define _LCDML_CONTROL_analog_right_min 610 // Button Right
|
||||
#define _LCDML_CONTROL_analog_right_max 680
|
||||
// *********************************************************************
|
||||
// setup
|
||||
void LCDML_CONTROL_setup()
|
||||
{
|
||||
}
|
||||
// *********************************************************************
|
||||
// loop
|
||||
void LCDML_CONTROL_loop()
|
||||
{
|
||||
// check debounce timer
|
||||
if((millis() - g_LCDML_DISP_press_time) >= _LCDML_DISP_cfg_button_press_time) {
|
||||
g_LCDML_DISP_press_time = millis(); // reset debounce timer
|
||||
|
||||
uint16_t value = analogRead(_LCDML_CONTROL_analog_pin); // analogpin for keypad
|
||||
|
||||
if (value >= _LCDML_CONTROL_analog_enter_min && value <= _LCDML_CONTROL_analog_enter_max) { LCDML_BUTTON_enter(); }
|
||||
if (value >= _LCDML_CONTROL_analog_up_min && value <= _LCDML_CONTROL_analog_up_max) { LCDML_BUTTON_up(); }
|
||||
if (value >= _LCDML_CONTROL_analog_down_min && value <= _LCDML_CONTROL_analog_down_max) { LCDML_BUTTON_down(); }
|
||||
if (value >= _LCDML_CONTROL_analog_left_min && value <= _LCDML_CONTROL_analog_left_max) { LCDML_BUTTON_left(); }
|
||||
if (value >= _LCDML_CONTROL_analog_right_min && value <= _LCDML_CONTROL_analog_right_max) { LCDML_BUTTON_right(); }
|
||||
if (value >= _LCDML_CONTROL_analog_back_min && value <= _LCDML_CONTROL_analog_back_max) { LCDML_BUTTON_quit(); }
|
||||
}
|
||||
}
|
||||
// *********************************************************************
|
||||
// ******************************* END *********************************
|
||||
// *********************************************************************
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// *********************************************************************
|
||||
// *************** (2) CONTROL OVER DIGITAL PINS ***********************
|
||||
// *********************************************************************
|
||||
#elif(_LCDML_CONTROL_cfg == 2)
|
||||
// settings
|
||||
#define _LCDML_CONTROL_digital_low_active 0 // (1 = low active (pullup), 0 = high active (pulldown) button
|
||||
// http://playground.arduino.cc/CommonTopics/PullUpDownResistor
|
||||
#define _LCDML_CONTROL_digital_enable_quit 1
|
||||
#define _LCDML_CONTROL_digital_enable_lr 1
|
||||
#define _LCDML_CONTROL_digital_enter 8
|
||||
#define _LCDML_CONTROL_digital_up 9
|
||||
#define _LCDML_CONTROL_digital_down 10
|
||||
#define _LCDML_CONTROL_digital_quit 11
|
||||
#define _LCDML_CONTROL_digital_left 12
|
||||
#define _LCDML_CONTROL_digital_right 13
|
||||
// *********************************************************************
|
||||
// setup
|
||||
void LCDML_CONTROL_setup()
|
||||
{
|
||||
// init buttons
|
||||
pinMode(_LCDML_CONTROL_digital_enter , INPUT_PULLUP);
|
||||
pinMode(_LCDML_CONTROL_digital_up , INPUT_PULLUP);
|
||||
pinMode(_LCDML_CONTROL_digital_down , INPUT_PULLUP);
|
||||
# if(_LCDML_CONTROL_digital_enable_quit == 1)
|
||||
pinMode(_LCDML_CONTROL_digital_quit , INPUT_PULLUP);
|
||||
# endif
|
||||
# if(_LCDML_CONTROL_digital_enable_lr == 1)
|
||||
pinMode(_LCDML_CONTROL_digital_left , INPUT_PULLUP);
|
||||
pinMode(_LCDML_CONTROL_digital_right , INPUT_PULLUP);
|
||||
# endif
|
||||
}
|
||||
// *********************************************************************
|
||||
// loop
|
||||
void LCDML_CONTROL_loop()
|
||||
{
|
||||
#if(_LCDML_CONTROL_digital_low_active == 1)
|
||||
# define _LCDML_CONTROL_digital_a !
|
||||
#else
|
||||
# define _LCDML_CONTROL_digital_a
|
||||
#endif
|
||||
|
||||
uint8_t but_stat = 0x00;
|
||||
|
||||
bitWrite(but_stat, 0, _LCDML_CONTROL_digital_a(digitalRead(_LCDML_CONTROL_digital_enter)));
|
||||
bitWrite(but_stat, 1, _LCDML_CONTROL_digital_a(digitalRead(_LCDML_CONTROL_digital_up)));
|
||||
bitWrite(but_stat, 2, _LCDML_CONTROL_digital_a(digitalRead(_LCDML_CONTROL_digital_down)));
|
||||
#if(_LCDML_CONTROL_digital_enable_quit == 1)
|
||||
bitWrite(but_stat, 3, _LCDML_CONTROL_digital_a(digitalRead(_LCDML_CONTROL_digital_quit)));
|
||||
#endif
|
||||
#if(_LCDML_CONTROL_digital_enable_lr == 1)
|
||||
bitWrite(but_stat, 4, _LCDML_CONTROL_digital_a(digitalRead(_LCDML_CONTROL_digital_left)));
|
||||
bitWrite(but_stat, 5, _LCDML_CONTROL_digital_a(digitalRead(_LCDML_CONTROL_digital_right)));
|
||||
#endif
|
||||
|
||||
if (but_stat > 0) {
|
||||
if((millis() - g_LCDML_DISP_press_time) >= _LCDML_DISP_cfg_button_press_time) {
|
||||
g_LCDML_DISP_press_time = millis(); // reset press time
|
||||
|
||||
if (bitRead(but_stat, 0)) { LCDML_BUTTON_enter(); }
|
||||
if (bitRead(but_stat, 1)) { LCDML_BUTTON_up(); }
|
||||
if (bitRead(but_stat, 2)) { LCDML_BUTTON_down(); }
|
||||
if (bitRead(but_stat, 3)) { LCDML_BUTTON_quit(); }
|
||||
if (bitRead(but_stat, 4)) { LCDML_BUTTON_left(); }
|
||||
if (bitRead(but_stat, 5)) { LCDML_BUTTON_right(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
// *********************************************************************
|
||||
// ******************************* END *********************************
|
||||
// *********************************************************************
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// *********************************************************************
|
||||
// *************** (3) CONTROL WITH ENCODER ****************************
|
||||
// *********************************************************************
|
||||
#elif(_LCDML_CONTROL_cfg == 3)
|
||||
// settings
|
||||
#define _LCDML_CONTROL_encoder_pin_a 4 // pin encoder b
|
||||
#define _LCDML_CONTROL_encoder_pin_b 5 // pin encoder a
|
||||
#define _LCDML_CONTROL_encoder_pin_button 7 // pin taster
|
||||
#define _LCDML_CONTROL_encoder_high_active 0 // (0 = low active (pullup), 1 = high active (pulldown)) button
|
||||
// http://playground.arduino.cc/CommonTopics/PullUpDownResistor
|
||||
|
||||
#define _LCDML_CONTROL_encoder_refresh_time 5UL // 5ms
|
||||
#define _LCDML_CONTROL_encoder_switch_time 75UL // 75 ms
|
||||
|
||||
// macros which define the functionality
|
||||
#define _LCDML_CONTROL_encoder_switch_press_short() LCDML_BUTTON_enter()
|
||||
#define _LCDML_CONTROL_encoder_rotary_a() LCDML_BUTTON_up()
|
||||
#define _LCDML_CONTROL_encoder_rotary_b() LCDML_BUTTON_down()
|
||||
|
||||
#define _LCDML_CONTROL_encoder_advanced_switch 1
|
||||
#define _LCDML_CONTROL_encoder_switch_press_long() LCDML_BUTTON_quit()
|
||||
|
||||
#define _LCDML_CONTROL_encoder_advanced_rotary 1
|
||||
#define _LCDML_CONTROL_encoder_rotary_a_and_press() LCDML_BUTTON_left()
|
||||
#define _LCDML_CONTROL_encoder_rotary_b_and_press() LCDML_BUTTON_right()
|
||||
|
||||
#define _LCDML_CONTROL_encoder_t_long_press 1000 // maximum is 1275 (5*255)
|
||||
|
||||
// global defines
|
||||
uint8_t g_LCDML_CONTROL_encoder_t_prev = 0;
|
||||
uint8_t g_LCDML_CONTROL_encoder_a_prev = 0;
|
||||
uint8_t g_LCDML_CONTROL_t_pressed = 0;
|
||||
uint8_t g_LCDML_CONTROL_t_press_time = 0;
|
||||
|
||||
// *********************************************************************
|
||||
// setup
|
||||
void LCDML_CONTROL_setup()
|
||||
{
|
||||
// set encoder update intervall time
|
||||
LCDML_BACK_dynamic_setLoopTime(LCDML_BACKEND_control, _LCDML_CONTROL_encoder_refresh_time); // 5ms
|
||||
|
||||
// init pins
|
||||
if(_LCDML_CONTROL_encoder_high_active == 0)
|
||||
{
|
||||
pinMode(_LCDML_CONTROL_encoder_pin_a , INPUT_PULLUP);
|
||||
pinMode(_LCDML_CONTROL_encoder_pin_b , INPUT_PULLUP);
|
||||
pinMode(_LCDML_CONTROL_encoder_pin_button , INPUT_PULLUP);
|
||||
}
|
||||
}
|
||||
|
||||
// *********************************************************************
|
||||
// loop
|
||||
void LCDML_CONTROL_loop()
|
||||
{
|
||||
// read encoder status
|
||||
unsigned char a = digitalRead(_LCDML_CONTROL_encoder_pin_a);
|
||||
unsigned char b = digitalRead(_LCDML_CONTROL_encoder_pin_b);
|
||||
unsigned char t = digitalRead(_LCDML_CONTROL_encoder_pin_button);
|
||||
|
||||
// change button status if high and low active are switched
|
||||
if (_LCDML_CONTROL_encoder_high_active == 1) {
|
||||
t = !t;
|
||||
}
|
||||
|
||||
// check if the button was pressed and save this state
|
||||
if((millis() - g_LCDML_DISP_press_time) >= _LCDML_CONTROL_encoder_switch_time) {
|
||||
g_LCDML_DISP_press_time = millis(); // reset button press time
|
||||
|
||||
// press button once
|
||||
if (t == false && g_LCDML_CONTROL_encoder_t_prev == 0)
|
||||
{
|
||||
g_LCDML_CONTROL_t_pressed = 1;
|
||||
}
|
||||
else {
|
||||
g_LCDML_CONTROL_encoder_t_prev = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// check if button is currently pressed
|
||||
if(t == false)
|
||||
{
|
||||
// check if the advanced rotary function is enabled
|
||||
if(_LCDML_CONTROL_encoder_advanced_rotary == 1)
|
||||
{
|
||||
// check if the rotary encoder was moved
|
||||
if (a == false && g_LCDML_CONTROL_encoder_a_prev ) {
|
||||
g_LCDML_CONTROL_encoder_t_prev = 1;
|
||||
|
||||
if (b == false)
|
||||
{
|
||||
// switch active and rotary b moved
|
||||
_LCDML_CONTROL_encoder_rotary_b_and_press();
|
||||
}
|
||||
else
|
||||
{
|
||||
// switch active and rotary a moved
|
||||
_LCDML_CONTROL_encoder_rotary_a_and_press();
|
||||
}
|
||||
|
||||
g_LCDML_CONTROL_t_pressed = 0;
|
||||
g_LCDML_CONTROL_t_press_time = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// check advanced mode "long press switch"
|
||||
if(_LCDML_CONTROL_encoder_advanced_switch == 1)
|
||||
{
|
||||
// button was pressed
|
||||
if(g_LCDML_CONTROL_t_pressed == 1)
|
||||
{
|
||||
// check overrun and stop
|
||||
if(g_LCDML_CONTROL_t_press_time < 255)
|
||||
{
|
||||
g_LCDML_CONTROL_t_press_time++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// switch is not active
|
||||
|
||||
// check encoder
|
||||
if (a == false && g_LCDML_CONTROL_encoder_a_prev) {
|
||||
g_LCDML_CONTROL_encoder_t_prev = 1;
|
||||
|
||||
if (b == false)
|
||||
{
|
||||
_LCDML_CONTROL_encoder_rotary_a();
|
||||
}
|
||||
else
|
||||
{
|
||||
_LCDML_CONTROL_encoder_rotary_b();
|
||||
}
|
||||
|
||||
g_LCDML_CONTROL_t_pressed = 0;
|
||||
g_LCDML_CONTROL_t_press_time = 0;
|
||||
g_LCDML_DISP_press_time = millis();
|
||||
}
|
||||
|
||||
// check if an button was pressed
|
||||
if(g_LCDML_CONTROL_t_pressed == 1)
|
||||
{
|
||||
if(g_LCDML_CONTROL_t_press_time * _LCDML_CONTROL_encoder_refresh_time >= _LCDML_CONTROL_encoder_t_long_press && _LCDML_CONTROL_encoder_advanced_switch == 1)
|
||||
{
|
||||
_LCDML_CONTROL_encoder_switch_press_long();
|
||||
}
|
||||
else
|
||||
{
|
||||
_LCDML_CONTROL_encoder_switch_press_short();
|
||||
}
|
||||
|
||||
g_LCDML_CONTROL_t_pressed = 0;
|
||||
g_LCDML_CONTROL_t_press_time = 0;
|
||||
g_LCDML_DISP_press_time = millis();
|
||||
}
|
||||
}
|
||||
|
||||
g_LCDML_CONTROL_encoder_a_prev = a; // set new encoder status
|
||||
|
||||
}
|
||||
// *********************************************************************
|
||||
// ******************************* END *********************************
|
||||
// *********************************************************************
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// *********************************************************************
|
||||
// *************** (4) CONTROL WITH A KEYPAD ***************************
|
||||
// *********************************************************************
|
||||
#elif(_LCDML_CONTROL_cfg == 4)
|
||||
// include
|
||||
// more information under http://playground.arduino.cc/Main/KeypadTutorial
|
||||
#include <Keypad.h>
|
||||
// settings
|
||||
#define _LCDML_CONTROL_keypad_rows 4 // Four rows
|
||||
#define _LCDML_CONTROL_keypad_cols 3 // Three columns
|
||||
// global vars
|
||||
char keys[_LCDML_CONTROL_keypad_rows][_LCDML_CONTROL_keypad_cols] = {
|
||||
{'1','2','3'},
|
||||
{'4','5','6'},
|
||||
{'7','8','9'},
|
||||
{'#','0','*'}
|
||||
};
|
||||
byte rowPins[_LCDML_CONTROL_keypad_rows] = { 9, 8, 7, 6 }; // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
|
||||
byte colPins[_LCDML_CONTROL_keypad_cols] = { 12, 11, 10 }; // Create the Keypad
|
||||
// objects
|
||||
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, _LCDML_CONTROL_keypad_rows, _LCDML_CONTROL_keypad_cols );
|
||||
// *********************************************************************
|
||||
// setup
|
||||
void LCDML_CONTROL_setup()
|
||||
{
|
||||
}
|
||||
// *********************************************************************
|
||||
// loop
|
||||
void LCDML_CONTROL_loop()
|
||||
{
|
||||
char key = kpd.getKey();
|
||||
if(key) // Check for a valid key.
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
case '#': LCDML_BUTTON_enter(); break;
|
||||
case '2': LCDML_BUTTON_up(); break;
|
||||
case '8': LCDML_BUTTON_down(); break;
|
||||
case '4': LCDML_BUTTON_left(); break;
|
||||
case '6': LCDML_BUTTON_right(); break;
|
||||
case '*': LCDML_BUTTON_quit(); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// *********************************************************************
|
||||
// ******************************* END *********************************
|
||||
// *********************************************************************
|
||||
|
||||
|
||||
// *********************************************************************
|
||||
// *************** (5) CONTROL WITH IR REMOTE ***************************
|
||||
// *********************************************************************
|
||||
#elif(_LCDML_CONTROL_cfg == 5)
|
||||
// ir include (this lib have to be installed)
|
||||
#include <IRremote.h>
|
||||
// ir global vars
|
||||
int RECV_PIN = 11;
|
||||
// ir objects
|
||||
IRrecv irrecv(RECV_PIN);
|
||||
decode_results results;
|
||||
|
||||
// *********************************************************************
|
||||
// setup (nothing change here)
|
||||
void LCDML_CONTROL_setup()
|
||||
{
|
||||
irrecv.enableIRIn(); // Start the receiver
|
||||
}
|
||||
// *********************************************************************
|
||||
// loop
|
||||
// change in this function the ir values to your values
|
||||
void LCDML_CONTROL_loop()
|
||||
{
|
||||
if (irrecv.decode(&results))
|
||||
{
|
||||
// comment this line out, to check the correct code
|
||||
//Serial.println(results.value, HEX);
|
||||
|
||||
// in this switch case you have to change the value 0x...1 to the correct ir code
|
||||
switch (results.value)
|
||||
{
|
||||
case 0x00000001: LCDML_BUTTON_enter(); break;
|
||||
case 0x00000002: LCDML_BUTTON_up(); break;
|
||||
case 0x00000003: LCDML_BUTTON_down(); break;
|
||||
case 0x00000004: LCDML_BUTTON_left(); break;
|
||||
case 0x00000005: LCDML_BUTTON_right(); break;
|
||||
case 0x00000006: LCDML_BUTTON_quit(); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// *********************************************************************
|
||||
// ******************************* END *********************************
|
||||
// *********************************************************************
|
||||
|
||||
|
||||
|
||||
// *********************************************************************
|
||||
// *************** (6) CONTROL OVER JOYSTICK *********************
|
||||
// *********************************************************************
|
||||
#elif(_LCDML_CONTROL_cfg == 6)
|
||||
// settings
|
||||
#define _LCDML_CONTROL_analog_pinx A0
|
||||
#define _LCDML_CONTROL_analog_piny A1
|
||||
#define _LCDML_CONTROL_digitalread 33 //don't work with u8glib
|
||||
|
||||
// when you did not use a button set the value to zero
|
||||
#define _LCDML_CONTROL_analog_up_min 612 // Button Up
|
||||
#define _LCDML_CONTROL_analog_up_max 1023
|
||||
#define _LCDML_CONTROL_analog_down_min 0 // Button Down
|
||||
#define _LCDML_CONTROL_analog_down_max 412
|
||||
#define _LCDML_CONTROL_analog_left_min 612 // Button Left
|
||||
#define _LCDML_CONTROL_analog_left_max 1023
|
||||
#define _LCDML_CONTROL_analog_right_min 0 // Button Right
|
||||
#define _LCDML_CONTROL_analog_right_max 412
|
||||
// *********************************************************************
|
||||
// setup
|
||||
void LCDML_CONTROL_setup()
|
||||
{
|
||||
pinMode (_LCDML_CONTROL_digitalread, INPUT);
|
||||
}
|
||||
// *********************************************************************
|
||||
// loop
|
||||
void LCDML_CONTROL_loop()
|
||||
{
|
||||
// check debounce timer
|
||||
if((millis() - g_LCDML_DISP_press_time) >= _LCDML_DISP_cfg_button_press_time) {
|
||||
g_LCDML_DISP_press_time = millis(); // reset debounce timer
|
||||
|
||||
uint16_t valuex = analogRead(_LCDML_CONTROL_analog_pinx); // analogpinx
|
||||
uint16_t valuey = analogRead(_LCDML_CONTROL_analog_piny); // analogpinx
|
||||
uint16_t valuee = digitalRead(_LCDML_CONTROL_digitalread); //digitalpinenter
|
||||
|
||||
|
||||
if (valuey >= _LCDML_CONTROL_analog_up_min && valuey <= _LCDML_CONTROL_analog_up_max) { LCDML_BUTTON_up(); } // up
|
||||
if (valuey >= _LCDML_CONTROL_analog_down_min && valuey <= _LCDML_CONTROL_analog_down_max) { LCDML_BUTTON_down(); } // down
|
||||
if (valuex >= _LCDML_CONTROL_analog_left_min && valuex <= _LCDML_CONTROL_analog_left_max) { LCDML_BUTTON_left(); } // left
|
||||
if (valuex >= _LCDML_CONTROL_analog_right_min && valuex <= _LCDML_CONTROL_analog_right_max) { LCDML_BUTTON_right(); } // right
|
||||
|
||||
if(valuee == true) {LCDML_BUTTON_enter();} // enter
|
||||
|
||||
// back buttons have to be included as menuitem
|
||||
// lock at the examle LCDML_back_button
|
||||
|
||||
}
|
||||
}
|
||||
// *********************************************************************
|
||||
// ******************************* END *********************************
|
||||
// *********************************************************************
|
||||
|
||||
|
||||
#else
|
||||
#error _LCDML_CONTROL_cfg is not defined or not in range
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
// =====================================================================
|
||||
//
|
||||
// Output function
|
||||
//
|
||||
// =====================================================================
|
||||
|
||||
/* ******************************************************************** */
|
||||
void LCDML_lcd_menu_display()
|
||||
/* ******************************************************************** */
|
||||
{
|
||||
// check if menu needs an update
|
||||
if (LCDML_DISP_update()) {
|
||||
|
||||
// init vars
|
||||
uint8_t n_max = (LCDML.getChilds() >= _LCDML_DISP_rows) ? _LCDML_DISP_rows : (LCDML.getChilds());
|
||||
uint8_t scrollbar_min = 0;
|
||||
uint8_t scrollbar_max = LCDML.getChilds();
|
||||
uint8_t scrollbar_cur_pos = LCDML.getCursorPosAbs();
|
||||
uint8_t scroll_pos = ((1.*n_max * _LCDML_DISP_rows) / (scrollbar_max - 1) * scrollbar_cur_pos);
|
||||
|
||||
|
||||
// update content
|
||||
if (LCDML_DISP_update_content()) {
|
||||
// clear menu
|
||||
LCDML_lcd_menu_clear();
|
||||
|
||||
// display rows
|
||||
for (uint8_t n = 0; n < n_max; n++)
|
||||
{
|
||||
// set cursor
|
||||
lcd.setCursor(1, n);
|
||||
// set content
|
||||
// with content id you can add special content to your static menu or replace the content
|
||||
// the content_id contains the id wich is set on main tab for a menuitem
|
||||
switch(LCDML.content_id[n])
|
||||
{
|
||||
//case 0:
|
||||
// lcd.print("special"); // or datetime or other things
|
||||
// break;
|
||||
|
||||
default: // static content
|
||||
lcd.print(LCDML.content[n]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// update cursor and scrollbar
|
||||
if (LCDML_DISP_update_cursor()) {
|
||||
|
||||
// display rows
|
||||
for (uint8_t n = 0; n < n_max; n++)
|
||||
{
|
||||
//set cursor
|
||||
lcd.setCursor(0, n);
|
||||
|
||||
//set cursor char
|
||||
if (n == LCDML.getCursorPos()) {
|
||||
lcd.write(_LCDML_DISP_cfg_cursor);
|
||||
} else {
|
||||
lcd.write(' ');
|
||||
}
|
||||
|
||||
// delete or reset scrollbar
|
||||
if (_LCDML_DISP_cfg_scrollbar == 1) {
|
||||
if (scrollbar_max > n_max) {
|
||||
lcd.setCursor((_LCDML_DISP_cols - 1), n);
|
||||
lcd.write((uint8_t)0);
|
||||
}
|
||||
else {
|
||||
lcd.setCursor((_LCDML_DISP_cols - 1), n);
|
||||
lcd.print(' ');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// display scrollbar
|
||||
if (_LCDML_DISP_cfg_scrollbar == 1) {
|
||||
if (scrollbar_max > n_max) {
|
||||
//set scroll position
|
||||
if (scrollbar_cur_pos == scrollbar_min) {
|
||||
// min pos
|
||||
lcd.setCursor((_LCDML_DISP_cols - 1), 0);
|
||||
lcd.write((uint8_t)1);
|
||||
} else if (scrollbar_cur_pos == (scrollbar_max - 1)) {
|
||||
// max pos
|
||||
lcd.setCursor((_LCDML_DISP_cols - 1), (n_max - 1));
|
||||
lcd.write((uint8_t)4);
|
||||
} else {
|
||||
// between
|
||||
lcd.setCursor((_LCDML_DISP_cols - 1), scroll_pos / n_max);
|
||||
lcd.write((uint8_t)(scroll_pos % n_max) + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// reinit some vars
|
||||
LCDML_DISP_update_end();
|
||||
}
|
||||
|
||||
|
||||
// lcd clear
|
||||
void LCDML_lcd_menu_clear()
|
||||
{
|
||||
lcd.clear();
|
||||
lcd.setCursor(0, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
/* ===================================================================== *
|
||||
* *
|
||||
* BACKEND SYSTEM *
|
||||
* *
|
||||
* ===================================================================== *
|
||||
* every "backend function" needs three functions
|
||||
* - void LCDML_BACK_setup(..func_name..)
|
||||
* - void LCDML_BACK_loop(..func_name..)
|
||||
* - void LCDML_BACK_stable(..func_name..)
|
||||
*
|
||||
* - every BACKEND function can be stopped and started
|
||||
* EXAMPLE CODE:
|
||||
void LCDML_BACK_setup(LCDML_BACKEND_control)
|
||||
{
|
||||
// setup
|
||||
// is called only if it is started or restartet (reset+start)
|
||||
}
|
||||
|
||||
boolean LCDML_BACK_loop(LCDML_BACKEND_control)
|
||||
{
|
||||
// runs in loop
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void LCDML_BACK_stable(LCDML_BACKEND_control)
|
||||
{
|
||||
// stable stop
|
||||
// is called when a backend function is stopped with stopStable
|
||||
}
|
||||
* ===================================================================== *
|
||||
*/
|
||||
@@ -0,0 +1,221 @@
|
||||
/* ===================================================================== *
|
||||
* *
|
||||
* DISPLAY SYSTEM *
|
||||
* *
|
||||
* ===================================================================== *
|
||||
* every "disp menu function" needs three functions
|
||||
* - void LCDML_DISP_setup(func_name)
|
||||
* - void LCDML_DISP_loop(func_name)
|
||||
* - void LCDML_DISP_loop_end(func_name)
|
||||
*
|
||||
* EXAMPLE CODE:
|
||||
void LCDML_DISP_setup(..menu_func_name..)
|
||||
{
|
||||
// setup
|
||||
// is called only if it is started
|
||||
|
||||
// starts a trigger event for the loop function every 100 millisecounds
|
||||
LCDML_DISP_triggerMenu(100);
|
||||
}
|
||||
|
||||
void LCDML_DISP_loop(..menu_func_name..)
|
||||
{
|
||||
// loop
|
||||
// is called when it is triggert
|
||||
// - with LCDML_DISP_triggerMenu( millisecounds )
|
||||
// - with every button status change
|
||||
|
||||
// check if any button is presed (enter, up, down, left, right)
|
||||
if(LCDML_BUTTON_checkAny()) {
|
||||
LCDML_DISP_funcend();
|
||||
}
|
||||
}
|
||||
|
||||
void LCDML_DISP_loop_end(..menu_func_name..)
|
||||
{
|
||||
// loop end
|
||||
// this functions is ever called when a DISP function is quit
|
||||
// you can here reset some global vars or do nothing
|
||||
}
|
||||
* ===================================================================== *
|
||||
*/
|
||||
|
||||
|
||||
// this needs some bytes in ram
|
||||
#define MAX_FILES_IN_LIST 20
|
||||
|
||||
// scroll row position
|
||||
uint8_t scroll_row;
|
||||
// cursor real position on lcd lines (0 - _LCDML_DISP_rows - 1)
|
||||
uint8_t cursor_real_pos;
|
||||
// max filelist count
|
||||
uint8_t current_max_list_count;
|
||||
// cursor position to file list entry
|
||||
uint8_t cursor_position_cur;
|
||||
// filelist
|
||||
static char filelist[MAX_FILES_IN_LIST][20];
|
||||
|
||||
|
||||
// *********************************************************************
|
||||
void LCDML_DISP_setup(LCDML_FUNC_dyn_filelist)
|
||||
// *********************************************************************
|
||||
{
|
||||
// use this to init some vars
|
||||
|
||||
// set max file counter
|
||||
current_max_list_count = 0;
|
||||
// set current cursor position
|
||||
cursor_position_cur = 0;
|
||||
//
|
||||
scroll_row = 0;
|
||||
cursor_real_pos = 0;
|
||||
|
||||
|
||||
// for example read sd card files here
|
||||
|
||||
// for example here only with a dummy list
|
||||
// with 12 dummyfiles
|
||||
// subfolders are not supported
|
||||
|
||||
// generate file names
|
||||
for(uint8_t i = 0; i<13; i++) {
|
||||
if(i < MAX_FILES_IN_LIST) {
|
||||
// copy dummy string to new position
|
||||
memcpy(filelist[i], "File .txt", 11);
|
||||
|
||||
if(i<10) { // filenames < 10
|
||||
filelist[i][5] = 0+48; // number + '0' '0' = 48
|
||||
filelist[i][6] = i+48; // number
|
||||
} else { // filenames >= 10
|
||||
filelist[i][5] = i/10+48;
|
||||
filelist[i][6] = i%10+48;
|
||||
}
|
||||
current_max_list_count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LCDML_DISP_loop(LCDML_FUNC_dyn_filelist)
|
||||
{
|
||||
// loop function, can be run in a loop when LCDML_DISP_triggerMenu(xx) is set
|
||||
// the quit button works in every DISP function without any checks; it starts the loop_end function
|
||||
|
||||
// init some vars for scrollbar
|
||||
uint8_t n_max = (current_max_list_count >= _LCDML_DISP_rows) ? _LCDML_DISP_rows : (current_max_list_count);;
|
||||
uint8_t scrollbar_min = 0;
|
||||
uint8_t scrollbar_max = current_max_list_count;
|
||||
//uint8_t scrollbar_cur_pos = cursor_position_cur;
|
||||
uint8_t scroll_pos = ((1.*n_max * _LCDML_DISP_rows) / (scrollbar_max - 1) * cursor_position_cur);
|
||||
|
||||
|
||||
// clear display
|
||||
// ================
|
||||
lcd.clear();
|
||||
|
||||
// display content
|
||||
// ==================
|
||||
for (uint8_t n = scroll_row; n < (scroll_row+_LCDML_DISP_rows); n++)
|
||||
{
|
||||
// set cursor
|
||||
lcd.setCursor(1, n-scroll_row);
|
||||
// set content
|
||||
lcd.print(filelist[n]);
|
||||
}
|
||||
|
||||
// set cursor and scrollbar
|
||||
// =============================
|
||||
for (uint8_t n = scroll_row; n <(scroll_row+_LCDML_DISP_rows); n++)
|
||||
{
|
||||
lcd.setCursor(0, n-scroll_row);
|
||||
|
||||
// set cursor
|
||||
// =====================
|
||||
if (n == cursor_position_cur) {
|
||||
lcd.write(_LCDML_DISP_cfg_cursor);
|
||||
cursor_real_pos = n-scroll_row;
|
||||
} else {
|
||||
lcd.write(' ');
|
||||
}
|
||||
|
||||
// display scrollbar
|
||||
// ==============================
|
||||
// delete or reset scrollbar
|
||||
if (scrollbar_max > n_max) {
|
||||
lcd.setCursor((_LCDML_DISP_cols - 1), n-scroll_row);
|
||||
lcd.write((uint8_t)0);
|
||||
}
|
||||
else {
|
||||
lcd.setCursor((_LCDML_DISP_cols - 1), n-scroll_row);
|
||||
lcd.print(' ');
|
||||
}
|
||||
|
||||
// set scrollbar
|
||||
if (scrollbar_max > n_max) {
|
||||
//set scroll position
|
||||
if (cursor_position_cur == scrollbar_min) {
|
||||
// min pos
|
||||
lcd.setCursor((_LCDML_DISP_cols - 1), 0);
|
||||
lcd.write((uint8_t)1);
|
||||
} else if (cursor_position_cur == (scrollbar_max - 1)) {
|
||||
// max pos
|
||||
lcd.setCursor((_LCDML_DISP_cols - 1), (n_max - 1));
|
||||
lcd.write((uint8_t)4);
|
||||
} else {
|
||||
// between
|
||||
lcd.setCursor((_LCDML_DISP_cols - 1), scroll_pos / n_max);
|
||||
lcd.write((uint8_t)(scroll_pos % n_max) + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// control
|
||||
// =====================================
|
||||
|
||||
// move up in list
|
||||
if(LCDML_BUTTON_checkUp()) {
|
||||
LCDML_BUTTON_resetAll();
|
||||
// scroll up
|
||||
if(cursor_position_cur > 0) { // check minimum cursor
|
||||
cursor_position_cur--; // scroll up
|
||||
if(cursor_real_pos == 0) {
|
||||
scroll_row--; // scroll display rows
|
||||
}
|
||||
}
|
||||
// update content above or remove this line and copy button checks on top of this function
|
||||
LCDML_BACK_start(LCDML_BACKEND_menu);
|
||||
}
|
||||
|
||||
// move down in list
|
||||
if(LCDML_BUTTON_checkDown()) {
|
||||
LCDML_BUTTON_resetAll();
|
||||
// scroll down
|
||||
if(cursor_position_cur < (current_max_list_count-1)) { // check list end
|
||||
cursor_position_cur++; // go to next element
|
||||
if(cursor_real_pos == (_LCDML_DISP_rows-1)) { // check if current cursor is in last display line
|
||||
scroll_row++; // scroll content
|
||||
}
|
||||
}
|
||||
// update content above or remove this line and copy button checks on top of this function
|
||||
LCDML_BACK_start(LCDML_BACKEND_menu);
|
||||
}
|
||||
|
||||
// enter file name
|
||||
if(LCDML_BUTTON_checkEnter()) {
|
||||
LCDML_BUTTON_resetAll();
|
||||
// save selected file (serial.print)
|
||||
LCDML_DISP_funcend();
|
||||
// output of current line
|
||||
Serial.println(filelist[cursor_position_cur]);
|
||||
}
|
||||
}
|
||||
|
||||
void LCDML_DISP_loop_end(LCDML_FUNC_dyn_filelist)
|
||||
{
|
||||
// this functions is ever called when a DISP function is quit
|
||||
// you can here reset some global vars or do nothing
|
||||
|
||||
// Display selected file name
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
// ============================================================
|
||||
// Example: LCDML: dynamic menu list
|
||||
// ============================================================
|
||||
// Autor: Nils Feldkämper
|
||||
// Last update: 08.01.2017
|
||||
// License: MIT
|
||||
// ============================================================
|
||||
// Descripton:
|
||||
// This example shows you, how to generate a dynamic menu list
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
// include libs
|
||||
#include <LiquidCrystal.h>
|
||||
#include <LCDMenuLib.h>
|
||||
|
||||
// lib config
|
||||
#define _LCDML_DISP_cfg_button_press_time 200 // button press time in ms
|
||||
#define _LCDML_DISP_cfg_scrollbar 1 // enable a scrollbar
|
||||
#define _LCDML_DISP_cfg_cursor 0x7E // cursor Symbol
|
||||
|
||||
// *********************************************************************
|
||||
// LCDML TYPE SELECT
|
||||
// *********************************************************************
|
||||
// settings for lcd
|
||||
#define _LCDML_DISP_cols 20
|
||||
#define _LCDML_DISP_rows 4
|
||||
|
||||
// lcd object
|
||||
// liquid crystal needs (rs, e, dat4, dat5, dat6, dat7)
|
||||
LiquidCrystal lcd(4,5,6,7,8,9);
|
||||
|
||||
const uint8_t scroll_bar[5][8] = {
|
||||
{B10001, B10001, B10001, B10001, B10001, B10001, B10001, B10001}, // scrollbar top
|
||||
{B11111, B11111, B10001, B10001, B10001, B10001, B10001, B10001}, // scroll state 1
|
||||
{B10001, B10001, B11111, B11111, B10001, B10001, B10001, B10001}, // scroll state 2
|
||||
{B10001, B10001, B10001, B10001, B11111, B11111, B10001, B10001}, // scroll state 3
|
||||
{B10001, B10001, B10001, B10001, B10001, B10001, B11111, B11111} // scrollbar bottom
|
||||
};
|
||||
|
||||
// *********************************************************************
|
||||
// LCDML MENU/DISP
|
||||
// *********************************************************************
|
||||
// create menu
|
||||
// menu element count - last element id
|
||||
// this value must be the same as the last menu element
|
||||
#define _LCDML_DISP_cnt 0
|
||||
|
||||
// LCDML_root => layer 0
|
||||
// LCDML_root_X => layer 1
|
||||
// LCDML_root_X_X => layer 2
|
||||
// LCDML_root_X_X_X => layer 3
|
||||
// LCDML_root_... => layer ...
|
||||
|
||||
// LCDMenuLib_add(id, group, prev_layer_element, new_element_num, lang_char_array, callback_function)
|
||||
LCDML_DISP_init(_LCDML_DISP_cnt);
|
||||
LCDML_DISP_add (0 , _LCDML_G1 , LCDML_root , 1 , "Dyn Filelist" , LCDML_FUNC_dyn_filelist);
|
||||
LCDML_DISP_createMenu(_LCDML_DISP_cnt);
|
||||
|
||||
|
||||
|
||||
// *********************************************************************
|
||||
// LCDML BACKEND (core of the menu, do not change here anything yet)
|
||||
// *********************************************************************
|
||||
// define backend function
|
||||
#define _LCDML_BACK_cnt 1 // last backend function id
|
||||
|
||||
LCDML_BACK_init(_LCDML_BACK_cnt);
|
||||
LCDML_BACK_new_timebased_dynamic (0 , ( 20UL ) , _LCDML_start , LCDML_BACKEND_control);
|
||||
LCDML_BACK_new_timebased_dynamic (1 , ( 100000000UL ) , _LCDML_stop , LCDML_BACKEND_menu);
|
||||
LCDML_BACK_create();
|
||||
|
||||
|
||||
// *********************************************************************
|
||||
// SETUP
|
||||
// *********************************************************************
|
||||
void setup()
|
||||
{
|
||||
// serial init; only be needed if serial control is used
|
||||
Serial.begin(9600); // start serial
|
||||
Serial.println(F(_LCDML_VERSION)); // only for examples
|
||||
|
||||
// LCD Begin
|
||||
lcd.begin(_LCDML_DISP_cols,_LCDML_DISP_rows);
|
||||
// set special chars for scrollbar
|
||||
lcd.createChar(0, (uint8_t*)scroll_bar[0]);
|
||||
lcd.createChar(1, (uint8_t*)scroll_bar[1]);
|
||||
lcd.createChar(2, (uint8_t*)scroll_bar[2]);
|
||||
lcd.createChar(3, (uint8_t*)scroll_bar[3]);
|
||||
lcd.createChar(4, (uint8_t*)scroll_bar[4]);
|
||||
|
||||
// Enable all items with _LCDML_G1
|
||||
LCDML_DISP_groupEnable(_LCDML_G1); // enable group 1
|
||||
|
||||
// Enable menu rollover if needed
|
||||
//LCDML.enRollover();
|
||||
|
||||
// LCDMenu Setup
|
||||
LCDML_setup(_LCDML_BACK_cnt);
|
||||
}
|
||||
|
||||
// *********************************************************************
|
||||
// LOOP
|
||||
// *********************************************************************
|
||||
void loop()
|
||||
{
|
||||
// this function must called here, do not delete it
|
||||
LCDML_run(_LCDML_priority);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// *********************************************************************
|
||||
// check some errors - do not change here anything
|
||||
// *********************************************************************
|
||||
# if(_LCDML_DISP_rows > _LCDML_DISP_cfg_max_rows)
|
||||
# error change value of _LCDML_DISP_cfg_max_rows in LCDMenuLib.h
|
||||
# endif
|
||||
# if(_LCDML_DISP_cols > _LCDML_DISP_cfg_max_string_length)
|
||||
# error change value of _LCDML_DISP_cfg_max_string_length in LCDMenuLib.h
|
||||
# endif
|
||||
Reference in New Issue
Block a user