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:
Tue Oct 04 08:21:19 2022 +0000
Revision:
6:a5886100eeeb
Parent:
5:58208433fe78
Child:
7:0b422690f413
This time using the D2 for the OLED VCCEN and D3 for the OLED PMODEN. Moved OLED RES to A6. This helps using the L432KC with a (modified) GROVE NANO board.

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 6:a5886100eeeb 16 * Adafruit_SSD1331_Mbed, select the version "Timo Karppine" which is modified for OS6.nn
timo_k2 6:a5886100eeeb 17 * Adafruit-GFX, select the version "Timo Karppine" which is modified for OS6.nn
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 6:a5886100eeeb 26 * L432KC D2 - OLED 9 VCCEN Enable, or connect to VCC
timo_k2 6:a5886100eeeb 27 * L432KC D3 - OLED10 PMODEN Pmod Enable, or connect to 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 6:a5886100eeeb 34 * Updated for OS67, ... OS6.15 compiles OK.
timo_k2 6:a5886100eeeb 35 * Timo Karppinen 3.10.2022 SPDX-License-Identifier: Apache-2.0
timo_k2 0:9f57fa937a86 36 **************************************************************/
timo_k2 0:9f57fa937a86 37
timo_k2 0:9f57fa937a86 38 #include "mbed.h"
timo_k2 0:9f57fa937a86 39 #include "Adafruit_SSD1331.h" // By using the Adafruit SSD1331 library and Adafruit GFX library
timo_k2 1:de95c9ab922e 40 #include "Adafruit_GFX.h" // we will get similar code working than in Arduino-boards.
timo_k2 0:9f57fa937a86 41 // There are other SSD1331 libraries, too.
timo_k2 0:9f57fa937a86 42 // https://os.mbed.com/search/?q=ssd1331
timo_k2 0:9f57fa937a86 43 // The tested are:
timo_k2 1:de95c9ab922e 44 // Adafruit_SSD1331_MbedOS6 by Timo Karppinen
timo_k2 1:de95c9ab922e 45 // Adafruit-GFX-MbedOS6 by Timo Karppinen
timo_k2 0:9f57fa937a86 46 // PmodOLEDrgb
timo_k2 6:a5886100eeeb 47 Adafruit_SSD1331 OLED(A7, A6, D10, D11, NC, D13); // cs, res, dc, mosi, (nc), sck
timo_k2 0:9f57fa937a86 48
timo_k2 3:64fb84822c90 49
timo_k2 4:b3559f0f6072 50 DigitalOut LED(D1); // LED1, LED2, LED3 and LED4 are the D13 PB_3 pin in this board and
timo_k2 1:de95c9ab922e 51 //can not be used as a LED because of the SPI
timo_k2 6:a5886100eeeb 52 DigitalOut VCCEN(D2);
timo_k2 6:a5886100eeeb 53 DigitalOut PMODEN(D3);
timo_k2 0:9f57fa937a86 54
timo_k2 0:9f57fa937a86 55 // Definition of colours on the OLED display
timo_k2 0:9f57fa937a86 56 #define Black 0x0000
timo_k2 0:9f57fa937a86 57 #define Blue 0x001F
timo_k2 0:9f57fa937a86 58 #define Red 0xF800
timo_k2 0:9f57fa937a86 59 #define Green 0x07E0
timo_k2 0:9f57fa937a86 60 #define Cyan 0x07FF
timo_k2 0:9f57fa937a86 61 #define Magenta 0xF81F
timo_k2 0:9f57fa937a86 62 #define Yellow 0xFFE0
timo_k2 0:9f57fa937a86 63 #define White 0xFFFF
timo_k2 0:9f57fa937a86 64
timo_k2 3:64fb84822c90 65 float ADCScaledF = 0; // 32 bit floating point
timo_k2 3:64fb84822c90 66 int volt = 0;
timo_k2 3:64fb84822c90 67 int millivolt = 0;
timo_k2 3:64fb84822c90 68 void getADC();
timo_k2 0:9f57fa937a86 69
timo_k2 4:b3559f0f6072 70 char Time[32];
timo_k2 4:b3559f0f6072 71
timo_k2 0:9f57fa937a86 72 void getTime();
timo_k2 0:9f57fa937a86 73 int first = 0;
timo_k2 0:9f57fa937a86 74
timo_k2 0:9f57fa937a86 75 int main()
timo_k2 0:9f57fa937a86 76 {
timo_k2 0:9f57fa937a86 77 // Showing with a LED that program has started
timo_k2 4:b3559f0f6072 78 LED = 0;
timo_k2 0:9f57fa937a86 79 VCCEN = 1; // if you did not connect VCCEN permanently to Vcc
timo_k2 0:9f57fa937a86 80 PMODEN = 1; // if you did not connect PMODEN permanently to Vcc
timo_k2 0:9f57fa937a86 81 ThisThread::sleep_for(2000ms);
timo_k2 4:b3559f0f6072 82 LED = 1;
timo_k2 0:9f57fa937a86 83 ThisThread::sleep_for(2000ms);
timo_k2 4:b3559f0f6072 84 LED = 0;
timo_k2 4:b3559f0f6072 85 // Setting the time
timo_k2 5:58208433fe78 86 set_time(1614069522); // Set RTC time to " https://www.epochconverter.com/ "
timo_k2 1:de95c9ab922e 87 // Initalize the PmodOLEDrgb, the library includes SPI and there is
timo_k2 1:de95c9ab922e 88 // no need to initialize the SPI in the main.cpp
timo_k2 0:9f57fa937a86 89 OLED.begin(); // initialization of display object
timo_k2 0:9f57fa937a86 90 OLED.clearScreen();
timo_k2 0:9f57fa937a86 91
timo_k2 0:9f57fa937a86 92 while (true) {
timo_k2 0:9f57fa937a86 93 while(first < 3)
timo_k2 0:9f57fa937a86 94 {
timo_k2 0:9f57fa937a86 95 first += 1;
timo_k2 0:9f57fa937a86 96 OLED.fillScreen(Black); // background screen in black
timo_k2 0:9f57fa937a86 97 OLED.setTextColor(Cyan); // colour of text in cyan
timo_k2 0:9f57fa937a86 98 OLED.setCursor(0,0); // cursor is in x=0 and y=15
timo_k2 0:9f57fa937a86 99 OLED.printf("Test module Pmod"); // display text
timo_k2 0:9f57fa937a86 100 ThisThread::sleep_for(500ms); // wait 500 ms
timo_k2 0:9f57fa937a86 101 OLED.setCursor(0,15); // cursor is in x=0 and y=15
timo_k2 0:9f57fa937a86 102 OLED.setTextSize(2); // size of text
timo_k2 0:9f57fa937a86 103 OLED.setTextColor(Red); // text in red colour
timo_k2 0:9f57fa937a86 104 OLED.printf("DIGILENT"); // display text
timo_k2 0:9f57fa937a86 105 OLED.setCursor(20,40); // cursor is in x=20 and y=40
timo_k2 0:9f57fa937a86 106 OLED.setTextSize(1); // size of text
timo_k2 0:9f57fa937a86 107 OLED.setTextColor(Green); // text in green colour
timo_k2 0:9f57fa937a86 108 OLED.printf("LEXTRONIC"); // display text
timo_k2 0:9f57fa937a86 109 OLED.drawFastHLine(1, 60, OLED.width()-1, Blue); //blue line x=1, width-1 and y=60
timo_k2 0:9f57fa937a86 110 ThisThread::sleep_for(2s); // wait 2 s
timo_k2 0:9f57fa937a86 111 OLED.fillScreen(Black); // background display in black (erase display)
timo_k2 0:9f57fa937a86 112 OLED.fillRoundRect(5, 5, 30, 40, 1, Blue); // French flag bleu blanc rouge
timo_k2 0:9f57fa937a86 113 OLED.fillRoundRect(35, 5, 30, 40, 1, White);
timo_k2 0:9f57fa937a86 114 OLED.fillRoundRect(65, 5, 30, 40, 1, Red);
timo_k2 0:9f57fa937a86 115 OLED.fillCircle(90, 55, 5, Yellow); // yellow circle with radius=5 in x=90 and y=55
timo_k2 4:b3559f0f6072 116 ThisThread::sleep_for(1s); // wait 1 s
timo_k2 0:9f57fa937a86 117 }
timo_k2 0:9f57fa937a86 118
timo_k2 4:b3559f0f6072 119
timo_k2 4:b3559f0f6072 120 //ThisThread::sleep_for(300ms);
timo_k2 0:9f57fa937a86 121 OLED.clearScreen();
timo_k2 4:b3559f0f6072 122 OLED.fillScreen(Blue); // background screen in blue
timo_k2 0:9f57fa937a86 123 OLED.setTextColor(Cyan); // colour of text in cyan
timo_k2 0:9f57fa937a86 124 OLED.setCursor(0,0); // cursor is in x=0 and y=0
timo_k2 4:b3559f0f6072 125 OLED.printf("Rec");
timo_k2 4:b3559f0f6072 126 getTime();
timo_k2 4:b3559f0f6072 127 OLED.printf(" at %s \n",Time);
timo_k2 3:64fb84822c90 128 getADC();
timo_k2 3:64fb84822c90 129 OLED.printf("VOLT :%d.%03d\r\n", volt, millivolt);
timo_k2 0:9f57fa937a86 130
timo_k2 4:b3559f0f6072 131 if (ADCScaledF > 2.70)
timo_k2 0:9f57fa937a86 132 {
timo_k2 0:9f57fa937a86 133 OLED.setTextColor(Yellow);
timo_k2 4:b3559f0f6072 134 OLED.printf("Be aware of reaching the limit\n");
timo_k2 4:b3559f0f6072 135 OLED.drawFastHLine(10, 43, 50, Yellow);
timo_k2 0:9f57fa937a86 136 }
timo_k2 4:b3559f0f6072 137 OLED.setCursor(0,46); // cursor is in x=0 and y=40
timo_k2 0:9f57fa937a86 138 OLED.printf("Wait a moment !");
timo_k2 4:b3559f0f6072 139 OLED.drawFastHLine(1, OLED.height()-1, OLED.width()-1, Red); //RED line x=1 to screen width-1 and
timo_k2 4:b3559f0f6072 140 // y=screen height-1
timo_k2 6:a5886100eeeb 141 //printf("printed on OLED\n\n"); // OLED.printf() is using the same resource!!
timo_k2 0:9f57fa937a86 142
timo_k2 4:b3559f0f6072 143 ThisThread::sleep_for(1000ms);
timo_k2 0:9f57fa937a86 144 }
timo_k2 0:9f57fa937a86 145 }
timo_k2 0:9f57fa937a86 146
timo_k2 0:9f57fa937a86 147 void getTime()
timo_k2 0:9f57fa937a86 148 {
timo_k2 4:b3559f0f6072 149 time_t seconds = time(NULL); // https://os.mbed.com/docs/mbed-os/v6.7/apis/time.html
timo_k2 4:b3559f0f6072 150 strftime(Time,32,"%I:%M:%p\n", localtime(&seconds));
timo_k2 4:b3559f0f6072 151 //printf("Recorded :%s \r\n", Time); // in mbed OS 6.7 interferes with OLED.printf !!!
timo_k2 4:b3559f0f6072 152
timo_k2 0:9f57fa937a86 153 }
timo_k2 0:9f57fa937a86 154
timo_k2 3:64fb84822c90 155 void getADC()
timo_k2 0:9f57fa937a86 156 {
timo_k2 4:b3559f0f6072 157 static int count = 200; // value like it would be from a 12 bit Analog to Digital Converter
timo_k2 0:9f57fa937a86 158
timo_k2 4:b3559f0f6072 159 count = count + 82;
timo_k2 0:9f57fa937a86 160 if (count > 4095){
timo_k2 3:64fb84822c90 161 count = 200;
timo_k2 0:9f57fa937a86 162 }
timo_k2 0:9f57fa937a86 163
timo_k2 4:b3559f0f6072 164 ADCScaledF = (float(count))*(float(3.3)/4095); // The 0.00 V to 3.30 V corresponds 32 bit floating point.
timo_k2 3:64fb84822c90 165 volt = (int)ADCScaledF; // The OS6 does not include floating point printf !!!
timo_k2 3:64fb84822c90 166 millivolt = ((int)(ADCScaledF*1000))% 1000; // Millivolts counted with c++ modulus operator
timo_k2 4:b3559f0f6072 167 //printf("ADC :%d \r\n",count); // in mbed OS 6.7 interferes with OLED.printf !!!
timo_k2 4:b3559f0f6072 168 //printf("VOLT :%d.%03d\r\n", volt, millivolt); // millivolts with preceeding zeros and three digits
timo_k2 0:9f57fa937a86 169 ThisThread::sleep_for(100ms);
timo_k2 0:9f57fa937a86 170 }