Skip to content

Commit b6b0022

Browse files
committed
Adjusting the code for 128x64 OLED display
1 parent 15b4354 commit b6b0022

File tree

1 file changed

+51
-38
lines changed

1 file changed

+51
-38
lines changed

src/main.cpp

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,26 @@
4444
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
4545
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
4646
#define SCREEN_WIDTH 128 // OLED display width, in pixels
47-
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
47+
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
4848
const int OLED_I2C_ADDR = 0x3C; // Address 0x3C for 128x32
4949

5050
SSD1306AsciiWire display;
5151

52-
// Current and voltage sensor
52+
// Current and voltage sensor class
5353
Adafruit_INA219 ina219_monitor;
5454

55-
// DS3231 RTC modul
55+
// DS3231 RTC modul I2C address
5656
const int RTC_I2C_addr = 0x68;
57+
// RTC class
5758
uRTCLib rtc(RTC_I2C_addr);
5859

59-
// SD card modul
60+
// SD card modul chip select
6061
#define SDCARD_CHIP_SELECT 4
6162

6263

6364
/* Global variables */
6465

65-
// RTC global variables
66+
/* RTC global variables */
6667
uint8_t rtc_second = 0;
6768
uint8_t rtc_minute = 0;
6869
uint8_t rtc_hour = 0;
@@ -71,27 +72,26 @@ uint8_t rtc_month = 0;
7172
uint8_t rtc_year = 0;
7273

7374
// Current sensor variables
74-
float f_BusVoltage_V;
75-
float f_ShuntCurrent_mA;
75+
float f_BusVoltage_V; /** Measured bus voltage */
76+
float f_ShuntCurrent_mA; /** Measured shunt current */
7677

7778

7879
// General variables
79-
char DateStampString[] = "2000.99.88";
80-
char TimeStampString[] = "00:00:00";
81-
char logFileName[] = "mmddHHMM.txt";
82-
char VoltString[] = "99.999 ";
83-
char CurrentString[] = "9999.999 ";
84-
bool SD_log_enabled = false;
85-
80+
char DateStampString[] = "2000.99.88"; /** String to store date value */
81+
char TimeStampString[] = "00:00:00"; /** String to store time value */
82+
char logFileName[] = "mmddHHMM.txt"; /** String to store log file name */
83+
char VoltString[] = "99.999 "; /** String to store measured voltage value */
84+
char CurrentString[] = "9999.999 "; /** String to store measured current value */
85+
bool SD_log_enabled = false; /** Enabling SD logging or not */
86+
87+
// Datafile class
8688
File dataFile;
8789

8890

89-
// Function definitions
90-
//void setup();
91-
//void loop();
92-
//bool Log_To_SD_card(const char *_logfile);
93-
bool Log_To_SD_card();
94-
void setTimeStampString();
91+
/* Function definitions */
92+
93+
bool Log_To_SD_card(void);
94+
void setTimeStampString(void);
9595

9696

9797
//setup()
@@ -105,12 +105,23 @@ void setup()
105105
//display.begin(SSD1306_SWITCHCAPVCC, OLED_I2C_ADDR);
106106
Serial.print(F("SSD1306 init..."));
107107
#if OLED_RESET >= 0
108-
display.begin(&Adafruit128x32, I2C_ADDRESS, RST_PIN);
108+
#if SCREEN_HEIGHT == 32
109+
display.begin(&Adafruit128x32, I2C_ADDRESS, RST_PIN);
110+
#endif
111+
#if SCREEN_HEIGHT == 64
112+
display.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
113+
#endif
109114
#else // RST_PIN >= 0
110-
display.begin(&Adafruit128x32, OLED_I2C_ADDR);
115+
#if SCREEN_HEIGHT == 32
116+
display.begin(&Adafruit128x32, OLED_I2C_ADDR);
117+
#endif
118+
#if SCREEN_HEIGHT == 64
119+
display.begin(&Adafruit128x64, OLED_I2C_ADDR);
120+
#endif
111121
#endif // RST_PIN >= 0
112122

113123
display.setFont(Adafruit5x7);
124+
//display.setFont(X11fixed7x14);
114125

115126
Serial.println(F(" OK"));
116127
display.clear();
@@ -151,11 +162,9 @@ void loop()
151162
{
152163
setTimeStampString();
153164

154-
// clear display
155-
//display.clear();
156-
//display.set1X();
157165
display.setRow(0);
158166
display.setCol(0);
167+
display.set1X();
159168

160169
//display time stamp
161170
Serial.print(DateStampString);
@@ -172,17 +181,14 @@ void loop()
172181
f_BusVoltage_V=ina219_monitor.getBusVoltage_V();
173182

174183
//convert to text
175-
dtostrf((f_ShuntCurrent_mA),8,3,CurrentString);
184+
dtostrf((f_ShuntCurrent_mA),7,2,CurrentString);
176185
dtostrf(f_BusVoltage_V,6,3,VoltString);
177-
//CurrentString=String(f_ShuntCurrent_mA*1000, 4);
178-
//CurrentString+=F(" mA");
179-
//VoltString="";
180-
//VoltString=String(f_BusVoltage_V,4);
181-
//VoltString+=F(" V");
182186

183187
//display volt
184188
Serial.print(VoltString);
185189
Serial.print(F(" V"));
190+
display.set2X();
191+
display.print(F(" "));
186192
display.print(VoltString);
187193
display.println(F(" V"));
188194

@@ -192,6 +198,7 @@ void loop()
192198
display.print(CurrentString);
193199
display.println(F(" mA"));
194200

201+
display.set1X();
195202
if(SD_log_enabled) {
196203
Serial.print(F("SD log: "));
197204
Serial.print(logFileName);
@@ -211,7 +218,11 @@ void loop()
211218

212219
}
213220

214-
void setTimeStampString()
221+
/**
222+
* @brief Query date & time and set the [Date|Time]StampStrings & logFileName accordingly.
223+
* @param void
224+
*/
225+
void setTimeStampString(void)
215226
{
216227
// get time stamp, convert to a string
217228
rtc.refresh();
@@ -244,12 +255,15 @@ void setTimeStampString()
244255
logFileName[5] = DateStampString[6];
245256
logFileName[6] = DateStampString[8];
246257
logFileName[7] = DateStampString[9];
247-
248-
258+
249259
}
250260

251-
//bool Log_To_SD_card(const char *_logfile)
252-
bool Log_To_SD_card()
261+
/**
262+
* @brief Log the measurements with timestamp to SD card in CSV format.
263+
* @param void
264+
* @return bool FileOpenSuccess
265+
*/
266+
bool Log_To_SD_card(void)
253267
{
254268
bool FileOpenSuccess = false;
255269

@@ -275,8 +289,7 @@ bool Log_To_SD_card()
275289
dataFile.print(CurrentString);
276290
dataFile.print(F(","));
277291
dataFile.println(F("mA"));
278-
dataFile.close();
279-
//Serial.println(F(" logfile closed"));
292+
dataFile.close();
280293
}
281294

282295
return FileOpenSuccess;

0 commit comments

Comments
 (0)