A concept design laboratory prototype for measuring ambient light value. The high levels typically mean that there is high rates of UVA and UVB radiation as well. A warning of this is displayed on a small screen. NXP FRDM-K64F, Digilent Pmod modules Pmod OLEDrgb and Pmod ALS. Tested as well on ST L432KC.

Dependencies:   Adafruit_SSD1331_Mbed Adafruit-GFX

main.cpp

Committer:
timo_k2
Date:
2020-04-19
Revision:
0:87339e72a5eb
Child:
1:fde67a85d481

File content as of revision 0:87339e72a5eb:

/* mbed Microcontroller Library
 * Copyright (c) 2019 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"
#include "Adafruit_SSD1331.h"
#include "Adafruit_GFX.h"


// PmodOLEDrgb
Adafruit_SSD1331 OLED(D8, D7, D10, D11, NC, D13); // cs, res, dc, mosi, (nc), sck  

DigitalOut LED(LED2); 
DigitalOut VCCEN(D6);
DigitalOut PMODEN(D5);

// Definition of colors on the OLED display
#define Black 0x0000
#define Blue 0x001F
#define Red 0xF800
#define Green 0x07E0
#define Cyan 0x07FF
#define Magenta 0xF81F
#define Yellow 0xFFE0
#define White 0xFFFF

// PmodALS
SPI spi(D11, D12, D13); // mosi, miso, sck
 

DigitalOut alsCS(D4);        // chip select for sensor SPI communication
char alsByte0 = 0;           // 8 bit data from sensor board, char is the unsigned 8 bit
char alsByte1 = 0;           // 8 bit data from sensor board
char alsByteSh0 = 0;
char alsByteSh1 = 0;
char als8bit = 0;
unsigned short alsRaw = 0;   // unsigned 16 bit
float alsScaledF = 0;       // 32 bit floating point
void getALS();


char Time[50],Date[50];
void getTime();
int first(0);

int main()
{
  // Showing with a LED that program has started
        LED = 0;
        VCCEN = 1;    // if you did not connect VCCEN permanently to Vcc
        PMODEN = 1;   // if you did not connect PMODEN permanently to Vcc
        wait_ms(2000);
        LED = 1;
        wait_ms(2000);
        LED = 0;
  // Initalize the PmodOLEDrgb    
        OLED.begin(); // initialization of display objcet
        
// SPI for the ALS      
        alsCS = 1;
        // Setup the spi for 8 bit data, high steady state clock,
        // second edge capture, with a 12MHz clock rate
        spi.format(8,0);           
        spi.frequency(12000000);   

    while (true) {
        while(first < 3)
            {
                first += 1; 
                OLED.fillScreen(Black); // background screen in black
                OLED.setTextColor(Cyan); // color of text in cyan
                OLED.setCursor(0,0); // cursor is in x=0 and y=15
                OLED.printf("Test module Pmod"); // display text
                wait_ms(500); // wait 500 ms
                OLED.setCursor(0,15); // cursor is in x=0 and y=15
                OLED.setTextSize(2); // size of text
                OLED.setTextColor(Red); // text in red color
                OLED.printf("DIGILENT"); // display text
                OLED.setCursor(20,40); // cursor is in x=20 and y=40
                OLED.setTextSize(1); // size of text
                OLED.setTextColor(Green); // text in green color
                OLED.printf("LEXTRONIC"); // display text
                OLED.drawFastHLine(1, 60, OLED.width()-1, Blue); // blue line x=1 to screen width-1 and y=60
                wait_ms(2000); // wait 2 s
                OLED.fillScreen(Black); // background display in black (erase display)
                OLED.fillRoundRect(5, 5, 30, 40, 1, Blue); // French flag bleu blanc rouge
                OLED.fillRoundRect(35, 5, 30, 40, 1, White);
                OLED.fillRoundRect(65, 5, 30, 40, 1, Red);
                OLED.fillCircle(90, 55, 5, Yellow); // yellow circle with radius=5 in x=90 and y=55
                wait_ms(2000); // wait 2 s
            }

        getTime();
        OLED.fillScreen(Black); // background screen in black
        OLED.setTextColor(Cyan); // color of text in cyan
        wait_ms(300);
        OLED.setCursor(0,0); // cursor is in x=0 and y=0
        OLED.printf("Rec '%s' \r\n",Time);
        
        getALS(); 
        OLED.printf("LUX = '%0.1f' \r\n",alsScaledF);

        if (alsScaledF > 100)
        { 
            OLED.setTextColor(Yellow);
            OLED.printf("Be aware of high UV radiation!");
            OLED.fillRoundRect(10, 35, 50, 3 , 1, Yellow);
        }
        OLED.setCursor(0,40); // cursor is in x=0 and y=0
        OLED.printf("Test completed");
        printf("printed on OLED");
        
        wait(1);
}
}

void getTime()
{    
    time_t seconds = time(NULL);
    strftime(Time,40,"%H:%M:%S", localtime(&seconds));
    strftime(Date,40,"%d-%b-%Y", localtime(&seconds));
    printf("Recorded '%s' \r\n",Time);
}

void getALS()
{
    alsCS = 0; 
    
    alsByte0 = spi.write(0x00);
    alsByte1 = spi.write(0x00);
  
    alsCS = 1;
   
    alsByteSh0 = alsByte0 << 4;
    alsByteSh1 = alsByte1 >> 4;
    
    als8bit =( alsByteSh0 | alsByteSh1 );
    
    alsRaw = als8bit; // 
    alsScaledF = (float(alsRaw))*(float(6.68)); // The value 6.68 is 64 bit double precision floating point of type double.
                                                // Conversions to 32 bit floating point of type float.
    printf("Ambient light raw 8 bit 0...255 =  '%d' \r\n",alsRaw);
    printf("Ambient light scaled to LUX =  '%0.1f' \r\n",alsScaledF);
    wait_ms(100);
}