Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: DmTouch_UniGraphic UniGraphic mbed
main.cpp
00001 //MiniProject Day 7 00002 //If tapped, the mbed will either read from a temperature sensor or the compass 00003 #include "stdio.h" 00004 #include "mbed.h" 00005 #include "string" 00006 #include "ILI932x.h" 00007 #include "DmTouch.h" 00008 00009 00010 #include "Arial12x12.h" 00011 #include "Arial24x23.h" 00012 //#include "pavement_48x34.h" 00013 #include "mbed.h" 00014 #include "compass_sensor.h" 00015 #include "temp_sensor.h" 00016 #include "acceler_sensor.h" 00017 char orient=3; //orientation of the LCD and touch screen 00018 Serial pc(USBTX, USBRX); 00019 I2C i2c_port(p9, p10); // configures pins 9,10 for I2C communication with external sensors 00020 float temp2; 00021 int avg; 00022 int choice = 0; //choice for the buttons on the touch screen: 1 = acceleration, 2 = compass, 3 = temperature 00023 //const int addr1 = 0x53<<1; 00024 DigitalOut led1(LED1); // configures mbed internal LED 1 00025 DigitalOut led2(LED2); // configures mbed internal LED 2 00026 DigitalIn interupt(p20); // sets accelerometer interupt as digital input at pin 20; 00027 Timer sensor_timer; 00028 DmTouch touch(DmTouch::DM_TFT24_104, p5, p6, p7, p8, p11); 00029 PinName dataBus[]= {p30, p29, p28, p27, p26, p25, p24, p23}; 00030 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 00031 bool down, lastDown; 00032 uint16_t tx, ty; 00033 Ticker inter_t; //Ticker for the touch screen 00034 Timer t; 00035 Timeout Response; 00036 float accel_x,accel_y,accel_mag; 00037 float compass_h; //compass heading 00038 float main_data[2]; 00039 00040 void UI() //the general UI code for the touch screen 00041 { 00042 myLCD.set_font((unsigned char*) Arial24x23); 00043 myLCD.locate(60, 100); 00044 myLCD.printf("A"); //accleration label 00045 myLCD.circle(60,100,30,Blue); //acceleration button 00046 myLCD.locate(160, 100); 00047 myLCD.printf("C"); //compass label 00048 myLCD.circle(160,100,30,Green); //compass button 00049 myLCD.locate(260, 100); 00050 myLCD.printf("T"); //temp. label 00051 myLCD.circle(260,100,30,Red); //temp. button 00052 } 00053 00054 void button() 00055 { 00056 myLCD.cls(); //reset 00057 UI(); 00058 if (choice == 1) //acceleration 00059 { 00060 myLCD.locate(60, 100); 00061 myLCD.printf("A"); 00062 myLCD.fillcircle(60,100,30,Blue); 00063 getacceleration(main_data); 00064 accel_x = main_data[0]; 00065 accel_y = main_data[1]; 00066 accel_mag = sqrt((accel_x*accel_x)+(accel_y*accel_y)); 00067 myLCD.locate(80,150); 00068 myLCD.printf("%f m/s^2",accel_mag); 00069 00070 } 00071 if (choice == 2) //compass 00072 { 00073 myLCD.locate(160, 100); 00074 myLCD.printf("C"); 00075 myLCD.fillcircle(160,100,30,Green); 00076 compass_h = compass_n(); 00077 myLCD.locate(80, 150); 00078 myLCD.printf("%f degrees N", compass_h); 00079 //compass 00080 } 00081 if (choice == 3) //temperature 00082 { 00083 myLCD.locate(260, 100); 00084 myLCD.printf("T"); 00085 myLCD.fillcircle(260,100,30,Red); 00086 temp2 = temperature(); 00087 myLCD.locate(80, 150); 00088 myLCD.printf("%f C", temp2); 00089 } 00090 } 00091 00092 int main() { 00093 myLCD.set_orientation(orient); //orientation of the screen 00094 myLCD.set_font((unsigned char*) Arial24x23); //font 00095 touch.setOrientation(orient); //orientation of the touch screen 00096 down = false; //down portion of the touch screen 00097 lastDown = false; //last down -- what down was last time 00098 tx = (uint16_t)0; //x-coordinates of the touch screen 00099 ty = (uint16_t)0; //y-coordinates of the touch screen 00100 //char sensorData[1]; 00101 led1 = 0; 00102 led2 = 0; 00103 t.start(); 00104 UI(); //sets the the UI 00105 touch.init(); //initializes touch screen 00106 compass_config(); 00107 temperature_config(); 00108 configure_acceleration(); 00109 while(1){ 00110 //myLCD.foreground(Black); 00111 //myLCD.background(White); 00112 00113 t.reset(); 00114 while(t.read()<10) 00115 { 00116 touch.readTouchData(tx, ty, down); //reads in touch screen coordinates 00117 if (down) 00118 { 00119 if(tx >= 30 && tx <= 90 && ty >= 70 && ty <= 130) //if the acceleration button is pressed 00120 { 00121 if (choice == 2 || choice == 3) //if other buttons were pushed before 00122 { 00123 myLCD.cls(); //reset 00124 UI(); 00125 } 00126 myLCD.fillcircle(60,100,30,Blue); //establish which button is pushed -- "highlight it" 00127 myLCD.locate(60, 100); 00128 myLCD.printf("A"); 00129 choice = 1; 00130 } 00131 if(tx >= 130 && tx <= 190 && ty >= 70 && ty <= 130) //if the compass button is pressed 00132 { 00133 if (choice == 1 || choice == 3) //if other buttons were pushed before 00134 { 00135 myLCD.cls(); //reset 00136 UI(); 00137 } 00138 myLCD.fillcircle(160,100,30,Green); //establish which button is pushed 00139 myLCD.locate(160, 100); 00140 myLCD.printf("C"); 00141 choice = 2; 00142 00143 } 00144 if(tx >= 230 && tx <= 290 && ty >= 70 && ty <= 130) //if the temperature button is pressed 00145 { 00146 if (choice == 1 || choice == 2) //if any other buttons were pushed before 00147 { 00148 myLCD.cls(); //reset 00149 UI(); 00150 } 00151 myLCD.fillcircle(260,100,30,Red); //establish which button is pushed 00152 myLCD.locate(260, 100); 00153 myLCD.printf("T"); 00154 choice = 3; 00155 } 00156 } 00157 else if (lastDown) 00158 { 00159 button(); 00160 } 00161 wait(0.040); 00162 lastDown = down; 00163 } 00164 inter_t.attach(&button,10); 00165 } 00166 } 00167 00168 00169 00170
Generated on Wed Jul 13 2022 03:19:26 by
1.7.2