SwitchBox.ino 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. HelloWorld.ino
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2016, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <Arduino.h>
  28. #include <U8g2lib.h>
  29. uint8_t light_to_pin[4] = { 11, 10, 9, 8 };
  30. uint8_t switch_to_pin[4] = { 7, 6, 5, 4 };
  31. #define SWITCH_ON 0
  32. #define SWITCH_OFF 1
  33. #define LIGHT_ON 1
  34. #define LIGHT_OFF 0
  35. #define TIMEOUT 3000
  36. uint8_t switch_status[4];
  37. #define DEBOUNCE_CNT 3
  38. uint8_t switch_debounce[4];
  39. uint8_t is_switch_changed;
  40. uint8_t last_switch_on_cnt; // number of "on" switches before the last change
  41. uint8_t switch_on_cnt; // current number of "on" switches
  42. uint8_t max_on_cnt; // last number max number of "on" switches
  43. // max_on_cnt==1: max_on_cnt_pos1 --> on switch
  44. // max_on_cnt==2: max_on_cnt_pos1/2 --> on switch
  45. // max_on_cnt==3: max_on_cnt_pos1 --> off switch
  46. // max_on_cnt==4: max_on_cnt_pos1 --> last switch which was turned off
  47. int8_t max_on_cnt_pos1 = -1;
  48. int8_t max_on_cnt_pos2 = -1;
  49. int8_t user_pos1;
  50. int8_t user_pos2;
  51. int8_t max_on_cnt_npos1 = -1;
  52. int8_t max_on_cnt_npos2 = -1;
  53. uint32_t switch_changed_millis;
  54. uint8_t map_switch_to_light[4];
  55. #define STATE_WAIT 0
  56. #define STATE_DELAYED_SWAP_WAIT 1
  57. uint8_t state = STATE_WAIT;
  58. U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R3, /* reset=*/ U8X8_PIN_NONE);
  59. U8G2LOG u8g2log;
  60. uint8_t u8log_buffer[6*14];
  61. void init_switch(void)
  62. {
  63. uint8_t i;
  64. for( i = 0; i < 4; i++ )
  65. {
  66. pinMode(switch_to_pin[i], INPUT_PULLUP);
  67. switch_debounce[i] = 0;
  68. switch_status[i] = 2;
  69. map_switch_to_light[i] = i;
  70. }
  71. }
  72. void read_switch_status(void)
  73. {
  74. uint8_t i, v, on_cnt;
  75. int8_t pos1, pos2, npos1;
  76. for( i = 0; i < 4; i++ )
  77. {
  78. v = digitalRead(switch_to_pin[i]);
  79. if ( switch_debounce[i] > 0 )
  80. {
  81. if ( switch_status[i] == v )
  82. {
  83. switch_debounce[i] = 0; // do nothing, wait for other values
  84. }
  85. else
  86. {
  87. switch_debounce[i]--;
  88. if ( switch_debounce[i] == 0 )
  89. {
  90. switch_status[i] = v; // new value detected
  91. is_switch_changed = 1;
  92. switch_changed_millis = millis();
  93. break; // break out of the loop, so that we detect only one change at a time
  94. }
  95. }
  96. }
  97. else if ( switch_status[i] != v )
  98. {
  99. switch_debounce[i] = DEBOUNCE_CNT; // start debounce
  100. }
  101. }
  102. on_cnt = 0;
  103. pos1 = -1;
  104. pos2 = -1;
  105. for( i = 0; i < 4; i++ )
  106. {
  107. if ( switch_status[i] == SWITCH_ON )
  108. {
  109. on_cnt++;
  110. if ( pos1 < 0 )
  111. pos1 = i;
  112. else
  113. pos2 = i;
  114. }
  115. else
  116. {
  117. npos1 = i;
  118. }
  119. }
  120. if ( switch_on_cnt != on_cnt )
  121. {
  122. last_switch_on_cnt = switch_on_cnt;
  123. switch_on_cnt = on_cnt;
  124. if ( last_switch_on_cnt < switch_on_cnt )
  125. {
  126. max_on_cnt = on_cnt;
  127. if ( max_on_cnt == 1 )
  128. {
  129. max_on_cnt_pos1 = pos1;
  130. max_on_cnt_pos2 = -1;
  131. }
  132. else if ( max_on_cnt == 2 )
  133. {
  134. max_on_cnt_pos1 = pos1;
  135. max_on_cnt_pos2 = pos2;
  136. }
  137. else if ( max_on_cnt == 3 )
  138. {
  139. max_on_cnt_pos1 = npos1;
  140. max_on_cnt_pos2 = -1;
  141. }
  142. else if ( max_on_cnt == 4 )
  143. {
  144. max_on_cnt_pos1 = -1;
  145. max_on_cnt_pos2 = -1;
  146. }
  147. else
  148. {
  149. max_on_cnt_pos1 = -1;
  150. max_on_cnt_pos2 = -1;
  151. }
  152. }
  153. else if ( last_switch_on_cnt > switch_on_cnt )
  154. {
  155. if ( max_on_cnt == 4 )
  156. {
  157. if ( switch_on_cnt == 1 )
  158. {
  159. max_on_cnt_pos1 = pos1;
  160. max_on_cnt_pos2 = -1;
  161. }
  162. }
  163. }
  164. }
  165. }
  166. void print_switch_status(void)
  167. {
  168. uint8_t i;
  169. u8g2log.print(last_switch_on_cnt);
  170. u8g2log.print(">");
  171. u8g2log.print(switch_on_cnt);
  172. u8g2log.print("/");
  173. u8g2log.print(max_on_cnt);
  174. u8g2log.print("\n");
  175. u8g2log.print(max_on_cnt_pos1);
  176. u8g2log.print("/");
  177. u8g2log.print(max_on_cnt_pos2);
  178. u8g2log.print("\n");
  179. for( i = 0; i < 4; i++ )
  180. u8g2log.print(switch_status[i] == 0 ? "*" : ".");
  181. u8g2log.print("\n");
  182. }
  183. void init_light(void)
  184. {
  185. uint8_t i;
  186. for( i = 0; i < 4; i++ )
  187. {
  188. digitalWrite(light_to_pin[i], 1);
  189. pinMode(light_to_pin[i], OUTPUT);
  190. digitalWrite(light_to_pin[i], 1);
  191. }
  192. }
  193. void write_switch_to_light(void)
  194. {
  195. uint8_t i;
  196. for( i = 0; i < 4; i++ )
  197. {
  198. if ( switch_status[i] == SWITCH_ON )
  199. digitalWrite(light_to_pin[map_switch_to_light[i]], LIGHT_ON);
  200. else
  201. digitalWrite(light_to_pin[map_switch_to_light[i]], LIGHT_OFF);
  202. }
  203. }
  204. /*==============================================*/
  205. void next_state(void)
  206. {
  207. read_switch_status();
  208. switch(state)
  209. {
  210. case STATE_WAIT:
  211. if ( switch_changed_millis+TIMEOUT < millis() )
  212. {
  213. if ( switch_on_cnt == 0 )
  214. {
  215. switch_changed_millis = millis();
  216. if ( max_on_cnt == 1 ) // main command "1 light max"
  217. {
  218. if ( max_on_cnt_pos1 == 0 ) // sub cmd reset
  219. {
  220. uint8_t i;
  221. for( i = 0; i < 4; i++ )
  222. {
  223. map_switch_to_light[i] = i;
  224. }
  225. u8g2log.print(F("reset\n"));
  226. }
  227. else if ( max_on_cnt_pos1 == 1 ) // sub cmd user choice
  228. {
  229. state = STATE_DELAYED_SWAP_WAIT;
  230. user_pos1 = -1;
  231. user_pos2 = -1;
  232. u8g2log.print(F("user\n"));
  233. }
  234. }
  235. else if ( max_on_cnt == 2 ) // main command "2 light max"
  236. {
  237. // swap two pins
  238. uint8_t tmp;
  239. tmp = map_switch_to_light[max_on_cnt_pos1];
  240. map_switch_to_light[max_on_cnt_pos1] = map_switch_to_light[max_on_cnt_pos2];
  241. map_switch_to_light[max_on_cnt_pos2] = tmp;
  242. u8g2log.print(F("swap\n"));
  243. }
  244. else
  245. {
  246. u8g2log.print("t-out\n");
  247. }
  248. max_on_cnt = 0; // ensure, that the command is not executed again
  249. }
  250. }
  251. break;
  252. case STATE_DELAYED_SWAP_WAIT: // user choice
  253. {
  254. uint8_t i, j;
  255. for( i = 0; i < 4; i++ )
  256. {
  257. if ( user_pos1 != i && switch_status[i] == SWITCH_ON )
  258. {
  259. if ( user_pos1 < 0 )
  260. user_pos1 = i;
  261. else
  262. {
  263. uint8_t tmp;
  264. user_pos2 = i;
  265. tmp = 1<<user_pos1;
  266. tmp |= 1<<user_pos2;
  267. max_on_cnt_npos1 = -1;
  268. max_on_cnt_npos2 = -1;
  269. for( j = 0; j < 4; j++ )
  270. {
  271. if ( ( tmp & (1<<j) ) == 0 )
  272. {
  273. if ( max_on_cnt_npos1 < 0 )
  274. max_on_cnt_npos1 = j;
  275. else
  276. max_on_cnt_npos2 = j;
  277. }
  278. }
  279. tmp = map_switch_to_light[max_on_cnt_npos1];
  280. map_switch_to_light[max_on_cnt_npos1] = map_switch_to_light[max_on_cnt_npos2];
  281. map_switch_to_light[max_on_cnt_npos2] = tmp;
  282. u8g2log.print(F("swap2\n"));
  283. u8g2log.print(max_on_cnt_npos1);
  284. u8g2log.print(F("<>"));
  285. u8g2log.print(max_on_cnt_npos2);
  286. u8g2log.print(F("\n"));
  287. state = STATE_WAIT;
  288. switch_changed_millis = millis();
  289. }
  290. }
  291. }
  292. }
  293. break;
  294. default:
  295. state = STATE_WAIT;
  296. break;
  297. }
  298. if ( is_switch_changed != 0 )
  299. {
  300. print_switch_status();
  301. write_switch_to_light();
  302. is_switch_changed = 0;
  303. }
  304. }
  305. /*==============================================*/
  306. void setup(void)
  307. {
  308. u8g2.begin();
  309. //u8g2.setFont(u8g2_font_tom_thumb_4x6_mf); // set the font for the terminal window
  310. //u8g2.setFont(u8g2_font_lucasfont_alternate_tr);
  311. u8g2.setFont(u8g2_font_6x12_tr);
  312. u8g2log.begin(u8g2, 6, 14, u8log_buffer);
  313. init_switch();
  314. init_light();
  315. }
  316. void loop(void)
  317. {
  318. next_state();
  319. }