![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Mini Project 10: Displaying stuff from day 7
Dependencies: DmTouch_UniGraphic UniGraphic mbed
main.cpp@15:565da8e90073, 2017-01-18 (annotated)
- Committer:
- ezimmerman17
- Date:
- Wed Jan 18 01:08:33 2017 +0000
- Revision:
- 15:565da8e90073
- Parent:
- 14:682bdfcff46a
- Child:
- 16:995c9920f14e
just in case I changed anything;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
swescott17 | 0:1ebe73e062a7 | 1 | //MiniProject Day 7 |
swescott17 | 0:1ebe73e062a7 | 2 | //If tapped, the mbed will either read from a temperature sensor or the compass |
swescott17 | 1:20661421fa4e | 3 | #include "stdio.h" |
swescott17 | 1:20661421fa4e | 4 | #include "mbed.h" |
swescott17 | 1:20661421fa4e | 5 | #include "string" |
swescott17 | 1:20661421fa4e | 6 | #include "ILI932x.h" |
swescott17 | 1:20661421fa4e | 7 | #include "DmTouch.h" |
swescott17 | 0:1ebe73e062a7 | 8 | |
swescott17 | 1:20661421fa4e | 9 | |
swescott17 | 1:20661421fa4e | 10 | #include "Arial12x12.h" |
swescott17 | 1:20661421fa4e | 11 | #include "Arial24x23.h" |
swescott17 | 1:20661421fa4e | 12 | //#include "pavement_48x34.h" |
swescott17 | 0:1ebe73e062a7 | 13 | #include "mbed.h" |
swescott17 | 0:1ebe73e062a7 | 14 | #include "compass_sensor.h" |
swescott17 | 0:1ebe73e062a7 | 15 | #include "temp_sensor.h" |
swescott17 | 0:1ebe73e062a7 | 16 | #include "acceler_sensor.h" |
swescott17 | 10:ee5708e7a36a | 17 | char orient=3; //orientation of the LCD and touch screen |
swescott17 | 0:1ebe73e062a7 | 18 | Serial pc(USBTX, USBRX); |
swescott17 | 0:1ebe73e062a7 | 19 | I2C i2c_port(p9, p10); // configures pins 9,10 for I2C communication with external sensors |
swescott17 | 0:1ebe73e062a7 | 20 | float temp2; |
swescott17 | 0:1ebe73e062a7 | 21 | int avg; |
swescott17 | 10:ee5708e7a36a | 22 | int choice = 0; //choice for the buttons on the touch screen: 1 = acceleration, 2 = compass, 3 = temperature |
swescott17 | 4:cb3a18844abc | 23 | //const int addr1 = 0x53<<1; |
swescott17 | 0:1ebe73e062a7 | 24 | DigitalOut led1(LED1); // configures mbed internal LED 1 |
swescott17 | 0:1ebe73e062a7 | 25 | DigitalOut led2(LED2); // configures mbed internal LED 2 |
swescott17 | 1:20661421fa4e | 26 | DigitalIn interupt(p20); // sets accelerometer interupt as digital input at pin 20; |
ezimmerman17 | 3:dbb843b8d2f4 | 27 | Timer sensor_timer; |
swescott17 | 12:bd1d030d5c30 | 28 | DmTouch touch(DmTouch::DM_TFT24_104, p5, p6, p7, p8, p11); |
swescott17 | 1:20661421fa4e | 29 | PinName dataBus[]= {p30, p29, p28, p27, p26, p25, p24, p23}; |
swescott17 | 1:20661421fa4e | 30 | ILI932x myLCD(BUS_8, dataBus, p15, p17, p16, p14, p20, "myLCD", 240, 320); // Bus 8 bit, bus pin array, CS, RST, DC, WR, RD, name, xpixels, ypixels |
swescott17 | 9:0f4c86e7218a | 31 | bool down, lastDown; |
swescott17 | 9:0f4c86e7218a | 32 | uint16_t tx, ty; |
swescott17 | 12:bd1d030d5c30 | 33 | Ticker inter_t; //Ticker for the touch screen |
swescott17 | 12:bd1d030d5c30 | 34 | Timer t; |
ezimmerman17 | 15:565da8e90073 | 35 | Timeout Response; |
ezimmerman17 | 14:682bdfcff46a | 36 | float accel_x,accel_y,accel_mag; |
swescott17 | 11:482a9abbc448 | 37 | float compass_h; //compass heading |
ezimmerman17 | 13:b3637adeaa2a | 38 | float main_data[2]; |
swescott17 | 9:0f4c86e7218a | 39 | |
swescott17 | 10:ee5708e7a36a | 40 | void UI() //the general UI code for the touch screen |
swescott17 | 9:0f4c86e7218a | 41 | { |
swescott17 | 9:0f4c86e7218a | 42 | myLCD.set_font((unsigned char*) Arial24x23); |
swescott17 | 4:cb3a18844abc | 43 | myLCD.locate(60, 100); |
swescott17 | 10:ee5708e7a36a | 44 | myLCD.printf("A"); //accleration label |
swescott17 | 10:ee5708e7a36a | 45 | myLCD.circle(60,100,30,Blue); //acceleration button |
swescott17 | 4:cb3a18844abc | 46 | myLCD.locate(160, 100); |
swescott17 | 10:ee5708e7a36a | 47 | myLCD.printf("C"); //compass label |
swescott17 | 10:ee5708e7a36a | 48 | myLCD.circle(160,100,30,Green); //compass button |
swescott17 | 4:cb3a18844abc | 49 | myLCD.locate(260, 100); |
swescott17 | 10:ee5708e7a36a | 50 | myLCD.printf("T"); //temp. label |
swescott17 | 10:ee5708e7a36a | 51 | myLCD.circle(260,100,30,Red); //temp. button |
swescott17 | 9:0f4c86e7218a | 52 | } |
swescott17 | 12:bd1d030d5c30 | 53 | |
swescott17 | 12:bd1d030d5c30 | 54 | void button() |
swescott17 | 12:bd1d030d5c30 | 55 | { |
swescott17 | 12:bd1d030d5c30 | 56 | myLCD.cls(); //reset |
swescott17 | 12:bd1d030d5c30 | 57 | UI(); |
swescott17 | 12:bd1d030d5c30 | 58 | } |
swescott17 | 9:0f4c86e7218a | 59 | |
swescott17 | 9:0f4c86e7218a | 60 | int main() { |
swescott17 | 10:ee5708e7a36a | 61 | myLCD.set_orientation(orient); //orientation of the screen |
swescott17 | 10:ee5708e7a36a | 62 | myLCD.set_font((unsigned char*) Arial24x23); //font |
swescott17 | 10:ee5708e7a36a | 63 | touch.setOrientation(orient); //orientation of the touch screen |
swescott17 | 10:ee5708e7a36a | 64 | down = false; //down portion of the touch screen |
swescott17 | 10:ee5708e7a36a | 65 | lastDown = false; //last down -- what down was last time |
swescott17 | 10:ee5708e7a36a | 66 | tx = (uint16_t)0; //x-coordinates of the touch screen |
swescott17 | 10:ee5708e7a36a | 67 | ty = (uint16_t)0; //y-coordinates of the touch screen |
swescott17 | 9:0f4c86e7218a | 68 | //char sensorData[1]; |
swescott17 | 9:0f4c86e7218a | 69 | led1 = 0; |
swescott17 | 9:0f4c86e7218a | 70 | led2 = 0; |
swescott17 | 12:bd1d030d5c30 | 71 | t.start(); |
swescott17 | 10:ee5708e7a36a | 72 | UI(); //sets the the UI |
swescott17 | 10:ee5708e7a36a | 73 | touch.init(); //initializes touch screen |
swescott17 | 11:482a9abbc448 | 74 | compass_config(); |
swescott17 | 11:482a9abbc448 | 75 | temperature_config(); |
ezimmerman17 | 13:b3637adeaa2a | 76 | configure_acceleration(); |
swescott17 | 9:0f4c86e7218a | 77 | while(1){ |
swescott17 | 9:0f4c86e7218a | 78 | //myLCD.foreground(Black); |
swescott17 | 12:bd1d030d5c30 | 79 | //myLCD.background(White); |
swescott17 | 12:bd1d030d5c30 | 80 | |
swescott17 | 12:bd1d030d5c30 | 81 | t.reset(); |
swescott17 | 12:bd1d030d5c30 | 82 | while(t.read()<10) |
swescott17 | 12:bd1d030d5c30 | 83 | { |
swescott17 | 12:bd1d030d5c30 | 84 | touch.readTouchData(tx, ty, down); //reads in touch screen coordinates |
swescott17 | 12:bd1d030d5c30 | 85 | if (down) |
swescott17 | 12:bd1d030d5c30 | 86 | { |
swescott17 | 12:bd1d030d5c30 | 87 | if(tx >= 30 && tx <= 90 && ty >= 70 && ty <= 130) //if the acceleration button is pressed |
swescott17 | 9:0f4c86e7218a | 88 | { |
swescott17 | 12:bd1d030d5c30 | 89 | if (choice == 2 || choice == 3) //if other buttons were pushed before |
swescott17 | 12:bd1d030d5c30 | 90 | { |
swescott17 | 12:bd1d030d5c30 | 91 | myLCD.cls(); //reset |
swescott17 | 12:bd1d030d5c30 | 92 | UI(); |
swescott17 | 12:bd1d030d5c30 | 93 | } |
swescott17 | 12:bd1d030d5c30 | 94 | myLCD.fillcircle(60,100,30,Blue); //establish which button is pushed -- "highlight it" |
swescott17 | 12:bd1d030d5c30 | 95 | myLCD.locate(60, 100); |
swescott17 | 12:bd1d030d5c30 | 96 | myLCD.printf("A"); |
swescott17 | 12:bd1d030d5c30 | 97 | choice = 1; |
swescott17 | 9:0f4c86e7218a | 98 | } |
swescott17 | 12:bd1d030d5c30 | 99 | if(tx >= 130 && tx <= 190 && ty >= 70 && ty <= 130) //if the compass button is pressed |
swescott17 | 9:0f4c86e7218a | 100 | { |
swescott17 | 12:bd1d030d5c30 | 101 | if (choice == 1 || choice == 3) //if other buttons were pushed before |
swescott17 | 12:bd1d030d5c30 | 102 | { |
swescott17 | 12:bd1d030d5c30 | 103 | myLCD.cls(); //reset |
swescott17 | 12:bd1d030d5c30 | 104 | UI(); |
swescott17 | 12:bd1d030d5c30 | 105 | } |
swescott17 | 12:bd1d030d5c30 | 106 | myLCD.fillcircle(160,100,30,Green); //establish which button is pushed |
swescott17 | 12:bd1d030d5c30 | 107 | myLCD.locate(160, 100); |
swescott17 | 12:bd1d030d5c30 | 108 | myLCD.printf("C"); |
swescott17 | 12:bd1d030d5c30 | 109 | choice = 2; |
swescott17 | 12:bd1d030d5c30 | 110 | |
swescott17 | 9:0f4c86e7218a | 111 | } |
swescott17 | 12:bd1d030d5c30 | 112 | if(tx >= 230 && tx <= 290 && ty >= 70 && ty <= 130) //if the temperature button is pressed |
swescott17 | 12:bd1d030d5c30 | 113 | { |
swescott17 | 12:bd1d030d5c30 | 114 | if (choice == 1 || choice == 2) //if any other buttons were pushed before |
swescott17 | 12:bd1d030d5c30 | 115 | { |
swescott17 | 12:bd1d030d5c30 | 116 | myLCD.cls(); //reset |
swescott17 | 12:bd1d030d5c30 | 117 | UI(); |
swescott17 | 12:bd1d030d5c30 | 118 | } |
swescott17 | 12:bd1d030d5c30 | 119 | myLCD.fillcircle(260,100,30,Red); //establish which button is pushed |
swescott17 | 12:bd1d030d5c30 | 120 | myLCD.locate(260, 100); |
swescott17 | 12:bd1d030d5c30 | 121 | myLCD.printf("T"); |
swescott17 | 12:bd1d030d5c30 | 122 | choice = 3; |
swescott17 | 12:bd1d030d5c30 | 123 | } |
swescott17 | 9:0f4c86e7218a | 124 | } |
swescott17 | 12:bd1d030d5c30 | 125 | else if (lastDown) |
swescott17 | 12:bd1d030d5c30 | 126 | { |
swescott17 | 12:bd1d030d5c30 | 127 | if (choice == 1) //acceleration |
swescott17 | 12:bd1d030d5c30 | 128 | { |
swescott17 | 12:bd1d030d5c30 | 129 | myLCD.locate(60, 100); |
swescott17 | 12:bd1d030d5c30 | 130 | myLCD.printf("A"); |
swescott17 | 12:bd1d030d5c30 | 131 | myLCD.fillcircle(60,100,30,Blue); |
ezimmerman17 | 13:b3637adeaa2a | 132 | getacceleration(main_data); |
ezimmerman17 | 14:682bdfcff46a | 133 | accel_x = main_data[0]; |
ezimmerman17 | 14:682bdfcff46a | 134 | accel_y = main_data[1]; |
ezimmerman17 | 14:682bdfcff46a | 135 | accel_mag = sqrt((accel_x*accel_x)+(accel_y*accel_y)); |
ezimmerman17 | 13:b3637adeaa2a | 136 | myLCD.locate(80,150); |
ezimmerman17 | 14:682bdfcff46a | 137 | myLCD.printf("%f m/s^2",accel_mag); |
ezimmerman17 | 15:565da8e90073 | 138 | |
swescott17 | 12:bd1d030d5c30 | 139 | } |
swescott17 | 12:bd1d030d5c30 | 140 | if (choice == 2) //compass |
swescott17 | 12:bd1d030d5c30 | 141 | { |
swescott17 | 12:bd1d030d5c30 | 142 | myLCD.locate(160, 100); |
swescott17 | 12:bd1d030d5c30 | 143 | myLCD.printf("C"); |
swescott17 | 12:bd1d030d5c30 | 144 | myLCD.fillcircle(160,100,30,Green); |
swescott17 | 12:bd1d030d5c30 | 145 | compass_h = compass_n(); |
swescott17 | 12:bd1d030d5c30 | 146 | myLCD.locate(80, 150); |
swescott17 | 12:bd1d030d5c30 | 147 | myLCD.printf("%f degrees N", compass_h); |
swescott17 | 12:bd1d030d5c30 | 148 | //compass |
swescott17 | 12:bd1d030d5c30 | 149 | } |
swescott17 | 12:bd1d030d5c30 | 150 | if (choice == 3) //temperature |
swescott17 | 12:bd1d030d5c30 | 151 | { |
swescott17 | 12:bd1d030d5c30 | 152 | myLCD.locate(260, 100); |
swescott17 | 12:bd1d030d5c30 | 153 | myLCD.printf("T"); |
swescott17 | 12:bd1d030d5c30 | 154 | myLCD.fillcircle(260,100,30,Red); |
swescott17 | 12:bd1d030d5c30 | 155 | temp2 = temperature(); |
swescott17 | 12:bd1d030d5c30 | 156 | myLCD.locate(80, 150); |
swescott17 | 12:bd1d030d5c30 | 157 | myLCD.printf("%f C", temp2); |
swescott17 | 12:bd1d030d5c30 | 158 | } |
swescott17 | 9:0f4c86e7218a | 159 | } |
swescott17 | 12:bd1d030d5c30 | 160 | wait(0.040); |
swescott17 | 12:bd1d030d5c30 | 161 | lastDown = down; |
swescott17 | 12:bd1d030d5c30 | 162 | } |
swescott17 | 12:bd1d030d5c30 | 163 | inter_t.attach(&button,10); |
ezimmerman17 | 15:565da8e90073 | 164 | } |
swescott17 | 0:1ebe73e062a7 | 165 | } |
ezimmerman17 | 15:565da8e90073 | 166 | |
ezimmerman17 | 15:565da8e90073 | 167 | |
ezimmerman17 | 15:565da8e90073 | 168 | |
ezimmerman17 | 15:565da8e90073 | 169 |