u8g2-copy/sys/arduino/u8g2_full_buffer/SwitchBox/SwitchBox.ino

374 lines
8.3 KiB
Arduino
Raw Normal View History

2018-05-06 02:23:50 +08:00
/*
HelloWorld.ino
Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
Copyright (c) 2016, olikraus@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <Arduino.h>
#include <U8g2lib.h>
uint8_t light_to_pin[4] = { 11, 10, 9, 8 };
uint8_t switch_to_pin[4] = { 7, 6, 5, 4 };
#define SWITCH_ON 0
2018-06-07 04:57:07 +08:00
#define SWITCH_OFF 1
2018-05-06 02:23:50 +08:00
#define LIGHT_ON 1
#define LIGHT_OFF 0
#define TIMEOUT 3000
uint8_t switch_status[4];
#define DEBOUNCE_CNT 3
uint8_t switch_debounce[4];
uint8_t is_switch_changed;
2018-05-06 16:56:49 +08:00
uint8_t last_switch_on_cnt; // number of "on" switches before the last change
uint8_t switch_on_cnt; // current number of "on" switches
uint8_t max_on_cnt; // last number max number of "on" switches
2018-05-06 16:29:26 +08:00
2018-05-06 16:56:49 +08:00
// max_on_cnt==1: max_on_cnt_pos1 --> on switch
// max_on_cnt==2: max_on_cnt_pos1/2 --> on switch
// max_on_cnt==3: max_on_cnt_pos1 --> off switch
2018-05-06 19:27:56 +08:00
// max_on_cnt==4: max_on_cnt_pos1 --> last switch which was turned off
2018-05-06 16:29:26 +08:00
int8_t max_on_cnt_pos1 = -1;
int8_t max_on_cnt_pos2 = -1;
2018-05-06 02:23:50 +08:00
2018-05-08 02:20:22 +08:00
int8_t user_pos1;
int8_t user_pos2;
2018-05-07 03:47:41 +08:00
int8_t max_on_cnt_npos1 = -1;
int8_t max_on_cnt_npos2 = -1;
2018-05-06 02:23:50 +08:00
uint32_t switch_changed_millis;
2018-05-06 16:56:49 +08:00
uint8_t map_switch_to_light[4];
2018-05-06 02:23:50 +08:00
#define STATE_WAIT 0
2018-05-07 00:31:20 +08:00
#define STATE_DELAYED_SWAP_WAIT 1
2018-05-06 02:23:50 +08:00
uint8_t state = STATE_WAIT;
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R3, /* reset=*/ U8X8_PIN_NONE);
U8G2LOG u8g2log;
uint8_t u8log_buffer[6*14];
void init_switch(void)
{
uint8_t i;
for( i = 0; i < 4; i++ )
{
pinMode(switch_to_pin[i], INPUT_PULLUP);
switch_debounce[i] = 0;
switch_status[i] = 2;
2018-05-06 16:56:49 +08:00
map_switch_to_light[i] = i;
2018-05-06 02:23:50 +08:00
}
}
void read_switch_status(void)
{
2018-05-06 16:29:26 +08:00
uint8_t i, v, on_cnt;
int8_t pos1, pos2, npos1;
2018-05-06 02:23:50 +08:00
for( i = 0; i < 4; i++ )
{
v = digitalRead(switch_to_pin[i]);
if ( switch_debounce[i] > 0 )
{
if ( switch_status[i] == v )
{
switch_debounce[i] = 0; // do nothing, wait for other values
}
else
{
switch_debounce[i]--;
if ( switch_debounce[i] == 0 )
{
switch_status[i] = v; // new value detected
is_switch_changed = 1;
switch_changed_millis = millis();
2018-05-06 22:56:43 +08:00
break; // break out of the loop, so that we detect only one change at a time
2018-05-06 02:23:50 +08:00
}
}
}
else if ( switch_status[i] != v )
{
switch_debounce[i] = DEBOUNCE_CNT; // start debounce
}
}
2018-05-06 16:29:26 +08:00
on_cnt = 0;
pos1 = -1;
2018-05-06 19:27:56 +08:00
pos2 = -1;
2018-05-06 16:29:26 +08:00
for( i = 0; i < 4; i++ )
{
if ( switch_status[i] == SWITCH_ON )
{
on_cnt++;
if ( pos1 < 0 )
pos1 = i;
else
pos2 = i;
}
else
{
npos1 = i;
}
}
if ( switch_on_cnt != on_cnt )
{
last_switch_on_cnt = switch_on_cnt;
switch_on_cnt = on_cnt;
if ( last_switch_on_cnt < switch_on_cnt )
{
max_on_cnt = on_cnt;
if ( max_on_cnt == 1 )
{
max_on_cnt_pos1 = pos1;
max_on_cnt_pos2 = -1;
}
else if ( max_on_cnt == 2 )
{
max_on_cnt_pos1 = pos1;
max_on_cnt_pos2 = pos2;
}
else if ( max_on_cnt == 3 )
{
max_on_cnt_pos1 = npos1;
max_on_cnt_pos2 = -1;
}
2018-05-06 19:27:56 +08:00
else if ( max_on_cnt == 4 )
{
max_on_cnt_pos1 = -1;
max_on_cnt_pos2 = -1;
}
2018-05-06 16:29:26 +08:00
else
{
max_on_cnt_pos1 = -1;
max_on_cnt_pos2 = -1;
}
}
2018-05-06 19:27:56 +08:00
else if ( last_switch_on_cnt > switch_on_cnt )
{
if ( max_on_cnt == 4 )
{
if ( switch_on_cnt == 1 )
{
max_on_cnt_pos1 = pos1;
max_on_cnt_pos2 = -1;
}
}
}
2018-05-06 16:29:26 +08:00
}
2018-05-06 02:23:50 +08:00
}
void print_switch_status(void)
{
uint8_t i;
2018-05-06 16:29:26 +08:00
u8g2log.print(last_switch_on_cnt);
u8g2log.print(">");
u8g2log.print(switch_on_cnt);
u8g2log.print("/");
u8g2log.print(max_on_cnt);
u8g2log.print("\n");
u8g2log.print(max_on_cnt_pos1);
u8g2log.print("/");
u8g2log.print(max_on_cnt_pos2);
u8g2log.print("\n");
2018-05-06 02:23:50 +08:00
for( i = 0; i < 4; i++ )
u8g2log.print(switch_status[i] == 0 ? "*" : ".");
u8g2log.print("\n");
}
void init_light(void)
{
uint8_t i;
for( i = 0; i < 4; i++ )
{
digitalWrite(light_to_pin[i], 1);
pinMode(light_to_pin[i], OUTPUT);
digitalWrite(light_to_pin[i], 1);
}
}
void write_switch_to_light(void)
{
uint8_t i;
for( i = 0; i < 4; i++ )
{
if ( switch_status[i] == SWITCH_ON )
2018-05-06 16:56:49 +08:00
digitalWrite(light_to_pin[map_switch_to_light[i]], LIGHT_ON);
2018-05-06 02:23:50 +08:00
else
2018-05-06 16:56:49 +08:00
digitalWrite(light_to_pin[map_switch_to_light[i]], LIGHT_OFF);
2018-05-06 02:23:50 +08:00
}
}
/*==============================================*/
void next_state(void)
{
read_switch_status();
2018-05-06 16:56:49 +08:00
2018-05-06 02:23:50 +08:00
switch(state)
{
case STATE_WAIT:
if ( switch_changed_millis+TIMEOUT < millis() )
{
2018-05-07 03:47:41 +08:00
if ( switch_on_cnt == 0 )
2018-05-06 16:56:49 +08:00
{
switch_changed_millis = millis();
2018-05-07 03:47:41 +08:00
if ( max_on_cnt == 1 ) // main command "1 light max"
2018-05-06 19:27:56 +08:00
{
2018-05-07 03:47:41 +08:00
if ( max_on_cnt_pos1 == 0 ) // sub cmd reset
2018-05-06 19:27:56 +08:00
{
uint8_t i;
for( i = 0; i < 4; i++ )
{
map_switch_to_light[i] = i;
}
u8g2log.print(F("reset\n"));
}
2018-05-07 03:47:41 +08:00
else if ( max_on_cnt_pos1 == 1 ) // sub cmd user choice
{
state = STATE_DELAYED_SWAP_WAIT;
2018-05-08 02:20:22 +08:00
user_pos1 = -1;
user_pos2 = -1;
2018-05-07 03:47:41 +08:00
u8g2log.print(F("user\n"));
}
2018-05-06 19:27:56 +08:00
}
else if ( max_on_cnt == 2 ) // main command "2 light max"
2018-05-06 16:56:49 +08:00
{
// swap two pins
uint8_t tmp;
tmp = map_switch_to_light[max_on_cnt_pos1];
map_switch_to_light[max_on_cnt_pos1] = map_switch_to_light[max_on_cnt_pos2];
map_switch_to_light[max_on_cnt_pos2] = tmp;
2018-05-06 19:27:56 +08:00
u8g2log.print(F("swap\n"));
2018-05-06 16:56:49 +08:00
}
else
{
u8g2log.print("t-out\n");
}
2018-05-06 19:27:56 +08:00
max_on_cnt = 0; // ensure, that the command is not executed again
2018-05-06 16:56:49 +08:00
}
2018-05-06 02:23:50 +08:00
}
2018-05-06 16:56:49 +08:00
break;
2018-05-07 03:47:41 +08:00
case STATE_DELAYED_SWAP_WAIT: // user choice
2018-05-06 22:56:43 +08:00
{
2018-05-08 02:20:22 +08:00
uint8_t i, j;
2018-05-06 22:56:43 +08:00
for( i = 0; i < 4; i++ )
{
2018-05-08 02:20:22 +08:00
if ( user_pos1 != i && switch_status[i] == SWITCH_ON )
2018-05-06 22:56:43 +08:00
{
2018-05-08 02:20:22 +08:00
if ( user_pos1 < 0 )
user_pos1 = i;
2018-05-06 22:56:43 +08:00
else
2018-05-07 03:47:41 +08:00
{
uint8_t tmp;
2018-05-08 02:20:22 +08:00
user_pos2 = i;
tmp = 1<<user_pos1;
tmp |= 1<<user_pos2;
max_on_cnt_npos1 = -1;
max_on_cnt_npos2 = -1;
for( j = 0; j < 4; j++ )
{
if ( ( tmp & (1<<j) ) == 0 )
{
if ( max_on_cnt_npos1 < 0 )
max_on_cnt_npos1 = j;
else
max_on_cnt_npos2 = j;
}
}
2018-05-07 03:47:41 +08:00
tmp = map_switch_to_light[max_on_cnt_npos1];
map_switch_to_light[max_on_cnt_npos1] = map_switch_to_light[max_on_cnt_npos2];
map_switch_to_light[max_on_cnt_npos2] = tmp;
2018-05-08 02:20:22 +08:00
u8g2log.print(F("swap2\n"));
u8g2log.print(max_on_cnt_npos1);
u8g2log.print(F("<>"));
u8g2log.print(max_on_cnt_npos2);
u8g2log.print(F("\n"));
2018-05-07 03:47:41 +08:00
state = STATE_WAIT;
switch_changed_millis = millis();
}
2018-05-06 22:56:43 +08:00
}
}
}
break;
2018-05-06 16:56:49 +08:00
default:
state = STATE_WAIT;
break;
}
if ( is_switch_changed != 0 )
{
print_switch_status();
write_switch_to_light();
is_switch_changed = 0;
2018-05-06 02:23:50 +08:00
}
}
/*==============================================*/
void setup(void)
{
u8g2.begin();
//u8g2.setFont(u8g2_font_tom_thumb_4x6_mf); // set the font for the terminal window
//u8g2.setFont(u8g2_font_lucasfont_alternate_tr);
u8g2.setFont(u8g2_font_6x12_tr);
u8g2log.begin(u8g2, 6, 14, u8log_buffer);
init_switch();
init_light();
}
void loop(void)
{
next_state();
}