Coding crash course
Original esk8.builders post
Alright, guys. It’s time to teach you some fishing since I don’t have enough fish for everyone.
Currently, there are 4 DAVEga screen layouts:
The layouts to be used can be configured here and then you can toggle between them with button 3. Normally you’ll want to enable 2 layouts: the text and one of the graphical ones. It doesn’t really make sense to enable more than 2 though you could.
The code is modular and each screen layout is one class:
You can customize existing layout by editing its class. (You can even add your own new layout. Just use any of the existing layouts as a template. This is not completely straightforward so I only recommend it to people with some coding experience.)
Now, let’s have a look how to get basic things done. To display a value, you’ll want to do two things:
- create a label for it
- display the actual value
Say that we want to modify the simple vertical screen such that it shows BATTERY AMPS instead of TRIP KM.
First, we’ll go to the reset()
method where the labels are drawn. We’ll want to change the following line:
_tft->drawText(0, 130, _config->imperial_units ? "TRIP MI" : "TRIP KM", COLOR_WHITE);
to the following:
_tft->drawText(0, 130, "BATTERY AMPS", COLOR_WHITE);
By that we’re simply saying to write the text BATTERY AMPS
in white color at coordinates [0, 130]. In the original, the ternary expression may look scary:
_config->imperial_units ? "TRIP MI" : "TRIP KM"
This simply says: write TRIP MI
if the _config->imperial_units
flag is true; write TRIP KM
otherwise.
Second, we’ll go to the update()
method where the values are displayed. The reset()
is only called at the screen layout initialization whereas the update()
is called periodically several times a second.
There we’ll want to locate the following code, which displays the trip distance:
// trip distance
dtostrf(convert_distance(data->trip_km, _config->imperial_units), 5, 2, fmt);
tft_util_draw_number(_tft, fmt, 0, 140, progress_to_color(data->session_reset_progress, _tft), COLOR_BLACK, 2, 6);
and change it to something like this:
// battery amps
dtostrf(data->battery_amps, 5, 1, fmt);
tft_util_draw_number(_tft, fmt, 0, 140, COLOR_WHITE, COLOR_BLACK, 2, 6);
Now, let me explain what all of that means. The first line is just a comment. On the second line, we convert the float value data->battery_amps
to a string value in fmt
dtostrf(data->battery_amps, 5, 1, fmt);
The 5 is the total number of characters including the decimal point; the 1 is the number of decimal places. For example, the value of
123.4466666666
would be formatted as
123.4
That’s 5 characters and 1 decimal point precision. If the value is less than 100, there will be leading spaces. For example:
5.339999999999
would be formatted as
__5.3
(two leading spaces). This ensures the value is right-aligned.
On the third line, we display the value stored in the fmt
variable.
tft_util_draw_number(_tft, fmt, 0, 140, COLOR_WHITE, COLOR_BLACK, 2, 6);
The parameters are as follows:
-
_tft
- reference to the display instance (don’t worry about it)
-
fmt
- the string to display
-
0
- x coordinate
-
140
- y coordinate
-
COLOR_WHITE
- text color
-
COLOR_BLACK
- background color
-
2
- number of pixels between characters
-
6
- font size
In the original code, instead of COLOR_WHITE
for text color, there’s the following expression:
progress_to_color(data->session_reset_progress, _tft)
This is to dim the text when the session data is about to be reset. As you press and hold the button 1, the data->session_reset_progress
value will gradually increase from 0.0 to 1.0 and the progress_to_color()
function translates the value into appropriate color (higher = darker). Once the data->session_reset_progress
reaches the value of 1.0, the session data is reset and the value becomes completely black. You don’t need to worry about this. It’s just to explain what’s going on.
The last thing you need to know is what values are available in the data
. You’ll find the list here . For example:
data->speed_kph
will give you the current speed in km/h. Session data can be accessed using double dereference. For example, this is how to get the max speed in km/h:
data->session->max_speed_kph
The list of values available in the session is here .
I hope this is helpful and I’m looking forward to seeing many customized screen layouts.