|
| 1 | +/* Arduino-based replica of the Back to the Future DeLorean Speedometer |
| 2 | + Copyright (C) 2017 Baptiste Candellier |
| 3 | +
|
| 4 | + This program is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation, either version 3 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +
|
| 9 | + This program is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU General Public License |
| 15 | + along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 | + |
| 17 | +#include <Arduino.h> |
| 18 | +#include "DigitalSpeedometer.h" |
| 19 | + |
| 20 | +void setup_display() { |
| 21 | + byte numDigits = 2; |
| 22 | + byte digitPins[] = {PIN_DIG_1, PIN_DIG_2}; |
| 23 | + byte segmentPins[] = { |
| 24 | + PIN_SEG_A, |
| 25 | + PIN_SEG_B, |
| 26 | + PIN_SEG_C, |
| 27 | + PIN_SEG_D, |
| 28 | + PIN_SEG_E, |
| 29 | + PIN_SEG_F, |
| 30 | + PIN_SEG_G, |
| 31 | + PIN_SEG_DP |
| 32 | + }; |
| 33 | + |
| 34 | + // If you change these you might need to hack around in SevSeg.cpp to |
| 35 | + // replay my crappy hacks |
| 36 | + bool resistorsOnSegments = true; |
| 37 | + byte hardwareConfig = COMMON_CATHODE; |
| 38 | + bool updateWithDelays = false; |
| 39 | + bool leadingZeros = false; |
| 40 | + |
| 41 | + sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, |
| 42 | + resistorsOnSegments, updateWithDelays, leadingZeros); |
| 43 | + |
| 44 | + sevseg.blank(); |
| 45 | +} |
| 46 | + |
| 47 | +void setup_timers() { |
| 48 | + // Timer that will refresh the display (interpolation using SevSeg) |
| 49 | + Timer1.initialize(TIMER_INTERVAL_DISP_REFRESH_MS * 1000); |
| 50 | + Timer1.attachInterrupt(isr_refresh_display); |
| 51 | + |
| 52 | + // Timer that will display the speed read from OBD every xxx ms, |
| 53 | + // interpolating if needed |
| 54 | + MsTimer2::set(TIMER_INTERVAL_DISP_INC_MS, isr_display); |
| 55 | + MsTimer2::start(); |
| 56 | +} |
| 57 | + |
| 58 | +speed_t adjust_speed(speed_t speed) { |
| 59 | + // Adjust read speed using modifier (set via potentiometer) and take |
| 60 | + // unit into account |
| 61 | + if (unit_select == UNIT_MPH) { |
| 62 | + // Convert from km/h to mph |
| 63 | + return round(modifier * (float)speed * 0.621371); |
| 64 | + } |
| 65 | + |
| 66 | + return round(modifier * (float)speed); |
| 67 | +} |
| 68 | + |
| 69 | +void set_displayed_speed(speed_t speed) { |
| 70 | + // Display number with a decimal point |
| 71 | + sevseg.setNumber(speed, 0); |
| 72 | +} |
| 73 | + |
| 74 | +void isr_display() { |
| 75 | + // Speed currently displayed; will be incremented to reach target speed |
| 76 | + static speed_t curr_disp_speed = 0; |
| 77 | + |
| 78 | + if (state != STATE_CONNECTED) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + // Make copy of target speed |
| 83 | + const speed_t target_speed = adjust_speed(target_read_speed); |
| 84 | + |
| 85 | + // We want to increment speed one by one until we hit the target speed |
| 86 | + // on a relatively short duration |
| 87 | + double interval_between_incs = TIMER_INTERVAL_DISP_INC_MS / (abs(target_speed - curr_disp_speed)); |
| 88 | + |
| 89 | + // Enable interrupts for the rest of the procedure, |
| 90 | + // because we want the display to still be refreshed |
| 91 | + interrupts(); |
| 92 | + |
| 93 | + if (curr_disp_speed == target_speed) { |
| 94 | + set_displayed_speed(curr_disp_speed); |
| 95 | + } |
| 96 | + |
| 97 | + // Until we've hit the target speed, increment, display and pause |
| 98 | + while (curr_disp_speed != target_speed) { |
| 99 | + if (curr_disp_speed < target_speed) { |
| 100 | + curr_disp_speed++; |
| 101 | + } else { |
| 102 | + curr_disp_speed--; |
| 103 | + } |
| 104 | + |
| 105 | + // Display and pause execution for a fixed amount of time between each iteration |
| 106 | + set_displayed_speed(curr_disp_speed); |
| 107 | + delay(interval_between_incs); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Called every 10 ms |
| 112 | +void isr_refresh_display() { |
| 113 | + sevseg.refreshDisplay(); |
| 114 | +} |
0 commit comments