Addition of FSM to try and set initial starting temperature for use throughout project. Additional menu option. Joystick input 'bouncing' during FSM use - to be debugged
Dependencies: mbed N5110v02 TMP102 JoystickIoT
main.cpp@2:e0091b5311f1, 2021-12-17 (annotated)
- Committer:
- legstar85
- Date:
- Fri Dec 17 08:34:45 2021 +0000
- Revision:
- 2:e0091b5311f1
- Parent:
- 1:dd5fb735acf1
- Child:
- 3:80c1eba78f9b
D.Leaming - 25574043 - University of Lincoln - IoT Project - V04
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
legstar85 | 2:e0091b5311f1 | 1 | /* * Print String |
legstar85 | 2:e0091b5311f1 | 2 | * @ File main.cpp |
legstar85 | 2:e0091b5311f1 | 3 | * Prints a string of characters to the screen buffer, string is cut off after the 83rd pixel. |
legstar85 | 2:e0091b5311f1 | 4 | * @param x - the column number (0 to 83) |
legstar85 | 2:e0091b5311f1 | 5 | * @param y - the row number (0-5) - the display is split into 6 banks - each bank can be considered a row |
legstar85 | 2:e0091b5311f1 | 6 | * @author - David Leaming - 25574043 |
legstar85 | 2:e0091b5311f1 | 7 | * @ Date - December 2021 |
legstar85 | 2:e0091b5311f1 | 8 | * |
legstar85 | 2:e0091b5311f1 | 9 | * Acknowledgements |
legstar85 | 2:e0091b5311f1 | 10 | * Craig A. Evans, University of Leeds, TMP102 Library ,Feb 2016 |
legstar85 | 2:e0091b5311f1 | 11 | * Dr Edmond Nurellari, University of Lincoln, Joystick & N5110 Libraries |
legstar85 | 2:e0091b5311f1 | 12 | * |
eencae | 0:21a200b880d7 | 13 | */ |
eencae | 0:21a200b880d7 | 14 | |
legstar85 | 2:e0091b5311f1 | 15 | #include "mbed.h" // include the library header, ensure the library has been imported into the project |
eencae | 1:dd5fb735acf1 | 16 | #include "TMP102.h" |
legstar85 | 2:e0091b5311f1 | 17 | #include "N5110.h" |
legstar85 | 2:e0091b5311f1 | 18 | #include "Joystick.h" |
legstar85 | 2:e0091b5311f1 | 19 | #include "Bitmap.h" |
eencae | 0:21a200b880d7 | 20 | |
legstar85 | 2:e0091b5311f1 | 21 | TMP102 tmp102(I2C_SDA,I2C_SCL); // Create TMP102 object |
legstar85 | 2:e0091b5311f1 | 22 | |
legstar85 | 2:e0091b5311f1 | 23 | // VCC,SCE,RST,D/C,MOSI,SCLK,LED |
legstar85 | 2:e0091b5311f1 | 24 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // Create Screen Object - K64F - pwr from 3V3, GND Pin also needs connecting |
legstar85 | 2:e0091b5311f1 | 25 | |
eencae | 0:21a200b880d7 | 26 | |
legstar85 | 2:e0091b5311f1 | 27 | Serial pc(USBTX,USBRX); // UART connection for PC |
legstar85 | 2:e0091b5311f1 | 28 | |
legstar85 | 2:e0091b5311f1 | 29 | DigitalOut r_led(LED_RED); // K64F on-board LEDs |
legstar85 | 2:e0091b5311f1 | 30 | DigitalOut g_led(LED_GREEN); // K64F on-board LEDs |
legstar85 | 2:e0091b5311f1 | 31 | DigitalOut b_led(LED_BLUE); // K64F on-board LEDs |
eencae | 0:21a200b880d7 | 32 | |
legstar85 | 2:e0091b5311f1 | 33 | InterruptIn sw2(SW2); // K64F on-board switches |
legstar85 | 2:e0091b5311f1 | 34 | InterruptIn sw3(SW3); // K64F on-board switches |
legstar85 | 2:e0091b5311f1 | 35 | |
legstar85 | 2:e0091b5311f1 | 36 | void error(); // error function hangs flashing an LED |
legstar85 | 2:e0091b5311f1 | 37 | void init_serial(); // setup serial port |
legstar85 | 2:e0091b5311f1 | 38 | void init_K64F(); // set-up the on-board LEDs and switches |
eencae | 0:21a200b880d7 | 39 | |
eencae | 0:21a200b880d7 | 40 | int main() |
eencae | 0:21a200b880d7 | 41 | { |
legstar85 | 2:e0091b5311f1 | 42 | init_K64F(); // initialise the board |
legstar85 | 2:e0091b5311f1 | 43 | init_serial(); // initialise the serial port |
legstar85 | 2:e0091b5311f1 | 44 | |
legstar85 | 2:e0091b5311f1 | 45 | tmp102.init(); // call the sensor init method using dot syntax |
legstar85 | 2:e0091b5311f1 | 46 | lcd.init(); // initialise display |
legstar85 | 2:e0091b5311f1 | 47 | |
legstar85 | 2:e0091b5311f1 | 48 | lcd.setContrast(0.5); // change set contrast in range 0.0 to 1.0 |
eencae | 0:21a200b880d7 | 49 | |
eencae | 0:21a200b880d7 | 50 | while (1) { |
eencae | 0:21a200b880d7 | 51 | |
legstar85 | 2:e0091b5311f1 | 52 | lcd.normalMode(); // normal colour mode |
legstar85 | 2:e0091b5311f1 | 53 | lcd.setBrightness(0.5); // put LED backlight on 50% |
legstar85 | 2:e0091b5311f1 | 54 | lcd.clear(); // clear buffer at start of every loop |
legstar85 | 2:e0091b5311f1 | 55 | |
legstar85 | 2:e0091b5311f1 | 56 | char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) |
legstar85 | 2:e0091b5311f1 | 57 | |
legstar85 | 2:e0091b5311f1 | 58 | |
legstar85 | 2:e0091b5311f1 | 59 | int temperature = 27; |
legstar85 | 2:e0091b5311f1 | 60 | 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 |
legstar85 | 2:e0091b5311f1 | 61 | if (length <= 14) // if string will fit on display (assuming printing at x=0) |
legstar85 | 2:e0091b5311f1 | 62 | lcd.printString(buffer,0,1); // display on screen |
legstar85 | 2:e0091b5311f1 | 63 | |
legstar85 | 2:e0091b5311f1 | 64 | float pressure = 1012.3; // same idea with floats |
legstar85 | 2:e0091b5311f1 | 65 | length = sprintf(buffer,"P = %.2f mb",pressure); |
legstar85 | 2:e0091b5311f1 | 66 | if (length <= 14) |
legstar85 | 2:e0091b5311f1 | 67 | lcd.printString(buffer,0,2); |
legstar85 | 2:e0091b5311f1 | 68 | |
legstar85 | 2:e0091b5311f1 | 69 | lcd.refresh(); // need to refresh display after setting pixels or writing strings |
legstar85 | 2:e0091b5311f1 | 70 | wait(2.0); |
legstar85 | 2:e0091b5311f1 | 71 | |
legstar85 | 2:e0091b5311f1 | 72 | lcd.clear(); // clear buffer at start of every loop |
legstar85 | 2:e0091b5311f1 | 73 | lcd.refresh(); // need to refresh display after setting pixels or writing strings |
legstar85 | 2:e0091b5311f1 | 74 | wait(0.5); |
legstar85 | 2:e0091b5311f1 | 75 | |
legstar85 | 2:e0091b5311f1 | 76 | float T = tmp102.get_temperature(); // read temperature and print to lcd |
legstar85 | 2:e0091b5311f1 | 77 | length = sprintf(buffer,"T = %.2f C",T); |
legstar85 | 2:e0091b5311f1 | 78 | if (length <= 14) |
legstar85 | 2:e0091b5311f1 | 79 | lcd.printString(buffer,0,2); |
legstar85 | 2:e0091b5311f1 | 80 | lcd.refresh(); // need to refresh display after setting pixels or writing strings |
legstar85 | 2:e0091b5311f1 | 81 | wait(2.0); |
eencae | 0:21a200b880d7 | 82 | |
eencae | 0:21a200b880d7 | 83 | } |
eencae | 0:21a200b880d7 | 84 | |
eencae | 0:21a200b880d7 | 85 | } |
eencae | 0:21a200b880d7 | 86 | |
eencae | 0:21a200b880d7 | 87 | void init_serial() { |
legstar85 | 2:e0091b5311f1 | 88 | pc.baud(115200); // set to highest baud - ensure terminal software matches |
eencae | 0:21a200b880d7 | 89 | } |
eencae | 0:21a200b880d7 | 90 | |
eencae | 0:21a200b880d7 | 91 | void init_K64F() |
eencae | 0:21a200b880d7 | 92 | { |
legstar85 | 2:e0091b5311f1 | 93 | r_led = 1; // on-board LEDs are active-low, so set pin high to turn them off. |
legstar85 | 2:e0091b5311f1 | 94 | g_led = 1; // on-board LEDs are active-low, so set pin high to turn them off. |
legstar85 | 2:e0091b5311f1 | 95 | b_led = 1; // on-board LEDs are active-low, so set pin high to turn them off. |
eencae | 0:21a200b880d7 | 96 | |
legstar85 | 2:e0091b5311f1 | 97 | sw2.mode(PullNone); // since the on-board switches have external pull-ups, we should disable the internal pull-down |
legstar85 | 2:e0091b5311f1 | 98 | sw3.mode(PullNone); // resistors that are enabled by default using InterruptIn |
eencae | 0:21a200b880d7 | 99 | |
eencae | 0:21a200b880d7 | 100 | } |