Up-Rev of code, small changes including general tidy of code and adding of missing comments

Dependencies:   mbed N5110v02 TMP102 JoystickIoT

main.cpp

Committer:
legstar85
Date:
2021-12-17
Revision:
2:e0091b5311f1
Parent:
1:dd5fb735acf1
Child:
3:80c1eba78f9b

File content as of revision 2:e0091b5311f1:

/* * Print String
* @ File main.cpp
* Prints a string of characters to the screen buffer, string is cut off after the 83rd  pixel.
* @param x - the column number (0 to 83)
* @param y - the row number (0-5) - the display is split into 6 banks - each bank can be considered a row
* @author - David Leaming - 25574043
* @ Date - December 2021
*
* Acknowledgements 
* Craig A. Evans, University of Leeds, TMP102 Library ,Feb 2016
* Dr Edmond Nurellari, University of Lincoln, Joystick & N5110 Libraries
*
*/ 

#include "mbed.h"                                                               // include the library header, ensure the library has been imported into the project
#include "TMP102.h"
#include "N5110.h"
#include "Joystick.h"
#include "Bitmap.h"

TMP102 tmp102(I2C_SDA,I2C_SCL);                                                 // Create TMP102 object  

//        VCC,SCE,RST,D/C,MOSI,SCLK,LED 
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);                                      // Create Screen Object - K64F - pwr from 3V3, GND Pin also needs connecting


Serial pc(USBTX,USBRX);                                                         // UART connection for PC

DigitalOut r_led(LED_RED);                                                      // K64F on-board LEDs 
DigitalOut g_led(LED_GREEN);                                                    // K64F on-board LEDs 
DigitalOut b_led(LED_BLUE);                                                     // K64F on-board LEDs 

InterruptIn sw2(SW2);                                                           // K64F on-board switches
InterruptIn sw3(SW3);                                                           // K64F on-board switches

void error();                                                                   // error function hangs flashing an LED
void init_serial();                                                             // setup serial port
void init_K64F();                                                               // set-up the on-board LEDs and switches

int main()
{
    init_K64F();                                                                // initialise the board
    init_serial();                                                              // initialise the serial port
    
    tmp102.init();                                                              // call the sensor init method using dot syntax
    lcd.init();                                                                 // initialise display
    
    lcd.setContrast(0.5);                                                       // change set contrast in range 0.0 to 1.0
    
    while (1) {
        
        lcd.normalMode();                                                       // normal colour mode
        lcd.setBrightness(0.5);                                                 // put LED backlight on 50%
        lcd.clear();                                                            // clear buffer at start of every loop
        
        char buffer[14];                                                        // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)

        
        int temperature = 27;
        int length = sprintf(buffer,"T = %2d C",temperature);                   // print formatted data to buffer - it is important the format specifier ensures the length will fit in the buffer
        if (length <= 14)                                                       // if string will fit on display (assuming printing at x=0)
        lcd.printString(buffer,0,1);                                            // display on screen

        float pressure = 1012.3;                                                // same idea with floats
        length = sprintf(buffer,"P = %.2f mb",pressure);
        if (length <= 14)
        lcd.printString(buffer,0,2);

        lcd.refresh();                                                          // need to refresh display after setting pixels or writing strings
        wait(2.0);     
        
        lcd.clear();                                                            // clear buffer at start of every loop
        lcd.refresh();                                                          // need to refresh display after setting pixels or writing strings 
        wait(0.5);
        
        float T = tmp102.get_temperature();                                     // read temperature and print to lcd
        length = sprintf(buffer,"T = %.2f C",T);
        if (length <= 14)
        lcd.printString(buffer,0,2);
        lcd.refresh();                                                          // need to refresh display after setting pixels or writing strings 
        wait(2.0);
        
    }

}

void init_serial() {
    pc.baud(115200);                                                            // set to highest baud - ensure terminal software matches
}

void init_K64F() 
{
    r_led = 1;                                                                  // on-board LEDs are active-low, so set pin high to turn them off.
    g_led = 1;                                                                  // on-board LEDs are active-low, so set pin high to turn them off.
    b_led = 1;                                                                  // on-board LEDs are active-low, so set pin high to turn them off.
    
    sw2.mode(PullNone);                                                         // since the on-board switches have external pull-ups, we should disable the internal pull-down
    sw3.mode(PullNone);                                                         // resistors that are enabled by default using InterruptIn

}