Skip to content

Commit 1b964da

Browse files
author
Jan Pomikálek
authored
Display refactor (#23)
* screen refactoring * screen refactoring: remove dependency on ILI9225 from util * screen refactoring: include screen header only when needed * mini screen (SSD1306)
1 parent 6a2530d commit 1b964da

17 files changed

+256
-42
lines changed

davega.ino

+14-20
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,10 @@
1818
*/
1919

2020
#include "davega_config.h"
21-
#include "davega_screen.h"
2221
#include "davega_eeprom.h"
2322
#include "davega_data.h"
2423
#include "davega_util.h"
2524
#include "davega_screen.h"
26-
#include "davega_default_screen.h"
27-
#include "davega_simple_horizontal_screen.h"
28-
#include "davega_simple_vertical_screen.h"
29-
#include "davega_text_screen.h"
3025
#include "vesc_comm.h"
3126

3227
#define REVISION_ID "$Id$"
@@ -43,27 +38,28 @@
4338
#define BUTTON_2_PIN A2
4439
#define BUTTON_3_PIN A1
4540

46-
#define TFT_RST 12
47-
#define TFT_RS 9
48-
#define TFT_CS 10 // SS
49-
#define TFT_SDI 11 // MOSI
50-
#define TFT_CLK 13 // SCK
51-
#define TFT_LED 0
52-
5341
#define LEN(X) (sizeof(X) / sizeof(X[0]))
5442

5543
#ifdef DEFAULT_SCREEN_ENABLED
44+
#include "davega_default_screen.h"
5645
DavegaDefaultScreen davega_default_screen = DavegaDefaultScreen();
5746
#endif
5847
#ifdef SIMPLE_HORIZONTAL_SCREEN_ENABLED
48+
#include "davega_simple_horizontal_screen.h"
5949
DavegaSimpleHorizontalScreen davega_simple_horizontal_screen = DavegaSimpleHorizontalScreen();
6050
#endif
6151
#ifdef SIMPLE_VERTICAL_SCREEN_ENABLED
52+
#include "davega_simple_vertical_screen.h"
6253
DavegaSimpleVerticalScreen davega_simple_vertical_screen = DavegaSimpleVerticalScreen();
6354
#endif
6455
#ifdef TEXT_SCREEN_ENABLED
56+
#include "davega_text_screen.h"
6557
DavegaTextScreen davega_text_screen = DavegaTextScreen();
6658
#endif
59+
#ifdef MINI_SCREEN_ENABLED
60+
#include "davega_mini_screen.h"
61+
DavegaMiniScreen davega_mini_screen = DavegaMiniScreen();
62+
#endif
6763

6864
DavegaScreen* davega_screens[] = {
6965
#ifdef DEFAULT_SCREEN_ENABLED
@@ -78,10 +74,11 @@ DavegaScreen* davega_screens[] = {
7874
#ifdef TEXT_SCREEN_ENABLED
7975
&davega_text_screen,
8076
#endif
77+
#ifdef MINI_SCREEN_ENABLED
78+
&davega_mini_screen,
79+
#endif
8180
};
8281

83-
TFT_22_ILI9225 tft = TFT_22_ILI9225(TFT_RST, TFT_RS, TFT_CS, TFT_LED, 200);
84-
8582
t_text_screen_item text_screen_items[] = TEXT_SCREEN_ITEMS;
8683

8784
t_davega_screen_config screen_config = {
@@ -92,7 +89,8 @@ t_davega_screen_config screen_config = {
9289
BATTERY_S,
9390
TEXT_SCREEN_BIG_FONT,
9491
text_screen_items,
95-
LEN(text_screen_items)
92+
LEN(text_screen_items),
93+
SCREEN_ORIENTATION
9694
};
9795

9896
int current_screen_index = 0;
@@ -173,12 +171,8 @@ void setup() {
173171
eeprom_write_session_data(session_data);
174172
}
175173

176-
tft.begin();
177-
tft.setOrientation(SCREEN_ORIENTATION);
178-
tft.setBackgroundColor(COLOR_BLACK);
179-
180174
for (int i=0; i<LEN(davega_screens); i++)
181-
davega_screens[i]->init(&tft, &screen_config);
175+
davega_screens[i]->init(&screen_config);
182176
scr = davega_screens[current_screen_index];
183177

184178
session_data = eeprom_read_session_data();

davega_config.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,15 @@
128128
#define SCREEN_ORIENTATION 0
129129

130130
// Screens. Uncomment the ones you want enabled.
131+
132+
// TFT ILI9225 Screens
131133
//#define DEFAULT_SCREEN_ENABLED 1
132134
//#define SIMPLE_HORIZONTAL_SCREEN_ENABLED 1
133-
#define SIMPLE_VERTICAL_SCREEN_ENABLED 1
134-
#define TEXT_SCREEN_ENABLED 1
135+
//#define SIMPLE_VERTICAL_SCREEN_ENABLED 1
136+
//#define TEXT_SCREEN_ENABLED 1
137+
138+
// OLED SSD1306 Screens
139+
#define MINI_SCREEN_ENABLED 1
135140

136141
// Information to be displayed on the text screen. Only relevant if TEXT_SCREEN_ENABLED is set.
137142
// Available options:

davega_default_screen.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
#define DAVEGA_DEFAULT_SCREEN_H
2222

2323
#include <TFT_22_ILI9225.h>
24-
#include "davega_screen.h"
24+
#include "davega_ili9225_screen.h"
2525
#include "vesc_comm.h"
2626

27-
class DavegaDefaultScreen: public DavegaScreen {
27+
class DavegaDefaultScreen: public DavegaILI9225Screen {
2828
public:
2929
void reset();
3030
void update(t_davega_data* data);

davega_ili9225_screen.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2019 Jan Pomikalek <jan.pomikalek@gmail.com>
3+
4+
This file is part of the DAVEga firmware.
5+
6+
DAVEga firmware is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
DAVEga firmware is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with DAVEga firmware. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include "davega_ili9225_screen.h"
21+
22+
#define TFT_RST 12
23+
#define TFT_RS 9
24+
#define TFT_CS 10 // SS
25+
#define TFT_SDI 11 // MOSI
26+
#define TFT_CLK 13 // SCK
27+
#define TFT_LED 0
28+
29+
TFT_22_ILI9225 tft = TFT_22_ILI9225(TFT_RST, TFT_RS, TFT_CS, TFT_LED, 200);
30+
TFT_22_ILI9225* p_tft = nullptr;
31+
32+
void DavegaILI9225Screen::init(t_davega_screen_config *config) {
33+
DavegaScreen::init(config);
34+
if (!p_tft) {
35+
p_tft = &tft;
36+
p_tft->begin();
37+
p_tft->setOrientation(config->orientation);
38+
p_tft->setBackgroundColor(COLOR_BLACK);
39+
}
40+
_tft = p_tft;
41+
}

davega_ili9225_screen.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright 2019 Jan Pomikalek <jan.pomikalek@gmail.com>
3+
4+
This file is part of the DAVEga firmware.
5+
6+
DAVEga firmware is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
DAVEga firmware is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with DAVEga firmware. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef DAVEGA_ILI9225_SCREEN_H
21+
#define DAVEGA_ILI9225_SCREEN_H
22+
23+
#include "davega_screen.h"
24+
#include <TFT_22_ILI9225.h>
25+
26+
class DavegaILI9225Screen: public DavegaScreen {
27+
public:
28+
void init(t_davega_screen_config* config) override;
29+
30+
protected:
31+
TFT_22_ILI9225* _tft;
32+
};
33+
34+
#endif //DAVEGA_ILI9225_H

davega_mini_screen.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2018 Jan Pomikalek <jan.pomikalek@gmail.com>
3+
4+
This file is part of the DAVEga firmware.
5+
6+
DAVEga firmware is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
DAVEga firmware is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with DAVEga firmware. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include "davega_mini_screen.h"
21+
22+
void DavegaMiniScreen::reset() {
23+
_oled->clearDisplay();
24+
}
25+
26+
void DavegaMiniScreen::update(t_davega_data *data) {
27+
_oled->clearDisplay();
28+
_oled->setTextSize(1);
29+
_oled->setTextColor(WHITE);
30+
_oled->setCursor(0,0);
31+
_oled->println(String(data->session->max_speed_kph) + String(" km/h [max spd]"));
32+
_oled->println(String(data->voltage / _config->battery_cells) + String(" V [battery]"));
33+
_oled->println(String(data->trip_km) + String(" km [trip]"));
34+
_oled->println(String(data->total_km) + String(" km [total]"));
35+
_oled->display();
36+
}
37+
38+
void DavegaMiniScreen::heartbeat(uint32_t duration_ms, bool successful_vesc_read) {
39+
// TODO
40+
}

davega_mini_screen.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright 2018 Jan Pomikalek <jan.pomikalek@gmail.com>
3+
4+
This file is part of the DAVEga firmware.
5+
6+
DAVEga firmware is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
DAVEga firmware is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with DAVEga firmware. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef DAVEGA_MINI_SCREEN_H
21+
#define DAVEGA_MINI_SCREEN_H
22+
23+
#include "davega_ssd1306_screen.h"
24+
25+
class DavegaMiniScreen: public DavegaSSD1306Screen {
26+
public:
27+
void reset();
28+
void update(t_davega_data* data);
29+
void heartbeat(uint32_t duration_ms, bool successful_vesc_read);
30+
};
31+
32+
#endif //DAVEGA_MINI_SCREEN_H

davega_screen.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#ifndef DAVEGA_SCREEN_H
2121
#define DAVEGA_SCREEN_H
2222

23-
#include <TFT_22_ILI9225.h>
2423
#include "davega_data.h"
2524

2625
typedef enum {
@@ -54,17 +53,17 @@ typedef struct {
5453
bool big_font;
5554
t_text_screen_item* text_screen_items;
5655
uint8_t text_screen_items_count;
56+
uint8_t orientation;
5757
} t_davega_screen_config;
5858

5959
class DavegaScreen {
6060
public:
61-
void init(TFT_22_ILI9225* tft, t_davega_screen_config* config) { _tft = tft; _config = config; };
61+
virtual void init(t_davega_screen_config* config) { _config = config; };
6262
virtual void reset() = 0;
6363
virtual void update(t_davega_data* data) = 0;
6464
virtual void heartbeat(uint32_t duration_ms, bool successful_vesc_read) = 0;
6565

6666
protected:
67-
TFT_22_ILI9225* _tft;
6867
t_davega_screen_config* _config;
6968
};
7069

davega_simple_horizontal_screen.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
#define DAVEGA_SIMPLE_HORIZONTAL_SCREEN_H
2222

2323
#include <TFT_22_ILI9225.h>
24-
#include "davega_screen.h"
24+
#include "davega_ili9225_screen.h"
2525
#include "davega_simple_screen.h"
2626

27-
class DavegaSimpleHorizontalScreen: public DavegaScreen {
27+
class DavegaSimpleHorizontalScreen: public DavegaILI9225Screen {
2828
public:
2929
void reset();
3030
void update(t_davega_data* data);

davega_simple_vertical_screen.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
#define DAVEGA_SIMPLE_VERTICAL_SCREEN_H
2222

2323
#include <TFT_22_ILI9225.h>
24-
#include "davega_screen.h"
24+
#include "davega_ili9225_screen.h"
2525
#include "davega_simple_screen.h"
2626

27-
class DavegaSimpleVerticalScreen: public DavegaScreen {
27+
class DavegaSimpleVerticalScreen: public DavegaILI9225Screen {
2828
public:
2929
void reset();
3030
void update(t_davega_data* data);

davega_ssd1306_screen.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright 2019 Jan Pomikalek <jan.pomikalek@gmail.com>
3+
4+
This file is part of the DAVEga firmware.
5+
6+
DAVEga firmware is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
DAVEga firmware is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with DAVEga firmware. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include "davega_mini_screen.h"
21+
22+
#define OLED_RESET 4
23+
Adafruit_SSD1306 oled(OLED_RESET);
24+
Adafruit_SSD1306* p_oled = nullptr;
25+
26+
void DavegaSSD1306Screen::init(t_davega_screen_config *config) {
27+
DavegaScreen::init(config);
28+
if (!p_oled) {
29+
p_oled = &oled;
30+
p_oled->begin(SSD1306_SWITCHCAPVCC, 0x3C);
31+
p_oled->clearDisplay();
32+
}
33+
_oled = p_oled;
34+
}

davega_ssd1306_screen.h

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2019 Jan Pomikalek <jan.pomikalek@gmail.com>
3+
4+
This file is part of the DAVEga firmware.
5+
6+
DAVEga firmware is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
DAVEga firmware is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with DAVEga firmware. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef DAVEGA_SSD1306_SCREEN_H
21+
#define DAVEGA_SSD1306_SCREEN_H
22+
23+
#include "davega_screen.h"
24+
#include <SPI.h>
25+
#include <Wire.h>
26+
#include <Adafruit_GFX.h>
27+
#include <Adafruit_SSD1306.h>
28+
29+
class DavegaSSD1306Screen: public DavegaScreen {
30+
public:
31+
void init(t_davega_screen_config* config) override;
32+
33+
protected:
34+
Adafruit_SSD1306* _oled;
35+
};
36+
37+
#endif //DAVEGA_SSD1306_SCREEN_H

0 commit comments

Comments
 (0)