![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
a demo of GUI on DISCOF7 consisting of a few buttons and feeders
Dependencies: ADXL345 BSP_DISCO_F746NG LCD_DISCO_F746NG TS_DISCO_F746NG
main.cpp@2:76522cfa03cd, 2016-08-05 (annotated)
- Committer:
- habiburrahman
- Date:
- Fri Aug 05 23:49:42 2016 +0000
- Revision:
- 2:76522cfa03cd
- Parent:
- 1:4f356fb65677
- Child:
- 3:afd2bb1331a4
cleaned unnecessary comments.. proper tabbing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
habiburrahman | 0:122b5c3cbf4d | 1 | #include "mbed.h" |
habiburrahman | 0:122b5c3cbf4d | 2 | #include "TS_DISCO_F746NG.h" |
habiburrahman | 0:122b5c3cbf4d | 3 | #include "LCD_DISCO_F746NG.h" |
habiburrahman | 0:122b5c3cbf4d | 4 | #include "ESP8266.h" |
habiburrahman | 0:122b5c3cbf4d | 5 | #include "TCPSocketConnection.h" |
habiburrahman | 0:122b5c3cbf4d | 6 | #include <string> |
habiburrahman | 0:122b5c3cbf4d | 7 | using namespace std; |
habiburrahman | 0:122b5c3cbf4d | 8 | |
habiburrahman | 0:122b5c3cbf4d | 9 | |
habiburrahman | 0:122b5c3cbf4d | 10 | |
habiburrahman | 0:122b5c3cbf4d | 11 | LCD_DISCO_F746NG lcd; |
habiburrahman | 0:122b5c3cbf4d | 12 | TS_DISCO_F746NG ts; |
habiburrahman | 0:122b5c3cbf4d | 13 | TS_StateTypeDef TS_State; |
habiburrahman | 0:122b5c3cbf4d | 14 | |
habiburrahman | 0:122b5c3cbf4d | 15 | |
habiburrahman | 0:122b5c3cbf4d | 16 | |
habiburrahman | 0:122b5c3cbf4d | 17 | bool touch_test_rectangle(uint16_t tx, uint16_t ty, uint16_t x, uint16_t y, uint16_t width, uint16_t height){ |
habiburrahman | 0:122b5c3cbf4d | 18 | |
habiburrahman | 0:122b5c3cbf4d | 19 | bool x_is_in =0; |
habiburrahman | 0:122b5c3cbf4d | 20 | bool y_is_in = 0; |
habiburrahman | 0:122b5c3cbf4d | 21 | |
habiburrahman | 0:122b5c3cbf4d | 22 | if( (tx>x) && (tx<(x+width))) x_is_in = 1; |
habiburrahman | 0:122b5c3cbf4d | 23 | if( (ty>y) && (ty<(y+height))) y_is_in = 1; |
habiburrahman | 0:122b5c3cbf4d | 24 | |
habiburrahman | 0:122b5c3cbf4d | 25 | bool buttest; |
habiburrahman | 0:122b5c3cbf4d | 26 | buttest = x_is_in & y_is_in; |
habiburrahman | 0:122b5c3cbf4d | 27 | |
habiburrahman | 0:122b5c3cbf4d | 28 | return (bool) buttest; |
habiburrahman | 0:122b5c3cbf4d | 29 | } |
habiburrahman | 0:122b5c3cbf4d | 30 | |
habiburrahman | 0:122b5c3cbf4d | 31 | |
habiburrahman | 0:122b5c3cbf4d | 32 | uint16_t h_feeder(uint16_t tx, uint16_t ty,uint16_t x, uint16_t y, uint16_t width, uint16_t height){ |
habiburrahman | 0:122b5c3cbf4d | 33 | |
habiburrahman | 0:122b5c3cbf4d | 34 | bool x_is_in =0; |
habiburrahman | 0:122b5c3cbf4d | 35 | bool y_is_in =0; |
habiburrahman | 0:122b5c3cbf4d | 36 | |
habiburrahman | 0:122b5c3cbf4d | 37 | if( (tx>x) && (tx<(x+width))) x_is_in = 1; |
habiburrahman | 0:122b5c3cbf4d | 38 | if( (ty>y) && (ty<(y+height))) y_is_in = 1; |
habiburrahman | 0:122b5c3cbf4d | 39 | |
habiburrahman | 0:122b5c3cbf4d | 40 | bool buttest; |
habiburrahman | 0:122b5c3cbf4d | 41 | buttest = x_is_in & y_is_in; |
habiburrahman | 0:122b5c3cbf4d | 42 | |
habiburrahman | 0:122b5c3cbf4d | 43 | if(buttest){ |
habiburrahman | 0:122b5c3cbf4d | 44 | return tx - x; |
habiburrahman | 0:122b5c3cbf4d | 45 | } |
habiburrahman | 0:122b5c3cbf4d | 46 | |
habiburrahman | 0:122b5c3cbf4d | 47 | |
habiburrahman | 0:122b5c3cbf4d | 48 | } |
habiburrahman | 0:122b5c3cbf4d | 49 | |
habiburrahman | 0:122b5c3cbf4d | 50 | bool but1=0,but2=0,but3=0; |
habiburrahman | 0:122b5c3cbf4d | 51 | uint16_t feed1_dist = 0; |
habiburrahman | 0:122b5c3cbf4d | 52 | uint16_t feed2_dist = 0; |
habiburrahman | 0:122b5c3cbf4d | 53 | |
habiburrahman | 1:4f356fb65677 | 54 | int main2() |
habiburrahman | 0:122b5c3cbf4d | 55 | { |
habiburrahman | 0:122b5c3cbf4d | 56 | |
habiburrahman | 0:122b5c3cbf4d | 57 | uint16_t x, y; |
habiburrahman | 0:122b5c3cbf4d | 58 | uint8_t text[30]; |
habiburrahman | 0:122b5c3cbf4d | 59 | uint8_t status; |
habiburrahman | 0:122b5c3cbf4d | 60 | uint8_t idx; |
habiburrahman | 0:122b5c3cbf4d | 61 | uint8_t cleared = 0; |
habiburrahman | 0:122b5c3cbf4d | 62 | uint8_t prev_nb_touches = 0; |
habiburrahman | 0:122b5c3cbf4d | 63 | |
habiburrahman | 0:122b5c3cbf4d | 64 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN DEMO", CENTER_MODE); |
habiburrahman | 0:122b5c3cbf4d | 65 | wait(1); |
habiburrahman | 0:122b5c3cbf4d | 66 | |
habiburrahman | 1:4f356fb65677 | 67 | |
habiburrahman | 1:4f356fb65677 | 68 | |
habiburrahman | 1:4f356fb65677 | 69 | |
habiburrahman | 0:122b5c3cbf4d | 70 | status = ts.Init(lcd.GetXSize(), lcd.GetYSize()); |
habiburrahman | 0:122b5c3cbf4d | 71 | if (status != TS_OK) { |
habiburrahman | 0:122b5c3cbf4d | 72 | lcd.Clear(LCD_COLOR_RED); |
habiburrahman | 0:122b5c3cbf4d | 73 | lcd.SetBackColor(LCD_COLOR_RED); |
habiburrahman | 0:122b5c3cbf4d | 74 | lcd.SetTextColor(LCD_COLOR_WHITE); |
habiburrahman | 0:122b5c3cbf4d | 75 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE); |
habiburrahman | 0:122b5c3cbf4d | 76 | } else { |
habiburrahman | 0:122b5c3cbf4d | 77 | lcd.Clear(LCD_COLOR_BLUE); |
habiburrahman | 0:122b5c3cbf4d | 78 | lcd.SetBackColor(LCD_COLOR_BLUE); |
habiburrahman | 0:122b5c3cbf4d | 79 | lcd.SetTextColor(LCD_COLOR_WHITE); |
habiburrahman | 0:122b5c3cbf4d | 80 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE); |
habiburrahman | 0:122b5c3cbf4d | 81 | } |
habiburrahman | 0:122b5c3cbf4d | 82 | |
habiburrahman | 0:122b5c3cbf4d | 83 | lcd.SetBackColor(LCD_COLOR_YELLOW); |
habiburrahman | 0:122b5c3cbf4d | 84 | lcd.SetTextColor(LCD_COLOR_RED); |
habiburrahman | 0:122b5c3cbf4d | 85 | |
habiburrahman | 0:122b5c3cbf4d | 86 | lcd.Clear(LCD_COLOR_WHITE); |
habiburrahman | 0:122b5c3cbf4d | 87 | |
habiburrahman | 0:122b5c3cbf4d | 88 | |
habiburrahman | 0:122b5c3cbf4d | 89 | lcd.SetTextColor(LCD_COLOR_RED); |
habiburrahman | 0:122b5c3cbf4d | 90 | lcd.FillRect(48, 68, 96, 60); |
habiburrahman | 0:122b5c3cbf4d | 91 | lcd.FillRect(192, 68, 96, 60); |
habiburrahman | 0:122b5c3cbf4d | 92 | lcd.FillRect(338, 68, 96, 60); |
habiburrahman | 0:122b5c3cbf4d | 93 | |
habiburrahman | 0:122b5c3cbf4d | 94 | lcd.FillRect(48, 152, 384, 30); |
habiburrahman | 0:122b5c3cbf4d | 95 | lcd.FillRect(48, 212, 384, 30); |
habiburrahman | 0:122b5c3cbf4d | 96 | |
habiburrahman | 0:122b5c3cbf4d | 97 | wait(1); |
habiburrahman | 0:122b5c3cbf4d | 98 | |
habiburrahman | 0:122b5c3cbf4d | 99 | |
habiburrahman | 0:122b5c3cbf4d | 100 | while(1){ |
habiburrahman | 0:122b5c3cbf4d | 101 | |
habiburrahman | 0:122b5c3cbf4d | 102 | but1=0; |
habiburrahman | 0:122b5c3cbf4d | 103 | but2=0; |
habiburrahman | 0:122b5c3cbf4d | 104 | but3=0; |
habiburrahman | 0:122b5c3cbf4d | 105 | |
habiburrahman | 0:122b5c3cbf4d | 106 | ts.GetState(&TS_State); |
habiburrahman | 0:122b5c3cbf4d | 107 | if (TS_State.touchDetected) { |
habiburrahman | 0:122b5c3cbf4d | 108 | |
habiburrahman | 0:122b5c3cbf4d | 109 | feed1_dist = h_feeder(TS_State.touchX[0],TS_State.touchY[0],48, 152, 384, 30); |
habiburrahman | 0:122b5c3cbf4d | 110 | feed2_dist = h_feeder(TS_State.touchX[0],TS_State.touchY[0],48, 212, 384, 30); |
habiburrahman | 0:122b5c3cbf4d | 111 | but1 = touch_test_rectangle(TS_State.touchX[0],TS_State.touchY[0], 48, 68, 96, 60); |
habiburrahman | 0:122b5c3cbf4d | 112 | but2 = touch_test_rectangle(TS_State.touchX[0],TS_State.touchY[0], 192, 68, 96, 60); |
habiburrahman | 0:122b5c3cbf4d | 113 | but3 = touch_test_rectangle(TS_State.touchX[0],TS_State.touchY[0], 338, 68, 96, 60); |
habiburrahman | 0:122b5c3cbf4d | 114 | |
habiburrahman | 0:122b5c3cbf4d | 115 | if(feed1_dist>0){ |
habiburrahman | 0:122b5c3cbf4d | 116 | lcd.SetTextColor(LCD_COLOR_RED); |
habiburrahman | 0:122b5c3cbf4d | 117 | lcd.FillRect(48, 152, 384, 30); |
habiburrahman | 0:122b5c3cbf4d | 118 | lcd.SetTextColor(LCD_COLOR_GREEN); |
habiburrahman | 0:122b5c3cbf4d | 119 | lcd.FillRect(48, 152, feed1_dist, 30); |
habiburrahman | 0:122b5c3cbf4d | 120 | } |
habiburrahman | 0:122b5c3cbf4d | 121 | |
habiburrahman | 0:122b5c3cbf4d | 122 | if(feed2_dist>0){ |
habiburrahman | 0:122b5c3cbf4d | 123 | lcd.SetTextColor(LCD_COLOR_RED); |
habiburrahman | 0:122b5c3cbf4d | 124 | lcd.FillRect(48, 212, 384, 30); |
habiburrahman | 0:122b5c3cbf4d | 125 | lcd.SetTextColor(LCD_COLOR_GREEN); |
habiburrahman | 0:122b5c3cbf4d | 126 | lcd.FillRect(48, 212, feed2_dist, 30); |
habiburrahman | 0:122b5c3cbf4d | 127 | } |
habiburrahman | 0:122b5c3cbf4d | 128 | } |
habiburrahman | 0:122b5c3cbf4d | 129 | |
habiburrahman | 0:122b5c3cbf4d | 130 | if(but1){ |
habiburrahman | 0:122b5c3cbf4d | 131 | lcd.SetTextColor(LCD_COLOR_GREEN); |
habiburrahman | 2:76522cfa03cd | 132 | lcd.FillRect(48, 68, 96, 60); |
habiburrahman | 0:122b5c3cbf4d | 133 | } |
habiburrahman | 0:122b5c3cbf4d | 134 | else{ |
habiburrahman | 0:122b5c3cbf4d | 135 | lcd.SetTextColor(LCD_COLOR_RED); |
habiburrahman | 0:122b5c3cbf4d | 136 | lcd.FillRect(48, 68, 96, 60); |
habiburrahman | 0:122b5c3cbf4d | 137 | } |
habiburrahman | 0:122b5c3cbf4d | 138 | |
habiburrahman | 0:122b5c3cbf4d | 139 | if(but2){ |
habiburrahman | 0:122b5c3cbf4d | 140 | lcd.SetTextColor(LCD_COLOR_GREEN); |
habiburrahman | 0:122b5c3cbf4d | 141 | lcd.FillRect(192, 68, 96, 60); |
habiburrahman | 0:122b5c3cbf4d | 142 | } |
habiburrahman | 0:122b5c3cbf4d | 143 | else{ |
habiburrahman | 0:122b5c3cbf4d | 144 | lcd.SetTextColor(LCD_COLOR_RED); |
habiburrahman | 0:122b5c3cbf4d | 145 | lcd.FillRect(192, 68, 96, 60); |
habiburrahman | 0:122b5c3cbf4d | 146 | } |
habiburrahman | 2:76522cfa03cd | 147 | if(but3){ |
habiburrahman | 2:76522cfa03cd | 148 | lcd.SetTextColor(LCD_COLOR_GREEN); |
habiburrahman | 2:76522cfa03cd | 149 | lcd.FillRect(338, 68, 96, 60); |
habiburrahman | 2:76522cfa03cd | 150 | } |
habiburrahman | 2:76522cfa03cd | 151 | else{ |
habiburrahman | 2:76522cfa03cd | 152 | lcd.SetTextColor(LCD_COLOR_RED); |
habiburrahman | 2:76522cfa03cd | 153 | lcd.FillRect(338, 68, 96, 60); |
habiburrahman | 2:76522cfa03cd | 154 | } |
habiburrahman | 0:122b5c3cbf4d | 155 | |
habiburrahman | 0:122b5c3cbf4d | 156 | } |
habiburrahman | 0:122b5c3cbf4d | 157 | } |
habiburrahman | 1:4f356fb65677 | 158 | |
habiburrahman | 1:4f356fb65677 | 159 | |
habiburrahman | 1:4f356fb65677 | 160 | ///////////////////////////////////////////////////////////////////// |
habiburrahman | 1:4f356fb65677 | 161 | //ACCELEROMETER TEST |
habiburrahman | 1:4f356fb65677 | 162 | |
habiburrahman | 1:4f356fb65677 | 163 | #include "ADXL345.h" |
habiburrahman | 1:4f356fb65677 | 164 | |
habiburrahman | 1:4f356fb65677 | 165 | ADXL345 accelerometer(D11,D12,D13,D10); |
habiburrahman | 1:4f356fb65677 | 166 | Serial pc(USBTX, USBRX); |
habiburrahman | 1:4f356fb65677 | 167 | |
habiburrahman | 1:4f356fb65677 | 168 | int main() { |
habiburrahman | 1:4f356fb65677 | 169 | |
habiburrahman | 1:4f356fb65677 | 170 | int readings[3] = {0, 0, 0}; |
habiburrahman | 1:4f356fb65677 | 171 | |
habiburrahman | 1:4f356fb65677 | 172 | pc.printf("Starting ADXL345 test...\n"); |
habiburrahman | 1:4f356fb65677 | 173 | pc.printf("Device ID is: 0x%02x\n", accelerometer.getDevId()); |
habiburrahman | 1:4f356fb65677 | 174 | |
habiburrahman | 1:4f356fb65677 | 175 | //Go into standby mode to configure the device. |
habiburrahman | 1:4f356fb65677 | 176 | accelerometer.setPowerControl(0x00); |
habiburrahman | 1:4f356fb65677 | 177 | |
habiburrahman | 1:4f356fb65677 | 178 | //Full resolution, +/-16g, 4mg/LSB. |
habiburrahman | 1:4f356fb65677 | 179 | accelerometer.setDataFormatControl(0x0B); |
habiburrahman | 1:4f356fb65677 | 180 | |
habiburrahman | 1:4f356fb65677 | 181 | //3.2kHz data rate. |
habiburrahman | 1:4f356fb65677 | 182 | accelerometer.setDataRate(ADXL345_3200HZ); |
habiburrahman | 1:4f356fb65677 | 183 | |
habiburrahman | 1:4f356fb65677 | 184 | //Measurement mode. |
habiburrahman | 1:4f356fb65677 | 185 | accelerometer.setPowerControl(0x08); |
habiburrahman | 1:4f356fb65677 | 186 | |
habiburrahman | 1:4f356fb65677 | 187 | while (1) { |
habiburrahman | 1:4f356fb65677 | 188 | |
habiburrahman | 1:4f356fb65677 | 189 | wait(0.1); |
habiburrahman | 1:4f356fb65677 | 190 | |
habiburrahman | 1:4f356fb65677 | 191 | accelerometer.getOutput(readings); |
habiburrahman | 1:4f356fb65677 | 192 | |
habiburrahman | 1:4f356fb65677 | 193 | //13-bit, sign extended values. |
habiburrahman | 1:4f356fb65677 | 194 | pc.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]); |
habiburrahman | 1:4f356fb65677 | 195 | |
habiburrahman | 1:4f356fb65677 | 196 | } |
habiburrahman | 1:4f356fb65677 | 197 | |
habiburrahman | 1:4f356fb65677 | 198 | } |
habiburrahman | 1:4f356fb65677 | 199 | |
habiburrahman | 1:4f356fb65677 | 200 | |
habiburrahman | 1:4f356fb65677 | 201 | |
habiburrahman | 1:4f356fb65677 | 202 | |
habiburrahman | 0:122b5c3cbf4d | 203 |