The ST NUCLEO L4342KC and Digilent Pmod OLEDrgb tested. Be aware of the correct revision of the OS and correct versions of the display driver libraries!

Dependencies:   Adafruit_SSD1331_Mbed Adafruit-GFX

Committer:
timo_k2
Date:
Sat Aug 29 08:26:56 2020 +0000
Revision:
0:adc62c00bb85
The purpose of this project is to teach the students the development process and test that the hardware the ST NUCLEO-L432KC and the Digilent Pmod OLEDrgb work just fine. Be aware of correct revision of the OS and correct versions of the libraries!

Who changed what in which revision?

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