The code is developed for the hardware NUCLEO-L432KC and Digilent Pmod OLEDrgb. The purpose is to make students learn the development process and test that the hardware works fine. For converting this to Mbed OS 6 Adafruit_GFX.h needed to add #include "Stream.h" in beginning of the Adafruit_GFX.h Adafruit_SSD1331.cpp wait_ms(200) replaced with ThisThread::sleep_for(200ms); for all the similar wait_ms lines.

Dependencies:   Adafruit-GFX-MbedOS6

Committer:
timo_k2
Date:
Mon Feb 22 18:03:21 2021 +0000
Revision:
4:b3559f0f6072
Parent:
3:64fb84822c90
Child:
5:58208433fe78
updated for the OS 6.7

Who changed what in which revision?

UserRevisionLine numberNew contents of line
timo_k2 0:9f57fa937a86 1 /* mbed Microcontroller Library
timo_k2 0:9f57fa937a86 2 * Copyright (c) 2019 ARM Limited
timo_k2 0:9f57fa937a86 3 * SPDX-License-Identifier: Apache-2.0
timo_k2 0:9f57fa937a86 4 ************************************************************************
timo_k2 0:9f57fa937a86 5 *
timo_k2 0:9f57fa937a86 6 * Commissioning test for the L432KC and the Pmod small OLED display
timo_k2 0:9f57fa937a86 7 *
timo_k2 0:9f57fa937a86 8 *************************************************************************
timo_k2 1:de95c9ab922e 9 * Description: McLab10_OLEDrgb_L432KC_OS60_tk2
timo_k2 0:9f57fa937a86 10 * "Test module Pmod Digilent Lextronic" will be displayed on OLEDrgb module
timo_k2 0:9f57fa937a86 11 * with different size and colors..... and later the variable values.
timo_k2 0:9f57fa937a86 12 *
timo_k2 0:9f57fa937a86 13 * Material
timo_k2 0:9f57fa937a86 14 * 1. ST L432KC ( or some other micro controller board with SPI communication )
timo_k2 0:9f57fa937a86 15 * 2. Digilent Pmod OLEDrgb and the libraries
timo_k2 0:9f57fa937a86 16 * Adafruit_SSD1331_Mbed
timo_k2 0:9f57fa937a86 17 * Adafruit-GFX
timo_k2 0:9f57fa937a86 18 * These libraries can be found with search on the page
timo_k2 0:9f57fa937a86 19 * https://www.mbed.com/en/platform/mbed-os/
timo_k2 0:9f57fa937a86 20 * Please connect L432KC - Pmod_OLEDrgb with lines:
timo_k2 0:9f57fa937a86 21 * L432KC D13 - OLED 4 SCK hardware defined for the SPI
timo_k2 0:9f57fa937a86 22 * L432KC D11 - OLED 2 MOSI hardware definef for the SPI
timo_k2 4:b3559f0f6072 23 * L432KC A7 - OLED 1 CS or any other free
timo_k2 0:9f57fa937a86 24 * L432KC D10 - OLED 7 DC or any other free
timo_k2 0:9f57fa937a86 25 * L432KC D6 - OLED 8 RES or any other free
timo_k2 0:9f57fa937a86 26 * VCC - OLED 9 VCCEN Enable - VCC
timo_k2 0:9f57fa937a86 27 * VCC - OLED10 PMODEN Pmod Enable - VCC
timo_k2 0:9f57fa937a86 28 * GND - OLED 5 Ground
timo_k2 2:29bb1ef0d6cd 29 * VCC - OLED 6 Power supply 3.3 V
timo_k2 2:29bb1ef0d6cd 30 * L432KC 3V3 - VCC L432 supplying 3.3 V
timo_k2 2:29bb1ef0d6cd 31 * L432KC GND - GND Ground
timo_k2 0:9f57fa937a86 32 *
timo_k2 0:9f57fa937a86 33 *************************************************************************
timo_k2 0:9f57fa937a86 34 * Compiles OK, works with hardware with operating system revision 6691:cf4f12a
timo_k2 0:9f57fa937a86 35 * Compiles OK, DOESN'T WORK with hardware with op sys revision 7088:3ab72c7
timo_k2 1:de95c9ab922e 36 * Updated for OS6, Compiles OK, works with hardware with os revision OS 7864:890f056
timo_k2 1:de95c9ab922e 37 * Timo Karppinen 29.08.2020 SPDX-License-Identifier: Apache-2.0
timo_k2 0:9f57fa937a86 38 **************************************************************/
timo_k2 0:9f57fa937a86 39
timo_k2 0:9f57fa937a86 40 #include "mbed.h"
timo_k2 0:9f57fa937a86 41 #include "Adafruit_SSD1331.h" // By using the Adafruit SSD1331 library and Adafruit GFX library
timo_k2 1:de95c9ab922e 42 #include "Adafruit_GFX.h" // we will get similar code working than in Arduino-boards.
timo_k2 0:9f57fa937a86 43 // There are other SSD1331 libraries, too.
timo_k2 0:9f57fa937a86 44 // https://os.mbed.com/search/?q=ssd1331
timo_k2 0:9f57fa937a86 45 // The tested are:
timo_k2 1:de95c9ab922e 46 // Adafruit_SSD1331_MbedOS6 by Timo Karppinen
timo_k2 1:de95c9ab922e 47 // Adafruit-GFX-MbedOS6 by Timo Karppinen
timo_k2 0:9f57fa937a86 48 // PmodOLEDrgb
timo_k2 4:b3559f0f6072 49 Adafruit_SSD1331 OLED(A7, D6, D10, D11, NC, D13); // cs, res, dc, mosi, (nc), sck
timo_k2 0:9f57fa937a86 50
timo_k2 3:64fb84822c90 51
timo_k2 4:b3559f0f6072 52 DigitalOut LED(D1); // LED1, LED2, LED3 and LED4 are the D13 PB_3 pin in this board and
timo_k2 1:de95c9ab922e 53 //can not be used as a LED because of the SPI
timo_k2 0:9f57fa937a86 54 DigitalOut VCCEN(D3);
timo_k2 0:9f57fa937a86 55 DigitalOut PMODEN(D5);
timo_k2 0:9f57fa937a86 56
timo_k2 0:9f57fa937a86 57 // Definition of colours on the OLED display
timo_k2 0:9f57fa937a86 58 #define Black 0x0000
timo_k2 0:9f57fa937a86 59 #define Blue 0x001F
timo_k2 0:9f57fa937a86 60 #define Red 0xF800
timo_k2 0:9f57fa937a86 61 #define Green 0x07E0
timo_k2 0:9f57fa937a86 62 #define Cyan 0x07FF
timo_k2 0:9f57fa937a86 63 #define Magenta 0xF81F
timo_k2 0:9f57fa937a86 64 #define Yellow 0xFFE0
timo_k2 0:9f57fa937a86 65 #define White 0xFFFF
timo_k2 0:9f57fa937a86 66
timo_k2 3:64fb84822c90 67 float ADCScaledF = 0; // 32 bit floating point
timo_k2 3:64fb84822c90 68 int volt = 0;
timo_k2 3:64fb84822c90 69 int millivolt = 0;
timo_k2 3:64fb84822c90 70 void getADC();
timo_k2 0:9f57fa937a86 71
timo_k2 4:b3559f0f6072 72 char Time[32];
timo_k2 4:b3559f0f6072 73
timo_k2 0:9f57fa937a86 74 void getTime();
timo_k2 0:9f57fa937a86 75 int first = 0;
timo_k2 0:9f57fa937a86 76
timo_k2 0:9f57fa937a86 77 int main()
timo_k2 0:9f57fa937a86 78 {
timo_k2 0:9f57fa937a86 79 // Showing with a LED that program has started
timo_k2 4:b3559f0f6072 80 LED = 0;
timo_k2 0:9f57fa937a86 81 VCCEN = 1; // if you did not connect VCCEN permanently to Vcc
timo_k2 0:9f57fa937a86 82 PMODEN = 1; // if you did not connect PMODEN permanently to Vcc
timo_k2 0:9f57fa937a86 83 ThisThread::sleep_for(2000ms);
timo_k2 4:b3559f0f6072 84 LED = 1;
timo_k2 0:9f57fa937a86 85 ThisThread::sleep_for(2000ms);
timo_k2 4:b3559f0f6072 86 LED = 0;
timo_k2 4:b3559f0f6072 87 // Setting the time
timo_k2 4:b3559f0f6072 88 set_time(1614012790); // Set RTC time to " https://www.epochconverter.com/ "
timo_k2 1:de95c9ab922e 89 // Initalize the PmodOLEDrgb, the library includes SPI and there is
timo_k2 1:de95c9ab922e 90 // no need to initialize the SPI in the main.cpp
timo_k2 0:9f57fa937a86 91 OLED.begin(); // initialization of display object
timo_k2 0:9f57fa937a86 92 OLED.clearScreen();
timo_k2 0:9f57fa937a86 93
timo_k2 0:9f57fa937a86 94 while (true) {
timo_k2 0:9f57fa937a86 95 while(first < 3)
timo_k2 0:9f57fa937a86 96 {
timo_k2 0:9f57fa937a86 97 first += 1;
timo_k2 0:9f57fa937a86 98 OLED.fillScreen(Black); // background screen in black
timo_k2 0:9f57fa937a86 99 OLED.setTextColor(Cyan); // colour of text in cyan
timo_k2 0:9f57fa937a86 100 OLED.setCursor(0,0); // cursor is in x=0 and y=15
timo_k2 0:9f57fa937a86 101 OLED.printf("Test module Pmod"); // display text
timo_k2 0:9f57fa937a86 102 ThisThread::sleep_for(500ms); // wait 500 ms
timo_k2 0:9f57fa937a86 103 OLED.setCursor(0,15); // cursor is in x=0 and y=15
timo_k2 0:9f57fa937a86 104 OLED.setTextSize(2); // size of text
timo_k2 0:9f57fa937a86 105 OLED.setTextColor(Red); // text in red colour
timo_k2 0:9f57fa937a86 106 OLED.printf("DIGILENT"); // display text
timo_k2 0:9f57fa937a86 107 OLED.setCursor(20,40); // cursor is in x=20 and y=40
timo_k2 0:9f57fa937a86 108 OLED.setTextSize(1); // size of text
timo_k2 0:9f57fa937a86 109 OLED.setTextColor(Green); // text in green colour
timo_k2 0:9f57fa937a86 110 OLED.printf("LEXTRONIC"); // display text
timo_k2 0:9f57fa937a86 111 OLED.drawFastHLine(1, 60, OLED.width()-1, Blue); //blue line x=1, width-1 and y=60
timo_k2 0:9f57fa937a86 112 ThisThread::sleep_for(2s); // wait 2 s
timo_k2 0:9f57fa937a86 113 OLED.fillScreen(Black); // background display in black (erase display)
timo_k2 0:9f57fa937a86 114 OLED.fillRoundRect(5, 5, 30, 40, 1, Blue); // French flag bleu blanc rouge
timo_k2 0:9f57fa937a86 115 OLED.fillRoundRect(35, 5, 30, 40, 1, White);
timo_k2 0:9f57fa937a86 116 OLED.fillRoundRect(65, 5, 30, 40, 1, Red);
timo_k2 0:9f57fa937a86 117 OLED.fillCircle(90, 55, 5, Yellow); // yellow circle with radius=5 in x=90 and y=55
timo_k2 4:b3559f0f6072 118 ThisThread::sleep_for(1s); // wait 1 s
timo_k2 0:9f57fa937a86 119 }
timo_k2 0:9f57fa937a86 120
timo_k2 4:b3559f0f6072 121
timo_k2 4:b3559f0f6072 122 //ThisThread::sleep_for(300ms);
timo_k2 0:9f57fa937a86 123 OLED.clearScreen();
timo_k2 4:b3559f0f6072 124 OLED.fillScreen(Blue); // background screen in blue
timo_k2 0:9f57fa937a86 125 OLED.setTextColor(Cyan); // colour of text in cyan
timo_k2 0:9f57fa937a86 126 OLED.setCursor(0,0); // cursor is in x=0 and y=0
timo_k2 4:b3559f0f6072 127 OLED.printf("Rec");
timo_k2 4:b3559f0f6072 128 getTime();
timo_k2 4:b3559f0f6072 129 OLED.printf(" at %s \n",Time);
timo_k2 3:64fb84822c90 130 getADC();
timo_k2 3:64fb84822c90 131 OLED.printf("VOLT :%d.%03d\r\n", volt, millivolt);
timo_k2 0:9f57fa937a86 132
timo_k2 4:b3559f0f6072 133 if (ADCScaledF > 2.70)
timo_k2 0:9f57fa937a86 134 {
timo_k2 0:9f57fa937a86 135 OLED.setTextColor(Yellow);
timo_k2 4:b3559f0f6072 136 OLED.printf("Be aware of reaching the limit\n");
timo_k2 4:b3559f0f6072 137 OLED.drawFastHLine(10, 43, 50, Yellow);
timo_k2 0:9f57fa937a86 138 }
timo_k2 4:b3559f0f6072 139 OLED.setCursor(0,46); // cursor is in x=0 and y=40
timo_k2 0:9f57fa937a86 140 OLED.printf("Wait a moment !");
timo_k2 4:b3559f0f6072 141 OLED.drawFastHLine(1, OLED.height()-1, OLED.width()-1, Red); //RED line x=1 to screen width-1 and
timo_k2 4:b3559f0f6072 142 // y=screen height-1
timo_k2 4:b3559f0f6072 143 //printf("printed on OLED\n\n");
timo_k2 0:9f57fa937a86 144
timo_k2 4:b3559f0f6072 145 ThisThread::sleep_for(1000ms);
timo_k2 0:9f57fa937a86 146 }
timo_k2 0:9f57fa937a86 147 }
timo_k2 0:9f57fa937a86 148
timo_k2 0:9f57fa937a86 149 void getTime()
timo_k2 0:9f57fa937a86 150 {
timo_k2 4:b3559f0f6072 151 time_t seconds = time(NULL); // https://os.mbed.com/docs/mbed-os/v6.7/apis/time.html
timo_k2 4:b3559f0f6072 152 strftime(Time,32,"%I:%M:%p\n", localtime(&seconds));
timo_k2 4:b3559f0f6072 153 //printf("Recorded :%s \r\n", Time); // in mbed OS 6.7 interferes with OLED.printf !!!
timo_k2 4:b3559f0f6072 154
timo_k2 0:9f57fa937a86 155 }
timo_k2 0:9f57fa937a86 156
timo_k2 3:64fb84822c90 157 void getADC()
timo_k2 0:9f57fa937a86 158 {
timo_k2 4:b3559f0f6072 159 static int count = 200; // value like it would be from a 12 bit Analog to Digital Converter
timo_k2 0:9f57fa937a86 160
timo_k2 4:b3559f0f6072 161 count = count + 82;
timo_k2 0:9f57fa937a86 162 if (count > 4095){
timo_k2 3:64fb84822c90 163 count = 200;
timo_k2 0:9f57fa937a86 164 }
timo_k2 0:9f57fa937a86 165
timo_k2 4:b3559f0f6072 166 ADCScaledF = (float(count))*(float(3.3)/4095); // The 0.00 V to 3.30 V corresponds 32 bit floating point.
timo_k2 3:64fb84822c90 167 volt = (int)ADCScaledF; // The OS6 does not include floating point printf !!!
timo_k2 3:64fb84822c90 168 millivolt = ((int)(ADCScaledF*1000))% 1000; // Millivolts counted with c++ modulus operator
timo_k2 4:b3559f0f6072 169 //printf("ADC :%d \r\n",count); // in mbed OS 6.7 interferes with OLED.printf !!!
timo_k2 4:b3559f0f6072 170 //printf("VOLT :%d.%03d\r\n", volt, millivolt); // millivolts with preceeding zeros and three digits
timo_k2 0:9f57fa937a86 171 ThisThread::sleep_for(100ms);
timo_k2 0:9f57fa937a86 172 }