Updated following fixing of Joystick 'Bounce'. Also insertion of a Starting Temp to be compared against following user selection.

Dependencies:   mbed N5110v02 TMP102 JoystickIoT

Committer:
legstar85
Date:
Tue Dec 21 17:22:42 2021 +0000
Revision:
4:0090145b3286
Parent:
3:80c1eba78f9b
Child:
5:eebd36a47049
D. Leaming - 25574043 - Initial Code Updated, Welcome messages added, TMP102 printing to LCD, External PCB buttons connected

Who changed what in which revision?

UserRevisionLine numberNew contents of line
legstar85 4:0090145b3286 1 /*
legstar85 2:e0091b5311f1 2 * @ File main.cpp
legstar85 4:0090145b3286 3 * @ Author - David Leaming - 25574043
legstar85 2:e0091b5311f1 4 * @ Date - December 2021
legstar85 2:e0091b5311f1 5 *
legstar85 2:e0091b5311f1 6 * Acknowledgements
legstar85 2:e0091b5311f1 7 * Craig A. Evans, University of Leeds, TMP102 Library ,Feb 2016
legstar85 2:e0091b5311f1 8 * Dr Edmond Nurellari, University of Lincoln, Joystick & N5110 Libraries
legstar85 2:e0091b5311f1 9 *
eencae 0:21a200b880d7 10 */
eencae 0:21a200b880d7 11
legstar85 2:e0091b5311f1 12 #include "mbed.h" // include the library header, ensure the library has been imported into the project
eencae 1:dd5fb735acf1 13 #include "TMP102.h"
legstar85 2:e0091b5311f1 14 #include "N5110.h"
legstar85 2:e0091b5311f1 15 #include "Joystick.h"
legstar85 2:e0091b5311f1 16 #include "Bitmap.h"
eencae 0:21a200b880d7 17
legstar85 2:e0091b5311f1 18 TMP102 tmp102(I2C_SDA,I2C_SCL); // Create TMP102 object
legstar85 2:e0091b5311f1 19
legstar85 2:e0091b5311f1 20 // VCC,SCE,RST,D/C,MOSI,SCLK,LED
legstar85 2:e0091b5311f1 21 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // Create Screen Object - K64F - pwr from 3V3, GND Pin also needs connecting
legstar85 2:e0091b5311f1 22
legstar85 3:80c1eba78f9b 23 // y x button
legstar85 3:80c1eba78f9b 24 Joystick joystick(PTB10,PTB11,PTC16); // Define Joystick Object
eencae 0:21a200b880d7 25
legstar85 2:e0091b5311f1 26 Serial pc(USBTX,USBRX); // UART connection for PC
legstar85 2:e0091b5311f1 27
legstar85 2:e0091b5311f1 28 DigitalOut r_led(LED_RED); // K64F on-board LEDs
legstar85 2:e0091b5311f1 29 DigitalOut g_led(LED_GREEN); // K64F on-board LEDs
legstar85 2:e0091b5311f1 30 DigitalOut b_led(LED_BLUE); // K64F on-board LEDs
eencae 0:21a200b880d7 31
legstar85 3:80c1eba78f9b 32 DigitalOut LED01 (PTA1); // PCB Surface Mounted LED's - LED1
legstar85 3:80c1eba78f9b 33 DigitalOut LED02 (PTA2); // PCB Surface Mounted LED's - LED2
legstar85 3:80c1eba78f9b 34 DigitalOut LED03 (PTC2); // PCB Surface Mounted LED's - LED3
legstar85 3:80c1eba78f9b 35 DigitalOut LED04 (PTC3); // PCB Surface Mounted LED's - LED4
legstar85 3:80c1eba78f9b 36 DigitalOut LED05 (PTC4); // PCB Surface Mounted LED's - LED5
legstar85 3:80c1eba78f9b 37 DigitalOut LED06 (PTD3); // PCB Surface Mounted LED's - LED6
legstar85 3:80c1eba78f9b 38
legstar85 2:e0091b5311f1 39 InterruptIn sw2(SW2); // K64F on-board switches
legstar85 2:e0091b5311f1 40 InterruptIn sw3(SW3); // K64F on-board switches
legstar85 3:80c1eba78f9b 41 InterruptIn ButtonA (PTB9); // PCB Button - A
legstar85 3:80c1eba78f9b 42 InterruptIn ButtonB (PTD0); // PCB Button - B
legstar85 3:80c1eba78f9b 43
legstar85 3:80c1eba78f9b 44 volatile int g_ButtonA_flag=0; // flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global
legstar85 3:80c1eba78f9b 45 volatile int g_ButtonB_flag=0; // flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global
legstar85 3:80c1eba78f9b 46 volatile int g_sw2_flag=0; // flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global
legstar85 2:e0091b5311f1 47
legstar85 2:e0091b5311f1 48 void error(); // error function hangs flashing an LED
legstar85 2:e0091b5311f1 49 void init_serial(); // setup serial port
legstar85 2:e0091b5311f1 50 void init_K64F(); // set-up the on-board LEDs and switches
legstar85 3:80c1eba78f9b 51 void init_PCB(); // set-up the PCB LEDs and buttons
legstar85 3:80c1eba78f9b 52 void ButtonA_isr();
legstar85 3:80c1eba78f9b 53 void ButtonB_isr();
legstar85 3:80c1eba78f9b 54 void sw2_isr();
eencae 0:21a200b880d7 55
eencae 0:21a200b880d7 56 int main()
eencae 0:21a200b880d7 57 {
legstar85 2:e0091b5311f1 58 init_K64F(); // initialise the board
legstar85 2:e0091b5311f1 59 init_serial(); // initialise the serial port
legstar85 3:80c1eba78f9b 60 init_PCB(); // initialise the PCB
legstar85 3:80c1eba78f9b 61
legstar85 2:e0091b5311f1 62 tmp102.init(); // call the sensor init method using dot syntax
legstar85 2:e0091b5311f1 63 lcd.init(); // initialise display
legstar85 3:80c1eba78f9b 64 joystick.init(); // initialise joystick
legstar85 3:80c1eba78f9b 65
legstar85 3:80c1eba78f9b 66 sw2.fall(&sw2_isr); // SW2 has a pull-up resistor, so the pin will be at 3.3 V by default and fall to 0 V when pressed. We therefore need to look for a falling edge on the pin to fire the interrupt
legstar85 3:80c1eba78f9b 67 ButtonA.rise(&ButtonA_isr); // External push button, pin set to 0V by pull down command, means a rising edge is looked for
legstar85 3:80c1eba78f9b 68 ButtonB.rise(&ButtonB_isr); // External push button, pin set to 0V by pull down command, means a rising edge is looked for
legstar85 2:e0091b5311f1 69
legstar85 2:e0091b5311f1 70 lcd.setContrast(0.5); // change set contrast in range 0.0 to 1.0
eencae 0:21a200b880d7 71
legstar85 3:80c1eba78f9b 72 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
legstar85 3:80c1eba78f9b 73
legstar85 3:80c1eba78f9b 74 lcd.clear(); // clear buffer at start of every loop
legstar85 3:80c1eba78f9b 75 lcd.printString("Smart Cold",0,0); // can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display)
legstar85 3:80c1eba78f9b 76 lcd.printString("Storage",0,1); // Just a welcome message before auto moving to main menu
legstar85 3:80c1eba78f9b 77 lcd.printString("Monitoring",0,2);
legstar85 3:80c1eba78f9b 78 lcd.printString("",0,2); // Blank Line
legstar85 3:80c1eba78f9b 79 lcd.printString("V1.0 - 2021",0,4);
legstar85 3:80c1eba78f9b 80 lcd.printString("David Leaming",0,5);
legstar85 3:80c1eba78f9b 81 lcd.refresh(); // need to refresh display after setting pixels or writing strings
legstar85 3:80c1eba78f9b 82 wait(5.0); // leave welcome screen on for designated amount of time
legstar85 3:80c1eba78f9b 83 lcd.clear(); // clear buffer at start of every loop
legstar85 3:80c1eba78f9b 84 lcd.refresh(); // need to refresh display after setting pixels or writing strings
legstar85 3:80c1eba78f9b 85 lcd.printString("Use Joystick",0,0); // Instruction for use of menu
legstar85 3:80c1eba78f9b 86 lcd.printString("To Navigate",0,1);
legstar85 3:80c1eba78f9b 87 lcd.printString("",0,2); // Blank Line
legstar85 3:80c1eba78f9b 88 lcd.printString("A = Select",0,3);
legstar85 3:80c1eba78f9b 89 lcd.printString("B = Back",0,4);
legstar85 3:80c1eba78f9b 90 lcd.refresh(); // need to refresh display after setting pixels or writing strings
legstar85 3:80c1eba78f9b 91 wait(5.0);
legstar85 3:80c1eba78f9b 92
legstar85 3:80c1eba78f9b 93 MainMenu: // Initial Main Menu Creation to allow mode to be selected
legstar85 3:80c1eba78f9b 94
legstar85 3:80c1eba78f9b 95
legstar85 3:80c1eba78f9b 96
eencae 0:21a200b880d7 97 while (1) {
eencae 0:21a200b880d7 98
legstar85 2:e0091b5311f1 99 lcd.normalMode(); // normal colour mode
legstar85 2:e0091b5311f1 100 lcd.setBrightness(0.5); // put LED backlight on 50%
legstar85 2:e0091b5311f1 101 lcd.clear(); // clear buffer at start of every loop
legstar85 2:e0091b5311f1 102 lcd.refresh(); // need to refresh display after setting pixels or writing strings
legstar85 2:e0091b5311f1 103
legstar85 2:e0091b5311f1 104 float T = tmp102.get_temperature(); // read temperature and print to lcd
legstar85 3:80c1eba78f9b 105 int length = sprintf(buffer,"T = %.2f C",T); // print formatted data to buffer - it is important the format specifier ensures the length will fit in the buffer
legstar85 3:80c1eba78f9b 106 if (length <= 14) // if string will fit on display (assuming printing at x=0)
legstar85 3:80c1eba78f9b 107 lcd.printString(buffer,0,2); // display on screen
legstar85 2:e0091b5311f1 108 lcd.refresh(); // need to refresh display after setting pixels or writing strings
legstar85 2:e0091b5311f1 109 wait(2.0);
legstar85 3:80c1eba78f9b 110
legstar85 3:80c1eba78f9b 111 if (g_ButtonA_flag) { // check if flag i.e. interrupt has occured
legstar85 3:80c1eba78f9b 112 g_ButtonA_flag = 0; // if it has, clear the flag
legstar85 3:80c1eba78f9b 113
legstar85 3:80c1eba78f9b 114 printf("Button A Press \n"); // send message over serial port - can observe in CoolTerm etc.
legstar85 3:80c1eba78f9b 115 LED01=0;
legstar85 3:80c1eba78f9b 116 wait(5.0);
legstar85 3:80c1eba78f9b 117 LED01=1;
legstar85 3:80c1eba78f9b 118 }
legstar85 3:80c1eba78f9b 119
legstar85 3:80c1eba78f9b 120 if (g_ButtonB_flag) { // check if flag i.e. interrupt has occured
legstar85 3:80c1eba78f9b 121 g_ButtonB_flag = 0; // if it has, clear the flag
legstar85 3:80c1eba78f9b 122
legstar85 3:80c1eba78f9b 123 printf("Button B Press \n"); // send message over serial port - can observe in CoolTerm etc.
legstar85 3:80c1eba78f9b 124 LED02=0;
legstar85 3:80c1eba78f9b 125 wait(5.0);
legstar85 3:80c1eba78f9b 126 LED02=1;
legstar85 3:80c1eba78f9b 127
legstar85 3:80c1eba78f9b 128 }
eencae 0:21a200b880d7 129
eencae 0:21a200b880d7 130 }
eencae 0:21a200b880d7 131
eencae 0:21a200b880d7 132 }
eencae 0:21a200b880d7 133
eencae 0:21a200b880d7 134 void init_serial() {
legstar85 2:e0091b5311f1 135 pc.baud(115200); // set to highest baud - ensure terminal software matches
eencae 0:21a200b880d7 136 }
eencae 0:21a200b880d7 137
eencae 0:21a200b880d7 138 void init_K64F()
eencae 0:21a200b880d7 139 {
legstar85 2:e0091b5311f1 140 r_led = 1; // on-board LEDs are active-low, so set pin high to turn them off.
legstar85 2:e0091b5311f1 141 g_led = 1; // on-board LEDs are active-low, so set pin high to turn them off.
legstar85 2:e0091b5311f1 142 b_led = 1; // on-board LEDs are active-low, so set pin high to turn them off.
eencae 0:21a200b880d7 143
legstar85 2:e0091b5311f1 144 sw2.mode(PullNone); // since the on-board switches have external pull-ups, we should disable the internal pull-down
legstar85 2:e0091b5311f1 145 sw3.mode(PullNone); // resistors that are enabled by default using InterruptIn
eencae 0:21a200b880d7 146
eencae 0:21a200b880d7 147 }
legstar85 3:80c1eba78f9b 148
legstar85 3:80c1eba78f9b 149 void init_PCB ()
legstar85 3:80c1eba78f9b 150 {
legstar85 3:80c1eba78f9b 151 LED01 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly
legstar85 3:80c1eba78f9b 152 LED02 = 1;
legstar85 3:80c1eba78f9b 153 LED03 = 1;
legstar85 3:80c1eba78f9b 154 LED04 = 1;
legstar85 3:80c1eba78f9b 155 LED05 = 1;
legstar85 3:80c1eba78f9b 156 LED06 = 1;
legstar85 3:80c1eba78f9b 157
legstar85 3:80c1eba78f9b 158 ButtonA.mode(PullDown); // Set pin to Pull Down to OV, meaning that a rising edge is looked for when button is pressed
legstar85 3:80c1eba78f9b 159 ButtonB.mode(PullDown); // Set pin to Pull Down to OV, meaning that a rising edge is looked for when button is pressed
legstar85 3:80c1eba78f9b 160
legstar85 3:80c1eba78f9b 161 }
legstar85 3:80c1eba78f9b 162
legstar85 3:80c1eba78f9b 163 void ButtonA_isr() // ButtonA event-triggered interrupt
legstar85 3:80c1eba78f9b 164 {
legstar85 3:80c1eba78f9b 165 g_ButtonA_flag = 1; // set flag in ISR
legstar85 3:80c1eba78f9b 166 }
legstar85 3:80c1eba78f9b 167
legstar85 3:80c1eba78f9b 168 void ButtonB_isr() // ButtonB event-triggered interrupt
legstar85 3:80c1eba78f9b 169 {
legstar85 3:80c1eba78f9b 170 g_ButtonB_flag = 1; // set flag in ISR
legstar85 3:80c1eba78f9b 171 }
legstar85 3:80c1eba78f9b 172
legstar85 3:80c1eba78f9b 173 void sw2_isr() // SW2 event-triggered interrupt
legstar85 3:80c1eba78f9b 174 {
legstar85 3:80c1eba78f9b 175 g_sw2_flag = 1; // set flag in ISR
legstar85 3:80c1eba78f9b 176 }