Rev 1.6 - Sample Period Work in progress

Dependencies:   mbed Bitmap N5110 TMP102 Joystick

Revision:
5:138a91e25e1c
Parent:
4:891e1b0fbd1e
Child:
6:117edd5dc0a0
--- a/main.cpp	Fri Dec 10 10:06:57 2021 +0000
+++ b/main.cpp	Tue Dec 14 14:24:31 2021 +0000
@@ -1,73 +1,74 @@
-/*
-@Acknowledgements to Craig A.Evans for TMP102 Library reading temperatures
-/Revision 1.2.1
+/* 
+
+2645_I2C_TMP102_Library
+
+Sample code from ELEC2645 Week 17 Lab
+
+Demonstrates how to re-factor the TMP102 code into a library
+
+(c) Craig A. Evans, University of Leeds, Feb 2016
+
 */ 
-/*============================ Libraries =====================================*/
-#include "mbed.h"                                                               // Including Mbed OS Header File Libraries
-#include "TMP102.h"                                                             // Including Tmp102 Header File Libraries
 
-
-/*============================ Variables =====================================*/
-//Serial
-TMP102 Tmp_I2C(PTE25,PTE24);                                                    //Establishing TMP102 I2C Sensor (I2C_SDA(PTE25),I2C_SCL(PTE24)
-Serial serial(USBTX,USBRX);                                                     //Serial Port 
+#include "mbed.h"
+// include the library header, ensure the library has been imported into the project
+#include "TMP102.h"
 
-//Outputs                                                                                
-DigitalOut Red_DO(LED_RED);                                                     // K64F on-board LEDs 
-DigitalOut Grn_DO(LED_GREEN);
-DigitalOut Blue_DO(LED_BLUE);
-//Inputs
-InterruptIn Sw2_DI(SW2);                                                        // K64F on-board switches
-InterruptIn Sw3_DI(SW3);
+// Create TMP102 object
+TMP102 tmp102(I2C_SDA,I2C_SCL);  
+// UART connection for PC
+Serial serial(USBTX,USBRX);
 
-/*=========================== Void Functions =================================*/
-
-void error();                                                                   // error function flashing blue LED from TMP102.cpp and prints Error State to CoolTerm
+// K64F on-board LEDs 
+DigitalOut r_led(LED_RED);
+DigitalOut g_led(LED_GREEN);
+DigitalOut b_led(LED_BLUE);
+// K64F on-board switches
+InterruptIn sw2(SW2);
+InterruptIn sw3(SW3);
 
-void init_serial();                                                             // Serial port setup
+// error function hangs flashing an LED
+void error();
+// setup serial port
+void init_serial();
+// set-up the on-board LEDs and switches
+void init_K64F();
 
-void init_K64FSetup();                                                          // Disabling on-board LEDs and switches
-
-/*============================ Main Code =====================================*/
 int main()
 {
-    serial.baud(9600);
-
-    init_K64FSetup();                                                           // Initiliasing the Void Functions into the main code
+    // initialise the board and serial port
+    init_K64F();
     init_serial(); 
-    error();
-    
-    Tmp_I2C.init();                                                             // I2C Sensor Initialising
+    // call the sensor init method using dot syntax
+    tmp102.init();
     
     while (1) {
         
-        float T = Tmp_I2C.get_temperature();                                    // Read Temp from I2C Sensor
-        serial.printf("Room Temperature = %f C\n",T);                                          // Print Value over CoolTerm
-        
-        wait(1.0);                                                              // small delay - 1s to match the update rate of the sensor (1 Hz)
+        // read temperature and print over serial port
+        float T = tmp102.get_temperature();
+        serial.printf("T = %f C\n",T);
+        // small delay - 1s to match the update rate of the sensor (1 Hz)
+        wait(1.0);
         
     }
+
 }
 
-/*=============================== Void Setup =================================*/
-
 void init_serial() {
     // set to highest baud - ensure terminal software matches
     serial.baud(9600); 
 }
 
-void init_K64FSetup() 
+void init_K64F() 
 {
-                                                                                
-    Red_DO = 1;                                                                 // on-board LEDs are active-low, so set pin high to turn them off.
-    Grn_DO = 1;
-    Blue_DO = 1;   
-                                                                                                                                                                    
-    Sw2_DI.mode(PullNone);                                                      // since the on-board switches have external pull-ups, we should disable the internal pull-down
-    Sw3_DI.mode(PullNone);                                                      // resistors that are enabled by default using InterruptIn
+    // on-board LEDs are active-low, so set pin high to turn them off.
+    r_led = 1;
+    g_led = 1;
+    b_led = 1;   
+    
+    // since the on-board switches have external pull-ups, we should disable the internal pull-down
+    // resistors that are enabled by default using InterruptIn
+    sw2.mode(PullNone);
+    sw3.mode(PullNone);
 
 }
-void error(){
-    serial.baud(9600);                                                          // Serial Baud Rate
-    serial.printf("Error State");                                               // Error Print Message
-}