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: ADXL345 BSP_DISCO_F746NG LCD_DISCO_F746NG TS_DISCO_F746NG
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 #include "TS_DISCO_F746NG.h" 00004 #include "LCD_DISCO_F746NG.h" 00005 #include <string> 00006 using namespace std; 00007 00008 00009 #define BUTTON_DEPRESS_TIME_MIN 0.01000f 00010 00011 00012 LCD_DISCO_F746NG lcd; 00013 TS_DISCO_F746NG ts; 00014 TS_StateTypeDef TS_State; 00015 00016 typedef struct{ 00017 uint16_t x; 00018 uint16_t y; 00019 uint16_t width; 00020 uint16_t height; 00021 }rectData_t; 00022 00023 typedef struct{ 00024 rectData_t rectObj; 00025 uint16_t value; 00026 bool update_flag; 00027 }hFeeder_t; 00028 00029 typedef struct{ 00030 rectData_t rectObj; 00031 bool isPressed; 00032 bool update_flag; 00033 Timer depressTime; 00034 }button_t; 00035 00036 00037 00038 rectData_t but1pos = {48, 68, 96, 60}; 00039 rectData_t but2pos = {192, 68, 96, 60}; 00040 rectData_t but3pos = {338, 68, 96, 60}; 00041 rectData_t hFeed1pos = {48, 152, 384, 30}; 00042 rectData_t hFeed2pos = {48, 212, 384, 30}; 00043 00044 button_t button1 = {but1pos,0,0}; 00045 button_t button2 = {but2pos,0,0}; 00046 button_t button3 = {but3pos,0,0}; 00047 hFeeder_t hFeed1 = {hFeed1pos,0, 0}; 00048 hFeeder_t hFeed2 = {hFeed2pos,0, 0}; 00049 00050 void button(button_t *buttonObj, TS_StateTypeDef *TS_State){ 00051 bool x_is_in =0; 00052 bool y_is_in =0; 00053 00054 uint16_t dyn_x = TS_State->touchX[0]; 00055 uint16_t dyn_y = TS_State->touchY[0]; 00056 00057 if( (dyn_x>buttonObj->rectObj.x) && (dyn_x<(buttonObj->rectObj.x+buttonObj->rectObj.width))) x_is_in = 1; 00058 if( (dyn_y>buttonObj->rectObj.y) && (dyn_y<(buttonObj->rectObj.y+buttonObj->rectObj.height))) y_is_in = 1; 00059 00060 if(x_is_in && y_is_in){ 00061 if(!(buttonObj->isPressed)){ 00062 buttonObj->depressTime.start(); 00063 buttonObj->isPressed = 1; 00064 lcd.SetTextColor(LCD_COLOR_GREEN); 00065 lcd.FillRect(buttonObj->rectObj.x, buttonObj->rectObj.y, buttonObj->rectObj.width, buttonObj->rectObj.height); 00066 } 00067 } 00068 00069 00070 else{ 00071 if(buttonObj->isPressed){ 00072 buttonObj->depressTime.stop(); 00073 buttonObj->isPressed = 0; 00074 if(buttonObj->depressTime.read()>BUTTON_DEPRESS_TIME_MIN){ 00075 buttonObj->update_flag=1; 00076 00077 } 00078 buttonObj->depressTime.reset(); 00079 lcd.SetTextColor(LCD_COLOR_RED); 00080 lcd.FillRect(buttonObj->rectObj.x, buttonObj->rectObj.y, buttonObj->rectObj.width, buttonObj->rectObj.height); 00081 } 00082 } 00083 } 00084 00085 00086 void h_feeder(hFeeder_t *feederObj, TS_StateTypeDef *TS_State){ 00087 bool x_is_in =0; 00088 bool y_is_in =0; 00089 00090 uint16_t dyn_x = TS_State->touchX[0]; 00091 uint16_t dyn_y = TS_State->touchY[0]; 00092 00093 if( (dyn_x>feederObj->rectObj.x) && (dyn_x<(feederObj->rectObj.x+feederObj->rectObj.width))) x_is_in = 1; 00094 if( (dyn_y>feederObj->rectObj.y) && (dyn_y<(feederObj->rectObj.y+feederObj->rectObj.height))) y_is_in = 1; 00095 00096 00097 if(x_is_in && y_is_in){ 00098 uint16_t newFeedValue; 00099 newFeedValue = dyn_x - feederObj->rectObj.x; 00100 if(newFeedValue != feederObj->value){ 00101 feederObj->update_flag = 1; 00102 feederObj->value = newFeedValue; 00103 00104 lcd.SetTextColor(LCD_COLOR_RED); 00105 lcd.FillRect(feederObj->rectObj.x, feederObj->rectObj.y, feederObj->rectObj.width, feederObj->rectObj.height); 00106 lcd.SetTextColor(LCD_COLOR_GREEN); 00107 lcd.FillRect(feederObj->rectObj.x, feederObj->rectObj.y, feederObj->value, feederObj->rectObj.height); 00108 } 00109 } 00110 } 00111 00112 int b1cnt=0, b2cnt=0,b3cnt=0; 00113 00114 00115 //below are the callback functions for the buttons and feeders 00116 00117 //callback when button1 is pressed 00118 void button1_callback(){ 00119 b1cnt++;//keeps count of number of presses 00120 button1.update_flag = 0; 00121 } 00122 00123 //callback when button2 is pressed 00124 void button2_callback(){ 00125 b2cnt++;//keeps count of number of presses 00126 button2.update_flag = 0; 00127 } 00128 00129 //callback when button3 is pressed 00130 void button3_callback(){ 00131 b3cnt++;//keeps count of number of presses 00132 button3.update_flag = 0; 00133 } 00134 00135 //callback on feeder1 update 00136 void hFeeder1_callback(){ 00137 00138 hFeed1.update_flag = 0; 00139 } 00140 00141 //callback on feeder2 update 00142 void hFeeder2_callback(){ 00143 00144 00145 hFeed2.update_flag = 0; 00146 } 00147 00148 00149 00150 00151 int main(){ 00152 00153 uint8_t status; 00154 00155 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"WOLLONGONG UNIVERSITY", CENTER_MODE); 00156 Thread::wait(1000); 00157 00158 status = ts.Init(lcd.GetXSize(), lcd.GetYSize()); 00159 if (status != TS_OK) { 00160 lcd.Clear(LCD_COLOR_RED); 00161 lcd.SetBackColor(LCD_COLOR_RED); 00162 lcd.SetTextColor(LCD_COLOR_WHITE); 00163 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE); 00164 } else { 00165 lcd.Clear(LCD_COLOR_BLUE); 00166 lcd.SetBackColor(LCD_COLOR_BLUE); 00167 lcd.SetTextColor(LCD_COLOR_WHITE); 00168 lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE); 00169 } 00170 00171 lcd.SetBackColor(LCD_COLOR_YELLOW); 00172 lcd.SetTextColor(LCD_COLOR_RED); 00173 00174 lcd.Clear(LCD_COLOR_WHITE); 00175 00176 00177 lcd.SetTextColor(LCD_COLOR_RED); 00178 lcd.FillRect(but1pos.x, but1pos.y, but1pos.width, but1pos.height); 00179 lcd.FillRect(but2pos.x, but2pos.y, but2pos.width, but2pos.height); 00180 lcd.FillRect(but3pos.x, but3pos.y, but3pos.width, but3pos.height); 00181 00182 lcd.FillRect(hFeed1pos.x, hFeed1pos.y, hFeed1pos.width, hFeed1pos.height); 00183 lcd.FillRect(hFeed2pos.x, hFeed2pos.y, hFeed2pos.width, hFeed2pos.height); 00184 00185 Thread::wait(1000); 00186 00187 00188 while(1){ 00189 00190 ts.ResetTouchData(&TS_State); 00191 ts.GetState(&TS_State); 00192 //if (TS_State.touchDetected) 00193 { 00194 h_feeder(&hFeed1, &TS_State); 00195 h_feeder(&hFeed2, &TS_State); 00196 button(&button1, &TS_State); 00197 button(&button2, &TS_State); 00198 button(&button3, &TS_State); 00199 00200 } 00201 00202 if(hFeed1.update_flag)hFeeder1_callback(); 00203 if(hFeed2.update_flag)hFeeder2_callback(); 00204 if(button1.update_flag) button1_callback(); 00205 if(button2.update_flag) button2_callback(); 00206 if(button3.update_flag) button3_callback(); 00207 00208 printf("GUI values are \t\t%d,%d,%d\t%d,%d\t(%d,%d)\r\n", b1cnt, b2cnt,b3cnt,hFeed1.value, hFeed2.value,TS_State.touchX[0],TS_State.touchY[0]); 00209 //printf("feeder values are (%d,%d) %f,%f,%f %d,%d\r\n", TS_State.touchX[0],TS_State.touchY[0],button1.depressTime.read(), button2.depressTime.read(),button3.depressTime.read(),hFeed1.value, hFeed2.value); 00210 00211 } 00212 } 00213 00214 00215 00216 ///////////////////////////////////////////////////////////////////// 00217 //ACCELEROMETER TEST 00218 00219 #include "ADXL345.h" 00220 00221 ADXL345 accelerometer(D11,D12,D13,D10); 00222 Serial pc(USBTX, USBRX); 00223 00224 int main2() { 00225 00226 int readings[3] = {0, 0, 0}; 00227 00228 pc.printf("Starting ADXL345 test...\n"); 00229 pc.printf("Device ID is: 0x%02x\n", accelerometer.getDevId()); 00230 00231 //Go into standby mode to configure the device. 00232 accelerometer.setPowerControl(0x00); 00233 00234 //Full resolution, +/-16g, 4mg/LSB. 00235 accelerometer.setDataFormatControl(0x0B); 00236 00237 //3.2kHz data rate. 00238 accelerometer.setDataRate(ADXL345_3200HZ); 00239 00240 //Measurement mode. 00241 accelerometer.setPowerControl(0x08); 00242 00243 while (1) { 00244 00245 wait(0.1); 00246 00247 accelerometer.getOutput(readings); 00248 00249 //13-bit, sign extended values. 00250 pc.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]); 00251 00252 } 00253 00254 } 00255 00256 00257 00258 00259
Generated on Fri Jul 15 2022 12:48:46 by
1.7.2