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: 4DGL-uLCD-SE EthernetInterface PinDetect mbed-rtos mbed
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 #include "uLCD_4DGL.h" 00004 #include "SongPlayer.h" 00005 #include "PinDetect.h" 00006 #include "string.h" 00007 #include "EthernetInterface.h" 00008 #include "Speaker.h" 00009 00010 //uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; 00011 uLCD_4DGL uLCD(p28, p27, p29); 00012 //SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card 00013 DigitalOut latch(p15); 00014 DigitalOut enable(p16); 00015 //AnalogOut DACout(p18); 00016 //Cycles through different colors on RGB LED 00017 SPI spi(p11, p12, p13); 00018 //Set up IR sensor 00019 AnalogIn IrSensor(p20); 00020 Serial serial(USBTX, USBRX); 00021 00022 SongPlayer mySpeaker(p26); 00023 Speaker NotePlayer(p26); 00024 00025 // ethernet setup 00026 EthernetInterface eth; 00027 00028 DigitalOut myLED1(LED1); 00029 DigitalOut myLED2(LED2); 00030 DigitalOut myLED3(LED3); 00031 DigitalOut myLED4(LED4); 00032 00033 PinDetect pb1(p21); 00034 PinDetect pb2(p22); 00035 PinDetect pb3(p23); 00036 PinDetect pb4(p24); 00037 00038 00039 //void Thread_maintnence(char Thread); 00040 void init_pbs(void); 00041 enum Statetype { Armed =0 ,IR_sensed = 1,Second_Step = 2,Cleared = 3, Alarm_ON = 4}; 00042 Statetype state = Armed; 00043 00044 int Code[4] = {0,0,0,0}; 00045 int CodeCounter = 0; 00046 00047 void pb1_hit_callback (void) { 00048 if ((state == IR_sensed) ||(state == Cleared)) { 00049 NotePlayer.PlayNote(1200.0,0.15,0.1); 00050 Code[CodeCounter] = 1; 00051 CodeCounter++; 00052 wait(0.500); 00053 } 00054 } 00055 void pb2_hit_callback (void) { 00056 if ((state == IR_sensed) ||(state == Cleared)){ 00057 NotePlayer.PlayNote(1200.0,0.15,0.1); 00058 Code[CodeCounter] = 2; 00059 CodeCounter++; 00060 wait(0.500); 00061 } 00062 } 00063 void pb3_hit_callback (void) { 00064 if ((state == IR_sensed) ||(state == Cleared)) { 00065 NotePlayer.PlayNote(1200.0,0.15,0.5); 00066 Code[CodeCounter] = 3; 00067 CodeCounter++; 00068 wait(0.500); 00069 } 00070 } 00071 void pb4_hit_callback (void) { 00072 if ((state == IR_sensed) ||(state == Cleared)) { 00073 NotePlayer.PlayNote(1200.0,0.15,0.5); 00074 Code[CodeCounter] = 4; 00075 CodeCounter++; 00076 wait(0.500); 00077 } 00078 } 00079 00080 Mutex LCD_Access; 00081 Semaphore Consul_Access(5); 00082 float IrVoltage = 0.0; 00083 00084 float note[18]= {1568.0,1396.9}; 00085 float duration[18]= {0.48,0.24}; 00086 00087 00088 00089 void init_LCD(); 00090 void RGB_LED(int red, int green, int blue); 00091 00092 void Speaker_thread(void const *args) { 00093 while (1) { 00094 if (state == Alarm_ON) { 00095 mySpeaker.PlaySong(note,duration); 00096 Thread::wait(1000); 00097 } 00098 } 00099 } 00100 00101 void uLCD_thread(void const *args) { 00102 int Change = 99; 00103 while(1) { 00104 00105 if (Change != state) { 00106 Change = state; 00107 switch (state) { 00108 case Armed: 00109 LCD_Access.lock(); 00110 uLCD.cls(); 00111 uLCD.color(WHITE); 00112 uLCD.text_width(2); 00113 uLCD.text_height(2); 00114 uLCD.printf(" ARMED\r\n"); 00115 uLCD.text_width(1); 00116 uLCD.text_height(1); 00117 00118 if (eth.getIPAddress() == "\0") { 00119 uLCD.printf("\n\n No Internet connection"); 00120 } 00121 else { 00122 uLCD.printf("\n\nConnected to the Internet\n"); 00123 uLCD.printf("IP Address:%sabcd ", eth.getIPAddress()); 00124 } 00125 LCD_Access.unlock(); 00126 break; 00127 case IR_sensed: 00128 LCD_Access.lock(); 00129 uLCD.cls(); 00130 uLCD.printf("\nSensor triggred \n"); 00131 uLCD.printf("\n Enter the code ..."); 00132 LCD_Access.unlock(); 00133 for (int i=30; i>=0; --i) { 00134 if (state == IR_sensed) { 00135 LCD_Access.lock(); 00136 uLCD.text_width(4); 00137 uLCD.text_height(4); 00138 uLCD.color(RED); 00139 uLCD.locate(1,2); 00140 uLCD.printf("%2D",i); 00141 LCD_Access.unlock(); 00142 Thread::wait(1000); 00143 } 00144 } 00145 if (state == IR_sensed) { 00146 state = Alarm_ON; 00147 } 00148 00149 break; 00150 case Second_Step: 00151 LCD_Access.lock(); 00152 uLCD.cls(); 00153 uLCD.color(BLUE); 00154 uLCD.printf("\nPleas enter code from text massage \n"); 00155 LCD_Access.unlock(); 00156 break; 00157 case Alarm_ON: 00158 LCD_Access.lock(); 00159 uLCD.cls(); 00160 uLCD.color(RED); 00161 uLCD.text_width(1.5); //4X size text 00162 uLCD.text_height(1.5); 00163 uLCD.printf("\nALARM IS ON \nText message sent \n"); 00164 LCD_Access.unlock(); 00165 break; 00166 case Cleared: 00167 LCD_Access.lock(); 00168 uLCD.cls(); 00169 uLCD.color(GREEN); 00170 uLCD.printf("\n\nAccess Granted. \n\n"); 00171 LCD_Access.unlock(); 00172 break; 00173 } 00174 } 00175 Thread::wait(500); 00176 } 00177 } 00178 00179 void IR_thread(void const *args) { 00180 00181 Timer t; 00182 t.start(); 00183 00184 while(1) { 00185 00186 if (state == Armed) { 00187 IrVoltage=IrSensor.read(); 00188 if (IrVoltage <= 0.1) { //if value just nois reset timer 00189 t.reset(); 00190 state = Armed; 00191 } 00192 if (t.read() >= 5) { //wait 5 seconds to make sure that sense someone 00193 state = IR_sensed; 00194 } 00195 Thread::wait(1000); 00196 } 00197 else { 00198 //nothing to do for this thread make space for others 00199 Thread::wait(1000); 00200 } 00201 } 00202 } 00203 00204 void Shiftbright_thread(void const *args){ 00205 spi.format(16,0); 00206 spi.frequency(500000); 00207 enable=0; 00208 latch=0; 00209 00210 while(1) { 00211 switch (state) { 00212 case Armed: 00213 for (int i = 0; i <= 50; i++) { 00214 RGB_LED( i, 0, 0); 00215 Thread::wait(10); 00216 } 00217 for (int i = 50; i >= 0; i--) { 00218 RGB_LED( i, 0, 0); 00219 Thread::wait(10); 00220 } 00221 break; 00222 case IR_sensed: 00223 RGB_LED( 100, 0, 0); 00224 Thread::wait(500); 00225 RGB_LED( 0, 0, 0); 00226 break; 00227 case Alarm_ON: 00228 for (int i = 0; i <= 100; i++) { 00229 RGB_LED( i, i/2, 0); 00230 Thread::wait(10); 00231 } 00232 for (int i = 100; i >= 0; i--) { 00233 RGB_LED( i, i/3, 0); 00234 Thread::wait(10); 00235 } 00236 break; 00237 case Cleared: 00238 RGB_LED( 0, 100, 0); 00239 break; 00240 } 00241 Thread::wait(1000); 00242 } 00243 } 00244 00245 void CheckCode_thread(void const *args) { 00246 while(1){ 00247 switch (state) { 00248 case Armed: 00249 break; 00250 case IR_sensed: 00251 if ((Code[0] == 1) && (Code[1] == 2) && (Code[2] == 3) && (Code[3] == 4)) { 00252 Thread::wait(300); 00253 state = Cleared; 00254 Code[0] = Code[1] = Code[2] = Code[3] =0; 00255 CodeCounter = 0; 00256 break; 00257 } 00258 else if ((Code[0] != 0) && (Code[1] != 0) && (Code[2] != 0) && (Code[3] != 0)) { 00259 Thread::wait(300); 00260 Code[0] = Code[1] = Code[2] = Code[3] =0; 00261 NotePlayer.PlayNote(200,1,0.5); 00262 state = Alarm_ON; 00263 } 00264 //Thread::wait(100); 00265 break; 00266 case Alarm_ON: 00267 Thread::wait(1000); 00268 break; 00269 case Cleared: 00270 //serial.printf("CC from check: %d%d%d%d\n\r",Code[0],Code[1],Code[2],Code[3]); 00271 if ((Code[0] == 4) && (Code[1] == 3) && (Code[2] == 2) && (Code[3] == 1)) { 00272 Thread::wait(300); 00273 Code[0] = Code[1] = Code[2] = Code[3] =0; 00274 CodeCounter = 0; 00275 state = Armed; 00276 break; 00277 } 00278 else if ((Code[0] != 0) && (Code[1] != 0) && (Code[2] != 0) && (Code[3] != 0)) { 00279 Thread::wait(300); 00280 Code[0] = Code[1] = Code[2] = Code[3] =0; 00281 NotePlayer.PlayNote(200,1,0.5); 00282 CodeCounter = 0; 00283 //state = Alarm_ON; 00284 } 00285 //Thread::wait(100); 00286 break; 00287 } 00288 } 00289 } 00290 00291 void Ethernet_thread(void const *args) { 00292 00293 int Change = 99; 00294 00295 eth.init(); //Use DHCP 00296 eth.connect(); 00297 serial.printf("IP Address is: %s", eth.getIPAddress()); 00298 00299 while(1) { 00300 if (Change != state) { 00301 00302 Change = state; 00303 switch (state) { 00304 case Armed: 00305 //add code that verifies connection every so often 00306 Thread::wait(1000); 00307 break; 00308 case Second_Step: 00309 Thread::wait(1000); 00310 break; 00311 case Alarm_ON: { 00312 // send alert 00313 char buffer[300]; 00314 int ret; 00315 TCPSocketConnection sock; 00316 sock.connect("dreamphysix.com", 80); 00317 00318 char http_cmd[] = "GET http://dreamphysix.com/alert.php?authcode=0e9cae34a0 HTTP/1.0\n\n"; 00319 sock.send_all(http_cmd, sizeof(http_cmd)-1); 00320 00321 00322 while (true) { 00323 ret = sock.receive(buffer, sizeof(buffer)-1); 00324 if (ret <= 0) 00325 break; 00326 buffer[ret] = '\0'; 00327 Consul_Access.wait(); 00328 serial.printf("Received %d chars from server:\n%s\n", ret, buffer); 00329 Consul_Access.release(); 00330 } 00331 sock.close(); 00332 } 00333 break; 00334 case Cleared: 00335 //nothing to do for this thread make space for others 00336 Thread::wait(1000); 00337 break; 00338 case IR_sensed: 00339 //nothing to do for this thread make space for others 00340 Thread::wait(1000); 00341 break; 00342 } 00343 } 00344 } 00345 } 00346 00347 void LCD_Code_Enter_Thread(void const *args) { 00348 int LineHight = 120; 00349 int LineWidth = 10; 00350 int SpaceWidth = 5; 00351 int MidPoint = 127/2; 00352 while(1) { 00353 switch (state) { 00354 case Armed: 00355 break; 00356 case IR_sensed: 00357 case Cleared: 00358 00359 Thread::wait(500); 00360 while((state == IR_sensed)||(state == Cleared)) { 00361 LCD_Access.lock(); 00362 //dusplay four lines 00363 uLCD.line(MidPoint-2*(LineWidth+SpaceWidth), LineHight, MidPoint- 2*SpaceWidth-LineWidth, LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color) 00364 uLCD.line(MidPoint-LineWidth-SpaceWidth, LineHight, MidPoint-SpaceWidth, LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color) 00365 uLCD.line(MidPoint+SpaceWidth, LineHight, MidPoint+SpaceWidth+LineWidth, LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color) 00366 uLCD.line(MidPoint+2*SpaceWidth+LineWidth, LineHight, MidPoint+2*(SpaceWidth+LineWidth), LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color) 00367 uLCD.locate(5,14); 00368 uLCD.text_width(1); //4X size text 00369 uLCD.text_height(1); 00370 uLCD.color(WHITE); 00371 uLCD.printf("%d %d %d %d",Code[0],Code[1],Code[2],Code[3]); 00372 LCD_Access.unlock(); 00373 } 00374 case Second_Step: 00375 break; 00376 case Alarm_ON: 00377 break; 00378 } 00379 } 00380 } 00381 00382 int main() { 00383 00384 serial.baud(921600); 00385 init_pbs(); 00386 00387 Thread Ethernetthread(Ethernet_thread); 00388 wait(5); //Give the Ethernet connection some time to set up 00389 00390 Thread LCDthread(uLCD_thread); 00391 Thread IRthread(IR_thread); 00392 Thread Shiftbright(Shiftbright_thread); 00393 Thread Speakerthread(Speaker_thread); 00394 Thread CheckCode(CheckCode_thread); 00395 Thread LCD_CodeEnterThread(LCD_Code_Enter_Thread); 00396 00397 while (true) { } 00398 } 00399 00400 void init_LCD() { 00401 uLCD.baudrate(3000000); 00402 uLCD.background_color(BLACK); 00403 } 00404 00405 void RGB_LED(int red, int green, int blue) { 00406 00407 unsigned int low_color=0; 00408 unsigned int high_color=0; 00409 high_color=(blue<<4)|((red&0x3C0)>>6); 00410 low_color=(((red&0x3F)<<10)|(green)); 00411 spi.write(high_color); 00412 spi.write(low_color); 00413 latch=1; 00414 latch=0; 00415 00416 } 00417 void init_pbs(void) { 00418 // Use internal pullups for the three pushbuttons 00419 pb1.mode(PullUp); 00420 pb2.mode(PullUp); 00421 pb3.mode(PullUp); 00422 pb4.mode(PullUp); 00423 // Delay for initial pullup to take effect 00424 wait(.01); 00425 // Setup Interrupt callback functions for a pb hit 00426 pb1.attach_deasserted(&pb1_hit_callback); 00427 pb2.attach_deasserted(&pb2_hit_callback); 00428 pb3.attach_deasserted(&pb3_hit_callback); 00429 pb4.attach_deasserted(&pb4_hit_callback); 00430 // Start sampling pb inputs using interrupts 00431 pb1.setSampleFrequency(); 00432 pb2.setSampleFrequency(); 00433 pb3.setSampleFrequency(); 00434 pb4.setSampleFrequency(); 00435 // pushbuttons now setup and running 00436 }
Generated on Mon Jul 18 2022 22:46:36 by
1.7.2