3D printed Arduino Airsoft Chronograph
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * lcd.cpp
  3. *
  4. * OpenChrono BB speed measurement device.
  5. *
  6. * Copyright (c) 2022 Thomas Buck <thomas@xythobuz.de>
  7. *
  8. * OpenChrono is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * OpenChrono is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with OpenChrono. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. * SSD1306 OLED display connected via I2C.
  22. */
  23. #include <Arduino.h>
  24. #include "ticks.h"
  25. #include "adc.h"
  26. #include "lcd.h"
  27. #include "config.h"
  28. #include <Wire.h>
  29. #include <U8g2lib.h>
  30. static uint8_t screens[] = SCREEN_ROTATION;
  31. static uint8_t lcd_screen = 0;
  32. static uint64_t lcd_rotate_time = 0;
  33. static LCD_TYPE u8g2(U8G2_R0, U8X8_PIN_NONE);
  34. void lcd_init(void) {
  35. u8g2.begin();
  36. u8g2.setFontPosBottom();
  37. u8g2.clearBuffer();
  38. u8g2.setFlipMode(FLIP_SCREEN);
  39. String s = F("OpenChrono");
  40. u8g2.setFont(HEADING_FONT);
  41. uint8_t heading_height = u8g2.getMaxCharHeight();
  42. u8g2.drawStr(
  43. 0,
  44. heading_height,
  45. s.c_str()
  46. );
  47. s = F("Version ");
  48. s += F(VERSION);
  49. u8g2.setFont(TEXT_FONT);
  50. u8g2.drawStr(
  51. (u8g2.getDisplayWidth() - u8g2.getStrWidth(s.c_str())) / 2,
  52. heading_height + u8g2.getMaxCharHeight() + 4,
  53. s.c_str()
  54. );
  55. s = String((double)SENSOR_DISTANCE, 0);
  56. s += F("mm");
  57. u8g2.setFont(TEXT_FONT);
  58. u8g2.drawStr(
  59. 0,
  60. u8g2.getDisplayHeight() - u8g2.getMaxCharHeight() - 2,
  61. s.c_str()
  62. );
  63. s = String((double)readVcc() / 1000.0, 1);
  64. s += F("V");
  65. u8g2.setFont(TEXT_FONT);
  66. u8g2.drawStr(
  67. (u8g2.getDisplayWidth() - u8g2.getStrWidth(s.c_str())) / 2,
  68. u8g2.getDisplayHeight() - u8g2.getMaxCharHeight() - 2,
  69. s.c_str()
  70. );
  71. s = String((double)BB_WEIGHT, 2);
  72. s += F("g");
  73. u8g2.setFont(TEXT_FONT);
  74. u8g2.drawStr(
  75. u8g2.getDisplayWidth() - u8g2.getStrWidth(s.c_str()),
  76. u8g2.getDisplayHeight() - u8g2.getMaxCharHeight() - 2,
  77. s.c_str()
  78. );
  79. s = F("by xythobuz.de");
  80. u8g2.setFont(TEXT_FONT);
  81. u8g2.drawStr(
  82. (u8g2.getDisplayWidth() - u8g2.getStrWidth(s.c_str())) / 2,
  83. u8g2.getDisplayHeight() - 1,
  84. s.c_str()
  85. );
  86. u8g2.sendBuffer();
  87. }
  88. void lcd_new_value(void) {
  89. lcd_rotate_time = millis();
  90. lcd_screen = 0;
  91. lcd_draw(screens[lcd_screen]);
  92. }
  93. void lcd_draw(uint8_t screen) {
  94. // fall back to first screen when no more data available
  95. if (tick_count <= 1) {
  96. screen = SCREEN_CURRENT;
  97. }
  98. if ((screen == SCREEN_CURRENT) || (screen == SCREEN_AVERAGE)
  99. || (screen == SCREEN_MIN) || (screen == SCREEN_MAX)) {
  100. if (tick_count < 1) {
  101. u8g2.clearBuffer();
  102. u8g2.setFlipMode(FLIP_SCREEN);
  103. String s = F("Ready!");
  104. u8g2.setFont(HEADING_FONT);
  105. u8g2.drawStr(
  106. (u8g2.getDisplayWidth() - u8g2.getStrWidth(s.c_str())) / 2,
  107. (u8g2.getDisplayHeight() + u8g2.getMaxCharHeight()) / 2,
  108. s.c_str()
  109. );
  110. u8g2.sendBuffer();
  111. return;
  112. }
  113. uint16_t tick = 0;
  114. if (screen == SCREEN_CURRENT) {
  115. // only show most recent value
  116. tick = tick_history[tick_count - 1];
  117. } else if (screen == SCREEN_AVERAGE) {
  118. tick = tick_average();
  119. } else if (screen == SCREEN_MAX) {
  120. tick = tick_min();
  121. } else if (screen == SCREEN_MIN) {
  122. tick = tick_max();
  123. }
  124. double metric = tick_to_metric(tick);
  125. double imperial = metric_to_imperial(metric);
  126. double weights[] = BB_WEIGHTS;
  127. double joules[sizeof(weights) / sizeof(weights[0])];
  128. for (uint8_t i = 0; i < (sizeof(weights) / sizeof(weights[0])); i++) {
  129. joules[i] = metric_to_joules(metric, weights[i]);
  130. }
  131. u8g2.clearBuffer();
  132. u8g2.setFlipMode(FLIP_SCREEN);
  133. u8g2.setFont(TEXT_FONT);
  134. String s;
  135. if (screen == SCREEN_CURRENT) {
  136. s = F("Last Shot (No. ");
  137. } else if (screen == SCREEN_AVERAGE) {
  138. s = F("Average (of ");
  139. } else if (screen == SCREEN_MAX) {
  140. s = F("Maximum (of ");
  141. } else if (screen == SCREEN_MIN) {
  142. s = F("Minimum (of ");
  143. }
  144. s += String(tick_count);
  145. s += F(")");
  146. u8g2.drawStr(
  147. (u8g2.getDisplayWidth() - u8g2.getStrWidth(s.c_str())) / 2,
  148. u8g2.getMaxCharHeight(),
  149. s.c_str()
  150. );
  151. uint8_t left_off = (u8g2.getDisplayHeight() - (u8g2.getMaxCharHeight() * 4 + 3)) / 2;
  152. if (metric < 10.0) {
  153. s = String(metric, 1);
  154. } else {
  155. s = String(metric, 0);
  156. }
  157. s += F(" m/s");
  158. uint8_t l1 = u8g2.getStrWidth(s.c_str());
  159. u8g2.drawStr(
  160. 0,
  161. u8g2.getMaxCharHeight() * 2 + 1 + left_off,
  162. s.c_str()
  163. );
  164. if (imperial < 10.0) {
  165. s = String(imperial, 1);
  166. } else {
  167. s = String(imperial, 0);
  168. }
  169. s += F(" FPS");
  170. uint8_t l2 = u8g2.getStrWidth(s.c_str());
  171. u8g2.drawStr(
  172. 0,
  173. u8g2.getMaxCharHeight() * 3 + 2 + left_off,
  174. s.c_str()
  175. );
  176. uint8_t l3 = 0;
  177. for (uint8_t i = 0; i < (sizeof(weights) / sizeof(weights[0])); i++) {
  178. if (weights[i] == BB_WEIGHT) {
  179. s = String(joules[i], 2);
  180. s += F(" J");
  181. l3 = u8g2.getStrWidth(s.c_str());
  182. u8g2.drawStr(
  183. 0,
  184. u8g2.getMaxCharHeight() * 4 + 3 + left_off,
  185. s.c_str()
  186. );
  187. break;
  188. }
  189. }
  190. uint8_t l4 = 0;
  191. uint8_t h = u8g2.getMaxCharHeight() + 1;
  192. for (uint8_t i = 0; i < (sizeof(weights) / sizeof(weights[0])); i++) {
  193. if (weights[i] == BB_WEIGHT) {
  194. //u8g2.setFont(TEXT_FONT);
  195. continue;
  196. } else {
  197. u8g2.setFont(SMALL_FONT);
  198. }
  199. s = String(weights[i], 2);
  200. s += F("g ");
  201. s += String(joules[i], 2);
  202. s += F("J");
  203. uint8_t l = u8g2.getStrWidth(s.c_str());
  204. if (l > l4) {
  205. l4 = l;
  206. }
  207. h += u8g2.getMaxCharHeight() + 1;
  208. u8g2.drawStr(
  209. u8g2.getDisplayWidth() - l,
  210. h,
  211. s.c_str()
  212. );
  213. }
  214. uint8_t l_left = max(max(l1, l2), l3);
  215. uint8_t l_right = u8g2.getDisplayWidth() - l4;
  216. uint8_t lmax = (uint8_t)((((uint16_t)l_left) + ((uint16_t)l_right)) / 2);
  217. u8g2.setFont(TEXT_FONT);
  218. u8g2.drawLine(
  219. lmax, u8g2.getMaxCharHeight() + 2,
  220. lmax, u8g2.getDisplayHeight()
  221. );
  222. u8g2.sendBuffer();
  223. } else if (screen == SCREEN_HISTORY) {
  224. uint16_t min = tick_max();
  225. uint16_t max = tick_min();
  226. String s;
  227. u8g2.clearBuffer();
  228. u8g2.setFlipMode(FLIP_SCREEN);
  229. u8g2.setFont(TEXT_FONT);
  230. // max text
  231. double max_metric = tick_to_metric(max);
  232. if (PREFERRED_UNITS == METRIC) {
  233. if (max_metric < 10.0) {
  234. s = String(max_metric, 1);
  235. } else {
  236. s = String(max_metric, 0);
  237. }
  238. } else if (PREFERRED_UNITS == IMPERIAL) {
  239. double max_imperial = metric_to_imperial(max_metric);
  240. if (max_imperial < 10.0) {
  241. s = String(max_imperial, 1);
  242. } else {
  243. s = String(max_imperial, 0);
  244. }
  245. } else {
  246. s = String(metric_to_joules(max_metric, BB_WEIGHT), 2);
  247. }
  248. uint8_t l1 = u8g2.getStrWidth(s.c_str());
  249. u8g2.drawStr(
  250. 0,
  251. u8g2.getMaxCharHeight(),
  252. s.c_str()
  253. );
  254. // unit indicator
  255. uint8_t l2;
  256. if (PREFERRED_UNITS == METRIC) {
  257. s = F("m/s");
  258. l2 = u8g2.getStrWidth(s.c_str());
  259. u8g2.drawStr(
  260. 0,
  261. (u8g2.getDisplayHeight() + u8g2.getMaxCharHeight()) / 2,
  262. s.c_str()
  263. );
  264. } else if (PREFERRED_UNITS == IMPERIAL) {
  265. s = F("FPS");
  266. l2 = u8g2.getStrWidth(s.c_str());
  267. u8g2.drawStr(
  268. 0,
  269. (u8g2.getDisplayHeight() + u8g2.getMaxCharHeight()) / 2,
  270. s.c_str()
  271. );
  272. } else {
  273. s = F("J");
  274. l2 = u8g2.getStrWidth(s.c_str());
  275. u8g2.drawStr(
  276. 0,
  277. (u8g2.getDisplayHeight() + u8g2.getMaxCharHeight()) / 2,
  278. s.c_str()
  279. );
  280. }
  281. // min text
  282. double min_metric = tick_to_metric(min);
  283. if (PREFERRED_UNITS == METRIC) {
  284. if (min_metric < 10.0) {
  285. s = String(min_metric, 1);
  286. } else {
  287. s = String(min_metric, 0);
  288. }
  289. } else if (PREFERRED_UNITS == IMPERIAL) {
  290. double min_imperial = metric_to_imperial(min_metric);
  291. if (min_imperial < 10.0) {
  292. s = String(min_imperial, 1);
  293. } else {
  294. s = String(min_imperial, 0);
  295. }
  296. } else {
  297. s = String(metric_to_joules(min_metric, BB_WEIGHT), 2);
  298. }
  299. uint8_t l3 = u8g2.getStrWidth(s.c_str());
  300. u8g2.drawStr(
  301. 0,
  302. u8g2.getDisplayHeight() - 1,
  303. s.c_str()
  304. );
  305. uint8_t lmax = max(max(l1, l2), l3);
  306. uint8_t graph_start = lmax + 1;
  307. // graph lines
  308. uint8_t segment_w = (u8g2.getDisplayWidth() - graph_start) / (tick_count - 1);
  309. for (int i = 0; i < tick_count - 1; i++) {
  310. u8g2.drawLine(
  311. graph_start + (i * segment_w),
  312. map(tick_history[i], min, max, u8g2.getDisplayHeight() - 1, 0),
  313. graph_start + ((i + 1) * segment_w),
  314. map(tick_history[i + 1], min, max, u8g2.getDisplayHeight() - 1, 0)
  315. );
  316. }
  317. u8g2.sendBuffer();
  318. }
  319. }
  320. void lcd_loop(void) {
  321. if ((millis() - lcd_rotate_time) > SCREEN_TIMEOUT) {
  322. lcd_rotate_time = millis();
  323. lcd_screen++;
  324. if (lcd_screen >= (sizeof(screens) / sizeof(screens[0]))) {
  325. lcd_screen = 0;
  326. }
  327. lcd_draw(screens[lcd_screen]);
  328. }
  329. }