IoT-MBBR (masterthesis)
Masterthesis upgrading a Moving Bed BioReactor with extra sensors and cloud connectivity.
serlcd.c File Reference

Sparkfun serLCD functionality. More...

#include <stdint.h>
#include <stdbool.h>
#include "em_device.h"
#include "em_cmu.h"
#include "em_gpio.h"
#include "em_i2c.h"
#include "serlcd.h"
#include "debug_dbprint.h"
#include "pin_mapping.h"
#include "i2c.h"

Go to the source code of this file.

Macros

#define DISPLAY_ADDRESS   0x72 /* This is the default address of the OpenLCD (7 bit notation) */
 
#define SPECIAL_COMMAND   254 /* Magic number for sending a special command */
 
#define SETTING_COMMAND   0x7C /* 124, |, the pipe character: The command to change settings: baud, lines, width, backlight, splash, etc */
 
#define CLEAR_COMMAND   0x2D
 
#define CONTRAST_COMMAND   0x18
 
#define ADDRESS_COMMAND   0x19
 
#define SET_RGB_COMMAND   0x2B
 
#define ENABLE_SYSTEM_MESSAGE_DISPLAY   0x2E
 
#define DISABLE_SYSTEM_MESSAGE_DISPLAY   0x2F
 
#define ENABLE_SPLASH_DISPLAY   0x30
 
#define DISABLE_SPLASH_DISPLAY   0x31
 
#define SAVE_CURRENT_DISPLAY_AS_SPLASH   0x0A
 
#define LCD_RETURNHOME   0x02
 
#define LCD_ENTRYMODESET   0x04
 
#define LCD_DISPLAYCONTROL   0x08
 
#define LCD_CURSORSHIFT   0x10
 
#define LCD_SETDDRAMADDR   0x80
 
#define LCD_ENTRYRIGHT   0x00
 
#define LCD_ENTRYLEFT   0x02
 
#define LCD_ENTRYSHIFTINCREMENT   0x01
 
#define LCD_ENTRYSHIFTDECREMENT   0x00
 
#define LCD_DISPLAYON   0x04
 
#define LCD_DISPLAYOFF   0x00
 
#define LCD_CURSORON   0x02
 
#define LCD_CURSOROFF   0x00
 
#define LCD_BLINKON   0x01
 
#define LCD_BLINKOFF   0x00
 
#define LCD_DISPLAYMOVE   0x08
 
#define LCD_CURSORMOVE   0x00
 
#define LCD_MOVERIGHT   0x04
 
#define LCD_MOVELEFT   0x00
 

Functions

int8_t clearLCD (void)
 Clear the LCD. More...
 
int8_t writeCharLCD (uint8_t data)
 Write a character (one byte) to the LCD. More...
 
int8_t writeStringLCD (char *message)
 Print a string (char array) to the LCD. More...
 
int8_t setCursorLCD (uint8_t col, uint8_t row)
 Set the cursor to a specific location on the LCD. More...
 
int8_t heartBeatLCD (void)
 Update the heartbeat icon. More...
 

Variables

bool heartBeat = false
 

Detailed Description

Sparkfun serLCD functionality.

Version
1.0
Author
Brecht Van Eeckhoudt

Versions

  • v1.0: Initial version.

License

Copyright (C) 2020 - Brecht Van Eeckhoudt

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

A copy of the GNU General Public License can be found in the LICENSE file along with this source code.


Some methods use code obtained from examples from Silicon Labs' GitHub. These sections are licensed under the Silabs License Agreement. See the file "Silabs_License_Agreement.txt" for details. Before using this software for any purpose, you must agree to the terms of that agreement.


Code based on: https://github.com/sparkfun/SparkFun_SerLCD_Arduino_Library/blob/master/src/SerLCD.cpp


Command cheat sheet:

ASCII / DEC / HEX '|' / 124 / 0x7C - Put into setting mode Ctrl+c / 3 / 0x03 - Change width to 20 Ctrl+d / 4 / 0x04 - Change width to 16 Ctrl+e / 5 / 0x05 - Change lines to 4 Ctrl+f / 6 / 0x06 - Change lines to 2 Ctrl+g / 7 / 0x07 - Change lines to 1 Ctrl+h / 8 / 0x08 - Software reset of the system Ctrl+i / 9 / 0x09 - Enable/disable splash screen Ctrl+j / 10 / 0x0A - Save currently displayed text as splash Ctrl+k / 11 / 0x0B - Change baud to 2400bps Ctrl+l / 12 / 0x0C - Change baud to 4800bps Ctrl+m / 13 / 0x0D - Change baud to 9600bps Ctrl+n / 14 / 0x0E - Change baud to 14400bps Ctrl+o / 15 / 0x0F - Change baud to 19200bps Ctrl+p / 16 / 0x10 - Change baud to 38400bps Ctrl+q / 17 / 0x11 - Change baud to 57600bps Ctrl+r / 18 / 0x12 - Change baud to 115200bps Ctrl+s / 19 / 0x13 - Change baud to 230400bps Ctrl+t / 20 / 0x14 - Change baud to 460800bps Ctrl+u / 21 / 0x15 - Change baud to 921600bps Ctrl+v / 22 / 0x16 - Change baud to 1000000bps Ctrl+w / 23 / 0x17 - Change baud to 1200bps Ctrl+x / 24 / 0x18 - Change the contrast. Follow Ctrl+x with number 0 to 255. 120 is default. Ctrl+y / 25 / 0x19 - Change the TWI address. Follow Ctrl+x with number 0 to 255. 114 (0x72) is default. Ctrl+z / 26 / 0x1A - Enable/disable ignore RX pin on startup (ignore emergency reset) '+' / 43 / 0x2B - Set RGB backlight with three following bytes, 0-255 ',' / 44 / 0x2C - Display current firmware version '-' / 45 / 0x2D - Clear display. Move cursor to home position. '.' / 46 / 0x2E - Enable system messages (ie, display 'Contrast: 5' when changed) '/' / 47 / 0x2F - Disable system messages (ie, don't display 'Contrast: 5' when changed) '0' / 48 / 0x30 - Enable splash screen '1' / 49 / 0x31 - Disable splash screen / 128-157 / 0x80-0x9D - Set the primary backlight brightness. 128 = Off, 157 = 100%. / 158-187 / 0x9E-0xBB - Set the green backlight brightness. 158 = Off, 187 = 100%. / 188-217 / 0xBC-0xD9 - Set the blue backlight brightness. 188 = Off, 217 = 100%. For example, to change the baud rate to 115200 send 124 followed by 18.

Definition in file serlcd.c.

Macro Definition Documentation

◆ ADDRESS_COMMAND

#define ADDRESS_COMMAND   0x19

Definition at line 111 of file serlcd.c.

◆ CLEAR_COMMAND

#define CLEAR_COMMAND   0x2D

Definition at line 109 of file serlcd.c.

◆ CONTRAST_COMMAND

#define CONTRAST_COMMAND   0x18

Definition at line 110 of file serlcd.c.

◆ DISABLE_SPLASH_DISPLAY

#define DISABLE_SPLASH_DISPLAY   0x31

Definition at line 116 of file serlcd.c.

◆ DISABLE_SYSTEM_MESSAGE_DISPLAY

#define DISABLE_SYSTEM_MESSAGE_DISPLAY   0x2F

Definition at line 114 of file serlcd.c.

◆ DISPLAY_ADDRESS

#define DISPLAY_ADDRESS   0x72 /* This is the default address of the OpenLCD (7 bit notation) */

Definition at line 102 of file serlcd.c.

◆ ENABLE_SPLASH_DISPLAY

#define ENABLE_SPLASH_DISPLAY   0x30

Definition at line 115 of file serlcd.c.

◆ ENABLE_SYSTEM_MESSAGE_DISPLAY

#define ENABLE_SYSTEM_MESSAGE_DISPLAY   0x2E

Definition at line 113 of file serlcd.c.

◆ LCD_BLINKOFF

#define LCD_BLINKOFF   0x00

Definition at line 138 of file serlcd.c.

◆ LCD_BLINKON

#define LCD_BLINKON   0x01

Definition at line 137 of file serlcd.c.

◆ LCD_CURSORMOVE

#define LCD_CURSORMOVE   0x00

Definition at line 142 of file serlcd.c.

◆ LCD_CURSOROFF

#define LCD_CURSOROFF   0x00

Definition at line 136 of file serlcd.c.

◆ LCD_CURSORON

#define LCD_CURSORON   0x02

Definition at line 135 of file serlcd.c.

◆ LCD_CURSORSHIFT

#define LCD_CURSORSHIFT   0x10

Definition at line 123 of file serlcd.c.

◆ LCD_DISPLAYCONTROL

#define LCD_DISPLAYCONTROL   0x08

Definition at line 122 of file serlcd.c.

◆ LCD_DISPLAYMOVE

#define LCD_DISPLAYMOVE   0x08

Definition at line 141 of file serlcd.c.

◆ LCD_DISPLAYOFF

#define LCD_DISPLAYOFF   0x00

Definition at line 134 of file serlcd.c.

◆ LCD_DISPLAYON

#define LCD_DISPLAYON   0x04

Definition at line 133 of file serlcd.c.

◆ LCD_ENTRYLEFT

#define LCD_ENTRYLEFT   0x02

Definition at line 128 of file serlcd.c.

◆ LCD_ENTRYMODESET

#define LCD_ENTRYMODESET   0x04

Definition at line 121 of file serlcd.c.

◆ LCD_ENTRYRIGHT

#define LCD_ENTRYRIGHT   0x00

Definition at line 127 of file serlcd.c.

◆ LCD_ENTRYSHIFTDECREMENT

#define LCD_ENTRYSHIFTDECREMENT   0x00

Definition at line 130 of file serlcd.c.

◆ LCD_ENTRYSHIFTINCREMENT

#define LCD_ENTRYSHIFTINCREMENT   0x01

Definition at line 129 of file serlcd.c.

◆ LCD_MOVELEFT

#define LCD_MOVELEFT   0x00

Definition at line 144 of file serlcd.c.

◆ LCD_MOVERIGHT

#define LCD_MOVERIGHT   0x04

Definition at line 143 of file serlcd.c.

◆ LCD_RETURNHOME

#define LCD_RETURNHOME   0x02

Definition at line 120 of file serlcd.c.

◆ LCD_SETDDRAMADDR

#define LCD_SETDDRAMADDR   0x80

Definition at line 124 of file serlcd.c.

◆ SAVE_CURRENT_DISPLAY_AS_SPLASH

#define SAVE_CURRENT_DISPLAY_AS_SPLASH   0x0A

Definition at line 117 of file serlcd.c.

◆ SET_RGB_COMMAND

#define SET_RGB_COMMAND   0x2B

Definition at line 112 of file serlcd.c.

◆ SETTING_COMMAND

#define SETTING_COMMAND   0x7C /* 124, |, the pipe character: The command to change settings: baud, lines, width, backlight, splash, etc */

Definition at line 106 of file serlcd.c.

◆ SPECIAL_COMMAND

#define SPECIAL_COMMAND   254 /* Magic number for sending a special command */

Definition at line 105 of file serlcd.c.

Function Documentation

◆ clearLCD()

int8_t clearLCD ( void  )

Clear the LCD.

This method also initializes i2C if necessary.

Returns
  • 0 - Byte written correctly.
  • -1 - i2cTransferNack, NACK received during transfer.
  • -2 - i2cTransferBusErr, Bus error during transfer (misplaced START/STOP).
  • -3 - i2cTransferArbLost, Arbitration lost during transfer.
  • -4 - i2cTransferUsageFault, Usage fault.
  • -5 - i2cTransferSwFault, SW fault.
  • -6 - Coudldn't configure I2C.

Definition at line 168 of file serlcd.c.

169 {
170  /* Initialize I2C if not already the case */
171  if (initI2C() != 0) return (-6);
172 
173  /* Put commands in array */
174  uint8_t data_buf[2] = {SETTING_COMMAND, CLEAR_COMMAND};
175 
176  /* Transfer structure */
177  I2C_TransferSeq_TypeDef i2cTransfer;
178  I2C_TransferReturn_TypeDef result;
179 
180  /* Initializing I2C transfer */
181  i2cTransfer.addr = DISPLAY_ADDRESS << 1; /* Left shift so we go to 8 bit notation instead of 7 */
182  i2cTransfer.flags = I2C_FLAG_WRITE; /* Data written from buf[0].data */
183  i2cTransfer.buf[0].data = data_buf;
184  i2cTransfer.buf[0].len = 2; /* Data is two bytes */
185  result = I2C_TransferInit(BUS_I2C, &i2cTransfer);
186 
187  /* Transmitting I2C data (busy-wait until transfer is complete) */
188  while (result == i2cTransferInProgress) result = I2C_Transfer(BUS_I2C);
189 
190  return (result);
191 }
#define SETTING_COMMAND
Definition: serlcd.c:106
#define BUS_I2C
Definition: pin_mapping.h:53
#define DISPLAY_ADDRESS
Definition: serlcd.c:102
int8_t initI2C(void)
I2C initialization (if not already initialized).
Definition: i2c.c:66
#define CLEAR_COMMAND
Definition: serlcd.c:109
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98

◆ heartBeatLCD()

int8_t heartBeatLCD ( void  )

Update the heartbeat icon.

This method also initializes i2C if necessary.

Returns
  • 0 - Byte written correctly.
  • -1 - i2cTransferNack, NACK received during transfer.
  • -2 - i2cTransferBusErr, Bus error during transfer (misplaced START/STOP).
  • -3 - i2cTransferArbLost, Arbitration lost during transfer.
  • -4 - i2cTransferUsageFault, Usage fault.
  • -5 - i2cTransferSwFault, SW fault.
  • -6 - Coudldn't configure I2C.

Definition at line 361 of file serlcd.c.

362 {
363  /* Initialize I2C if not already the case */
364  if (initI2C() != 0) return (-6);
365 
366  /* Separate data in bytes */
367  uint8_t data_buf[3] = {254, LCD_SETDDRAMADDR | 19 }; /* 19th position on first row */
368 
369  /* Fill last location of data_buf according to heartBeat status */
370  if (heartBeat) data_buf[2] = '+';
371  else data_buf[2] = '*';
372  heartBeat = !heartBeat;
373 
374  /* Transfer structure */
375  I2C_TransferSeq_TypeDef i2cTransfer;
376  I2C_TransferReturn_TypeDef result;
377 
378  /* Initializing I2C transfer */
379  i2cTransfer.addr = DISPLAY_ADDRESS << 1; /* Left shift so we go to 8 bit notation instead of 7 */
380  i2cTransfer.flags = I2C_FLAG_WRITE; /* Data written from buf[0].data */
381  i2cTransfer.buf[0].data = data_buf;
382  i2cTransfer.buf[0].len = 3; /* Data is two bytes */
383  result = I2C_TransferInit(BUS_I2C, &i2cTransfer);
384 
385  /* Transmitting I2C data (busy-wait until transfer is complete) */
386  while (result == i2cTransferInProgress) result = I2C_Transfer(BUS_I2C);
387 
388  return (result);
389 }
#define LCD_SETDDRAMADDR
Definition: serlcd.c:124
#define BUS_I2C
Definition: pin_mapping.h:53
#define DISPLAY_ADDRESS
Definition: serlcd.c:102
bool heartBeat
Definition: serlcd.c:148
int8_t initI2C(void)
I2C initialization (if not already initialized).
Definition: i2c.c:66
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98

◆ setCursorLCD()

int8_t setCursorLCD ( uint8_t  col,
uint8_t  row 
)

Set the cursor to a specific location on the LCD.

This method also initializes i2C if necessary.

Parameters
[in]colColumn (uint8_t, MAX = 20).
[in]rowRow (uint8_t, MAX = 4).
Returns
  • 0 - Byte written correctly.
  • -1 - i2cTransferNack, NACK received during transfer.
  • -2 - i2cTransferBusErr, Bus error during transfer (misplaced START/STOP).
  • -3 - i2cTransferArbLost, Arbitration lost during transfer.
  • -4 - i2cTransferUsageFault, Usage fault.
  • -5 - i2cTransferSwFault, SW fault.
  • -6 - Coudldn't configure I2C.

Definition at line 316 of file serlcd.c.

317 {
318  uint8_t row_offsets[] = {0x00, 0x40, 0x14, 0x54};
319 
320  /* Initialize I2C if not already the case */
321  if (initI2C() != 0) return (-6);
322 
323  /* Separate data in bytes */
324  uint8_t data_buf[2] = {254, LCD_SETDDRAMADDR | (col-1 + row_offsets[row-1])};
325 
326  /* Transfer structure */
327  I2C_TransferSeq_TypeDef i2cTransfer;
328  I2C_TransferReturn_TypeDef result;
329 
330  /* Initializing I2C transfer */
331  i2cTransfer.addr = DISPLAY_ADDRESS << 1; /* Left shift so we go to 8 bit notation instead of 7 */
332  i2cTransfer.flags = I2C_FLAG_WRITE; /* Data written from buf[0].data */
333  i2cTransfer.buf[0].data = data_buf;
334  i2cTransfer.buf[0].len = 2; /* Data is two bytes */
335  result = I2C_TransferInit(BUS_I2C, &i2cTransfer);
336 
337  /* Transmitting I2C data (busy-wait until transfer is complete) */
338  while (result == i2cTransferInProgress) result = I2C_Transfer(BUS_I2C);
339 
340  return (result);
341 }
#define LCD_SETDDRAMADDR
Definition: serlcd.c:124
#define BUS_I2C
Definition: pin_mapping.h:53
#define DISPLAY_ADDRESS
Definition: serlcd.c:102
int8_t initI2C(void)
I2C initialization (if not already initialized).
Definition: i2c.c:66
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98

◆ writeCharLCD()

int8_t writeCharLCD ( uint8_t  data)

Write a character (one byte) to the LCD.

This method also initializes i2C if necessary.

Parameters
[in]dataThe data (uint8_t) to put on the I2C bus.
Returns
  • 0 - Byte written correctly.
  • -1 - i2cTransferNack, NACK received during transfer.
  • -2 - i2cTransferBusErr, Bus error during transfer (misplaced START/STOP).
  • -3 - i2cTransferArbLost, Arbitration lost during transfer.
  • -4 - i2cTransferUsageFault, Usage fault.
  • -5 - i2cTransferSwFault, SW fault.
  • -6 - Coudldn't configure I2C.

Definition at line 214 of file serlcd.c.

215 {
216  /* Initialize I2C if not already the case */
217  if (initI2C() != 0) return (-6);
218 
219  /* Separate data in bytes */
220  uint8_t data_buf[1] = {data};
221 
222  /* Transfer structure */
223  I2C_TransferSeq_TypeDef i2cTransfer;
224  I2C_TransferReturn_TypeDef result;
225 
226  /* Initializing I2C transfer */
227  i2cTransfer.addr = DISPLAY_ADDRESS << 1; /* Left shift so we go to 8 bit notation instead of 7 */
228  i2cTransfer.flags = I2C_FLAG_WRITE; /* Data written from buf[0].data */
229  i2cTransfer.buf[0].data = data_buf;
230  i2cTransfer.buf[0].len = 1; /* Data is one byte */
231  result = I2C_TransferInit(BUS_I2C, &i2cTransfer);
232 
233  /* Transmitting I2C data (busy-wait until transfer is complete) */
234  while (result == i2cTransferInProgress) result = I2C_Transfer(BUS_I2C);
235 
236  return (result);
237 }
MeasurementData_t data
Definition: main.c:86
#define BUS_I2C
Definition: pin_mapping.h:53
#define DISPLAY_ADDRESS
Definition: serlcd.c:102
int8_t initI2C(void)
I2C initialization (if not already initialized).
Definition: i2c.c:66
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98

◆ writeStringLCD()

int8_t writeStringLCD ( char *  message)

Print a string (char array) to the LCD.

Note
If the input is not a string (ex.: "Hello world!") but a char array, the input message (array) needs to end with NULL ("\0")!

This method also initializes i2C if necessary.

Parameters
[in]messageThe string to print to the LCD.
Returns
  • 0 - Byte written correctly.
  • -1 - i2cTransferNack, NACK received during transfer.
  • -2 - i2cTransferBusErr, Bus error during transfer (misplaced START/STOP).
  • -3 - i2cTransferArbLost, Arbitration lost during transfer.
  • -4 - i2cTransferUsageFault, Usage fault.
  • -5 - i2cTransferSwFault, SW fault.
  • -6 - Coudldn't configure I2C.

Definition at line 264 of file serlcd.c.

265 {
266  /* Initialize I2C if not already the case */
267  if (initI2C() != 0) return (-6);
268 
269  /* Check string length */
270  uint8_t length = 0;
271 
272  /* Count up until we reach a NULL */
273  while (message[length] != '\0') length++;
274 
275  /* Transfer structure */
276  I2C_TransferSeq_TypeDef i2cTransfer;
277  I2C_TransferReturn_TypeDef result;
278 
279  /* Initializing I2C transfer */
280  i2cTransfer.addr = DISPLAY_ADDRESS << 1; /* Left shift so we go to 8 bit notation instead of 7 */
281  i2cTransfer.flags = I2C_FLAG_WRITE; /* Data written from buf[0].data */
282  i2cTransfer.buf[0].data = (unsigned char *)message;
283  i2cTransfer.buf[0].len = length; /* Data is one byte */
284  result = I2C_TransferInit(BUS_I2C, &i2cTransfer);
285 
286  /* Transmitting I2C data (busy-wait until transfer is complete) */
287  while (result == i2cTransferInProgress) result = I2C_Transfer(BUS_I2C);
288 
289  return (result);
290 }
#define BUS_I2C
Definition: pin_mapping.h:53
#define DISPLAY_ADDRESS
Definition: serlcd.c:102
int8_t initI2C(void)
I2C initialization (if not already initialized).
Definition: i2c.c:66
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98

Variable Documentation

◆ heartBeat

bool heartBeat = false

Definition at line 148 of file serlcd.c.