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:
Sun Aug 30 09:39:15 2020 +0000
Revision:
1:de95c9ab922e
Parent:
0:9f57fa937a86
Child:
2:29bb1ef0d6cd
Minor updates into code comments only.

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 0:9f57fa937a86 23 * L432KC D9 - 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 0:9f57fa937a86 29 * VCC - OLED 6 Power supply 3,3 V
timo_k2 0:9f57fa937a86 30 *
timo_k2 0:9f57fa937a86 31 *************************************************************************
timo_k2 0:9f57fa937a86 32 * Compiles OK, works with hardware with operating system revision 6691:cf4f12a
timo_k2 0:9f57fa937a86 33 * Compiles OK, DOESN'T WORK with hardware with op sys revision 7088:3ab72c7
timo_k2 1:de95c9ab922e 34 * Updated for OS6, Compiles OK, works with hardware with os revision OS 7864:890f056
timo_k2 1:de95c9ab922e 35 * Timo Karppinen 29.08.2020 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 0:9f57fa937a86 47 Adafruit_SSD1331 OLED(D9, D6, D10, D11, NC, D13); // cs, res, dc, mosi, (nc), sck
timo_k2 0:9f57fa937a86 48
timo_k2 0:9f57fa937a86 49 //DigitalOut LED(LED1); // LED1, LED2, LED3 and LED4 are the D13 PB_3 pin in this board and
timo_k2 1:de95c9ab922e 50 //can not be used as a LED because of the SPI
timo_k2 0:9f57fa937a86 51 DigitalOut VCCEN(D3);
timo_k2 0:9f57fa937a86 52 DigitalOut PMODEN(D5);
timo_k2 0:9f57fa937a86 53
timo_k2 0:9f57fa937a86 54 // Definition of colours on the OLED display
timo_k2 0:9f57fa937a86 55 #define Black 0x0000
timo_k2 0:9f57fa937a86 56 #define Blue 0x001F
timo_k2 0:9f57fa937a86 57 #define Red 0xF800
timo_k2 0:9f57fa937a86 58 #define Green 0x07E0
timo_k2 0:9f57fa937a86 59 #define Cyan 0x07FF
timo_k2 0:9f57fa937a86 60 #define Magenta 0xF81F
timo_k2 0:9f57fa937a86 61 #define Yellow 0xFFE0
timo_k2 0:9f57fa937a86 62 #define White 0xFFFF
timo_k2 0:9f57fa937a86 63
timo_k2 0:9f57fa937a86 64 float stepsScaledF = 0; // 32 bit floating point
timo_k2 0:9f57fa937a86 65 void getSTEPS();
timo_k2 0:9f57fa937a86 66
timo_k2 0:9f57fa937a86 67 char Time[50],Date[50];
timo_k2 0:9f57fa937a86 68 void getTime();
timo_k2 0:9f57fa937a86 69 int first = 0;
timo_k2 0:9f57fa937a86 70
timo_k2 0:9f57fa937a86 71 int main()
timo_k2 0:9f57fa937a86 72 {
timo_k2 0:9f57fa937a86 73 // Showing with a LED that program has started
timo_k2 0:9f57fa937a86 74 // LED = 0;
timo_k2 0:9f57fa937a86 75 VCCEN = 1; // if you did not connect VCCEN permanently to Vcc
timo_k2 0:9f57fa937a86 76 PMODEN = 1; // if you did not connect PMODEN permanently to Vcc
timo_k2 0:9f57fa937a86 77 ThisThread::sleep_for(2000ms);
timo_k2 0:9f57fa937a86 78 // LED = 1;
timo_k2 0:9f57fa937a86 79 ThisThread::sleep_for(2000ms);
timo_k2 0:9f57fa937a86 80 // LED = 0;
timo_k2 1:de95c9ab922e 81 // Initalize the PmodOLEDrgb, the library includes SPI and there is
timo_k2 1:de95c9ab922e 82 // no need to initialize the SPI in the main.cpp
timo_k2 0:9f57fa937a86 83 OLED.begin(); // initialization of display object
timo_k2 0:9f57fa937a86 84
timo_k2 0:9f57fa937a86 85 OLED.clearScreen();
timo_k2 0:9f57fa937a86 86
timo_k2 0:9f57fa937a86 87 while (true) {
timo_k2 0:9f57fa937a86 88 while(first < 3)
timo_k2 0:9f57fa937a86 89 {
timo_k2 0:9f57fa937a86 90 first += 1;
timo_k2 0:9f57fa937a86 91 OLED.fillScreen(Black); // background screen in black
timo_k2 0:9f57fa937a86 92 OLED.setTextColor(Cyan); // colour of text in cyan
timo_k2 0:9f57fa937a86 93 OLED.setCursor(0,0); // cursor is in x=0 and y=15
timo_k2 0:9f57fa937a86 94 OLED.printf("Test module Pmod"); // display text
timo_k2 0:9f57fa937a86 95 ThisThread::sleep_for(500ms); // wait 500 ms
timo_k2 0:9f57fa937a86 96 OLED.setCursor(0,15); // cursor is in x=0 and y=15
timo_k2 0:9f57fa937a86 97 OLED.setTextSize(2); // size of text
timo_k2 0:9f57fa937a86 98 OLED.setTextColor(Red); // text in red colour
timo_k2 0:9f57fa937a86 99 OLED.printf("DIGILENT"); // display text
timo_k2 0:9f57fa937a86 100 OLED.setCursor(20,40); // cursor is in x=20 and y=40
timo_k2 0:9f57fa937a86 101 OLED.setTextSize(1); // size of text
timo_k2 0:9f57fa937a86 102 OLED.setTextColor(Green); // text in green colour
timo_k2 0:9f57fa937a86 103 OLED.printf("LEXTRONIC"); // display text
timo_k2 0:9f57fa937a86 104 OLED.drawFastHLine(1, 60, OLED.width()-1, Blue); //blue line x=1, width-1 and y=60
timo_k2 0:9f57fa937a86 105 ThisThread::sleep_for(2s); // wait 2 s
timo_k2 0:9f57fa937a86 106 OLED.fillScreen(Black); // background display in black (erase display)
timo_k2 0:9f57fa937a86 107 OLED.fillRoundRect(5, 5, 30, 40, 1, Blue); // French flag bleu blanc rouge
timo_k2 0:9f57fa937a86 108 OLED.fillRoundRect(35, 5, 30, 40, 1, White);
timo_k2 0:9f57fa937a86 109 OLED.fillRoundRect(65, 5, 30, 40, 1, Red);
timo_k2 0:9f57fa937a86 110 OLED.fillCircle(90, 55, 5, Yellow); // yellow circle with radius=5 in x=90 and y=55
timo_k2 0:9f57fa937a86 111 ThisThread::sleep_for(2s); // wait 2 s
timo_k2 0:9f57fa937a86 112 }
timo_k2 0:9f57fa937a86 113
timo_k2 0:9f57fa937a86 114 getTime();
timo_k2 0:9f57fa937a86 115 OLED.clearScreen();
timo_k2 0:9f57fa937a86 116 OLED.fillScreen(Black); // background screen in black
timo_k2 0:9f57fa937a86 117 OLED.setTextColor(Cyan); // colour of text in cyan
timo_k2 0:9f57fa937a86 118 ThisThread::sleep_for(300ms);
timo_k2 0:9f57fa937a86 119 OLED.setCursor(0,0); // cursor is in x=0 and y=0
timo_k2 0:9f57fa937a86 120 OLED.printf("Rec '%s' \n",Time);
timo_k2 0:9f57fa937a86 121
timo_k2 0:9f57fa937a86 122 getSTEPS();
timo_k2 0:9f57fa937a86 123 OLED.printf("value = '%0.1f' \n",stepsScaledF);
timo_k2 0:9f57fa937a86 124
timo_k2 0:9f57fa937a86 125 if (stepsScaledF > 2.00)
timo_k2 0:9f57fa937a86 126 {
timo_k2 0:9f57fa937a86 127 OLED.setTextColor(Yellow);
timo_k2 0:9f57fa937a86 128 OLED.printf("Be aware of reaching the limit");
timo_k2 0:9f57fa937a86 129 OLED.fillRoundRect(10, 35, 50, 3 , 1, Yellow);
timo_k2 0:9f57fa937a86 130 }
timo_k2 1:de95c9ab922e 131 OLED.setCursor(0,40); // cursor is in x=0 and y=40
timo_k2 0:9f57fa937a86 132 OLED.printf("Wait a moment !");
timo_k2 0:9f57fa937a86 133 OLED.drawFastHLine(1, 60, OLED.width()-1, Blue); // blue line x=1 to screen width-1 and y=60
timo_k2 0:9f57fa937a86 134 printf("printed on OLED");
timo_k2 0:9f57fa937a86 135
timo_k2 0:9f57fa937a86 136 ThisThread::sleep_for(3000ms);
timo_k2 0:9f57fa937a86 137 }
timo_k2 0:9f57fa937a86 138 }
timo_k2 0:9f57fa937a86 139
timo_k2 0:9f57fa937a86 140 void getTime()
timo_k2 0:9f57fa937a86 141 {
timo_k2 0:9f57fa937a86 142 time_t seconds = time(NULL); // https://os.mbed.com/docs/mbed-os/v6.2/apis/time.html
timo_k2 0:9f57fa937a86 143 strftime(Time,40,"%H:%M:%S", localtime(&seconds));
timo_k2 0:9f57fa937a86 144 strftime(Date,40,"%d-%b-%Y", localtime(&seconds));
timo_k2 0:9f57fa937a86 145 printf("Recorded '%s' \r\n",Time);
timo_k2 0:9f57fa937a86 146 }
timo_k2 0:9f57fa937a86 147
timo_k2 0:9f57fa937a86 148 void getSTEPS()
timo_k2 0:9f57fa937a86 149 {
timo_k2 0:9f57fa937a86 150 static int count;
timo_k2 0:9f57fa937a86 151
timo_k2 0:9f57fa937a86 152 count = count + 100;
timo_k2 0:9f57fa937a86 153 if (count > 4095){
timo_k2 0:9f57fa937a86 154 count = 0;
timo_k2 0:9f57fa937a86 155 }
timo_k2 0:9f57fa937a86 156
timo_k2 0:9f57fa937a86 157 stepsScaledF = (float(count))*(float(2.50)/4095); // The value 2.50 is 64 bit double precision floating point of type double.
timo_k2 0:9f57fa937a86 158 // Conversions to 32 bit floating point of type float.
timo_k2 0:9f57fa937a86 159 printf("Counter value '%d' \r\n",count);
timo_k2 0:9f57fa937a86 160 printf("Scaled output '%0.1f' \r\n",stepsScaledF);
timo_k2 0:9f57fa937a86 161 ThisThread::sleep_for(100ms);
timo_k2 0:9f57fa937a86 162 }