![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Mini Project 10: Displaying stuff from day 7
Dependencies: DmTouch_UniGraphic UniGraphic mbed
main.cpp@12:bd1d030d5c30, 2017-01-17 (annotated)
- Committer:
- swescott17
- Date:
- Tue Jan 17 20:55:08 2017 +0000
- Revision:
- 12:bd1d030d5c30
- Parent:
- 11:482a9abbc448
- Child:
- 13:b3637adeaa2a
Ticker completed
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; |
swescott17 | 11:482a9abbc448 | 35 | float compass_h; //compass heading |
swescott17 | 9:0f4c86e7218a | 36 | |
swescott17 | 10:ee5708e7a36a | 37 | void UI() //the general UI code for the touch screen |
swescott17 | 9:0f4c86e7218a | 38 | { |
swescott17 | 9:0f4c86e7218a | 39 | myLCD.set_font((unsigned char*) Arial24x23); |
swescott17 | 4:cb3a18844abc | 40 | myLCD.locate(60, 100); |
swescott17 | 10:ee5708e7a36a | 41 | myLCD.printf("A"); //accleration label |
swescott17 | 10:ee5708e7a36a | 42 | myLCD.circle(60,100,30,Blue); //acceleration button |
swescott17 | 4:cb3a18844abc | 43 | myLCD.locate(160, 100); |
swescott17 | 10:ee5708e7a36a | 44 | myLCD.printf("C"); //compass label |
swescott17 | 10:ee5708e7a36a | 45 | myLCD.circle(160,100,30,Green); //compass button |
swescott17 | 4:cb3a18844abc | 46 | myLCD.locate(260, 100); |
swescott17 | 10:ee5708e7a36a | 47 | myLCD.printf("T"); //temp. label |
swescott17 | 10:ee5708e7a36a | 48 | myLCD.circle(260,100,30,Red); //temp. button |
swescott17 | 9:0f4c86e7218a | 49 | } |
swescott17 | 12:bd1d030d5c30 | 50 | |
swescott17 | 12:bd1d030d5c30 | 51 | void button() |
swescott17 | 12:bd1d030d5c30 | 52 | { |
swescott17 | 12:bd1d030d5c30 | 53 | myLCD.cls(); //reset |
swescott17 | 12:bd1d030d5c30 | 54 | UI(); |
swescott17 | 12:bd1d030d5c30 | 55 | } |
swescott17 | 9:0f4c86e7218a | 56 | |
swescott17 | 9:0f4c86e7218a | 57 | int main() { |
swescott17 | 10:ee5708e7a36a | 58 | myLCD.set_orientation(orient); //orientation of the screen |
swescott17 | 10:ee5708e7a36a | 59 | myLCD.set_font((unsigned char*) Arial24x23); //font |
swescott17 | 10:ee5708e7a36a | 60 | touch.setOrientation(orient); //orientation of the touch screen |
swescott17 | 10:ee5708e7a36a | 61 | down = false; //down portion of the touch screen |
swescott17 | 10:ee5708e7a36a | 62 | lastDown = false; //last down -- what down was last time |
swescott17 | 10:ee5708e7a36a | 63 | tx = (uint16_t)0; //x-coordinates of the touch screen |
swescott17 | 10:ee5708e7a36a | 64 | ty = (uint16_t)0; //y-coordinates of the touch screen |
swescott17 | 9:0f4c86e7218a | 65 | //char sensorData[1]; |
swescott17 | 9:0f4c86e7218a | 66 | led1 = 0; |
swescott17 | 9:0f4c86e7218a | 67 | led2 = 0; |
swescott17 | 12:bd1d030d5c30 | 68 | t.start(); |
swescott17 | 10:ee5708e7a36a | 69 | UI(); //sets the the UI |
swescott17 | 10:ee5708e7a36a | 70 | touch.init(); //initializes touch screen |
swescott17 | 11:482a9abbc448 | 71 | compass_config(); |
swescott17 | 11:482a9abbc448 | 72 | temperature_config(); |
swescott17 | 9:0f4c86e7218a | 73 | //configure_acceleration(); |
swescott17 | 9:0f4c86e7218a | 74 | while(1){ |
swescott17 | 9:0f4c86e7218a | 75 | //myLCD.foreground(Black); |
swescott17 | 12:bd1d030d5c30 | 76 | //myLCD.background(White); |
swescott17 | 12:bd1d030d5c30 | 77 | |
swescott17 | 12:bd1d030d5c30 | 78 | t.reset(); |
swescott17 | 12:bd1d030d5c30 | 79 | while(t.read()<10) |
swescott17 | 12:bd1d030d5c30 | 80 | { |
swescott17 | 12:bd1d030d5c30 | 81 | touch.readTouchData(tx, ty, down); //reads in touch screen coordinates |
swescott17 | 12:bd1d030d5c30 | 82 | if (down) |
swescott17 | 12:bd1d030d5c30 | 83 | { |
swescott17 | 12:bd1d030d5c30 | 84 | if(tx >= 30 && tx <= 90 && ty >= 70 && ty <= 130) //if the acceleration button is pressed |
swescott17 | 9:0f4c86e7218a | 85 | { |
swescott17 | 12:bd1d030d5c30 | 86 | if (choice == 2 || choice == 3) //if other buttons were pushed before |
swescott17 | 12:bd1d030d5c30 | 87 | { |
swescott17 | 12:bd1d030d5c30 | 88 | myLCD.cls(); //reset |
swescott17 | 12:bd1d030d5c30 | 89 | UI(); |
swescott17 | 12:bd1d030d5c30 | 90 | } |
swescott17 | 12:bd1d030d5c30 | 91 | myLCD.fillcircle(60,100,30,Blue); //establish which button is pushed -- "highlight it" |
swescott17 | 12:bd1d030d5c30 | 92 | myLCD.locate(60, 100); |
swescott17 | 12:bd1d030d5c30 | 93 | myLCD.printf("A"); |
swescott17 | 12:bd1d030d5c30 | 94 | choice = 1; |
swescott17 | 9:0f4c86e7218a | 95 | } |
swescott17 | 12:bd1d030d5c30 | 96 | if(tx >= 130 && tx <= 190 && ty >= 70 && ty <= 130) //if the compass button is pressed |
swescott17 | 9:0f4c86e7218a | 97 | { |
swescott17 | 12:bd1d030d5c30 | 98 | if (choice == 1 || choice == 3) //if other buttons were pushed before |
swescott17 | 12:bd1d030d5c30 | 99 | { |
swescott17 | 12:bd1d030d5c30 | 100 | myLCD.cls(); //reset |
swescott17 | 12:bd1d030d5c30 | 101 | UI(); |
swescott17 | 12:bd1d030d5c30 | 102 | } |
swescott17 | 12:bd1d030d5c30 | 103 | myLCD.fillcircle(160,100,30,Green); //establish which button is pushed |
swescott17 | 12:bd1d030d5c30 | 104 | myLCD.locate(160, 100); |
swescott17 | 12:bd1d030d5c30 | 105 | myLCD.printf("C"); |
swescott17 | 12:bd1d030d5c30 | 106 | choice = 2; |
swescott17 | 12:bd1d030d5c30 | 107 | |
swescott17 | 9:0f4c86e7218a | 108 | } |
swescott17 | 12:bd1d030d5c30 | 109 | if(tx >= 230 && tx <= 290 && ty >= 70 && ty <= 130) //if the temperature button is pressed |
swescott17 | 12:bd1d030d5c30 | 110 | { |
swescott17 | 12:bd1d030d5c30 | 111 | if (choice == 1 || choice == 2) //if any other buttons were pushed before |
swescott17 | 12:bd1d030d5c30 | 112 | { |
swescott17 | 12:bd1d030d5c30 | 113 | myLCD.cls(); //reset |
swescott17 | 12:bd1d030d5c30 | 114 | UI(); |
swescott17 | 12:bd1d030d5c30 | 115 | } |
swescott17 | 12:bd1d030d5c30 | 116 | myLCD.fillcircle(260,100,30,Red); //establish which button is pushed |
swescott17 | 12:bd1d030d5c30 | 117 | myLCD.locate(260, 100); |
swescott17 | 12:bd1d030d5c30 | 118 | myLCD.printf("T"); |
swescott17 | 12:bd1d030d5c30 | 119 | choice = 3; |
swescott17 | 12:bd1d030d5c30 | 120 | } |
swescott17 | 9:0f4c86e7218a | 121 | } |
swescott17 | 12:bd1d030d5c30 | 122 | else if (lastDown) |
swescott17 | 12:bd1d030d5c30 | 123 | { |
swescott17 | 12:bd1d030d5c30 | 124 | if (choice == 1) //acceleration |
swescott17 | 12:bd1d030d5c30 | 125 | { |
swescott17 | 12:bd1d030d5c30 | 126 | myLCD.locate(60, 100); |
swescott17 | 12:bd1d030d5c30 | 127 | myLCD.printf("A"); |
swescott17 | 12:bd1d030d5c30 | 128 | myLCD.fillcircle(60,100,30,Blue); |
swescott17 | 12:bd1d030d5c30 | 129 | //acceleration |
swescott17 | 12:bd1d030d5c30 | 130 | } |
swescott17 | 12:bd1d030d5c30 | 131 | if (choice == 2) //compass |
swescott17 | 12:bd1d030d5c30 | 132 | { |
swescott17 | 12:bd1d030d5c30 | 133 | myLCD.locate(160, 100); |
swescott17 | 12:bd1d030d5c30 | 134 | myLCD.printf("C"); |
swescott17 | 12:bd1d030d5c30 | 135 | myLCD.fillcircle(160,100,30,Green); |
swescott17 | 12:bd1d030d5c30 | 136 | compass_h = compass_n(); |
swescott17 | 12:bd1d030d5c30 | 137 | myLCD.locate(80, 150); |
swescott17 | 12:bd1d030d5c30 | 138 | myLCD.printf("%f degrees N", compass_h); |
swescott17 | 12:bd1d030d5c30 | 139 | //compass |
swescott17 | 12:bd1d030d5c30 | 140 | } |
swescott17 | 12:bd1d030d5c30 | 141 | if (choice == 3) //temperature |
swescott17 | 12:bd1d030d5c30 | 142 | { |
swescott17 | 12:bd1d030d5c30 | 143 | myLCD.locate(260, 100); |
swescott17 | 12:bd1d030d5c30 | 144 | myLCD.printf("T"); |
swescott17 | 12:bd1d030d5c30 | 145 | myLCD.fillcircle(260,100,30,Red); |
swescott17 | 12:bd1d030d5c30 | 146 | temp2 = temperature(); |
swescott17 | 12:bd1d030d5c30 | 147 | myLCD.locate(80, 150); |
swescott17 | 12:bd1d030d5c30 | 148 | myLCD.printf("%f C", temp2); |
swescott17 | 12:bd1d030d5c30 | 149 | } |
swescott17 | 9:0f4c86e7218a | 150 | } |
swescott17 | 12:bd1d030d5c30 | 151 | wait(0.040); |
swescott17 | 12:bd1d030d5c30 | 152 | lastDown = down; |
swescott17 | 12:bd1d030d5c30 | 153 | } |
swescott17 | 12:bd1d030d5c30 | 154 | inter_t.attach(&button,10); |
swescott17 | 9:0f4c86e7218a | 155 | } |
swescott17 | 9:0f4c86e7218a | 156 | //getacceleration(); |
swescott17 | 1:20661421fa4e | 157 | /*wait(1); |
swescott17 | 0:1ebe73e062a7 | 158 | i2c_port.start(); // forces start condition |
swescott17 | 0:1ebe73e062a7 | 159 | if (interupt == 1){ |
swescott17 | 0:1ebe73e062a7 | 160 | i2c_port.start(); |
swescott17 | 0:1ebe73e062a7 | 161 | i2c_port.write(addr1); |
swescott17 | 0:1ebe73e062a7 | 162 | i2c_port.write(0x30|0x80); |
swescott17 | 0:1ebe73e062a7 | 163 | i2c_port.stop(); |
swescott17 | 0:1ebe73e062a7 | 164 | i2c_port.read(addr1,sensorData,1); |
swescott17 | 0:1ebe73e062a7 | 165 | wait(1); //checks for interupt signal from accelerometer |
swescott17 | 0:1ebe73e062a7 | 166 | if (sensorData[0] & 0x40){ //if one tap |
swescott17 | 0:1ebe73e062a7 | 167 | led1 = 1; |
swescott17 | 0:1ebe73e062a7 | 168 | temp2 = temperature(); |
swescott17 | 0:1ebe73e062a7 | 169 | pc.printf("%f", temp2); |
swescott17 | 0:1ebe73e062a7 | 170 | } |
swescott17 | 0:1ebe73e062a7 | 171 | if (sensorData[0] & 0x20) { //if two taps |
swescott17 | 0:1ebe73e062a7 | 172 | led2 = 1; |
swescott17 | 0:1ebe73e062a7 | 173 | compass_n(avg); |
swescott17 | 1:20661421fa4e | 174 | }*/ |
swescott17 | 0:1ebe73e062a7 | 175 | } |