Extension to serial communication. GetInput() method that takes string input from serial device. Work still in progress.
Bluetooth.cpp@0:31203426e0fb, 2021-09-21 (annotated)
- Committer:
- nzupcic
- Date:
- Tue Sep 21 16:45:49 2021 +0000
- Revision:
- 0:31203426e0fb
1st commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nzupcic | 0:31203426e0fb | 1 | #include "Bluetooth.h" |
nzupcic | 0:31203426e0fb | 2 | |
nzupcic | 0:31203426e0fb | 3 | Bluetooth::Bluetooth(PinName tx, PinName rx) : Serial(tx, rx){ |
nzupcic | 0:31203426e0fb | 4 | } |
nzupcic | 0:31203426e0fb | 5 | |
nzupcic | 0:31203426e0fb | 6 | char Bluetooth::GetKey(void){ |
nzupcic | 0:31203426e0fb | 7 | return Serial::getc(); |
nzupcic | 0:31203426e0fb | 8 | } |
nzupcic | 0:31203426e0fb | 9 | |
nzupcic | 0:31203426e0fb | 10 | void Bluetooth::PutKey(char key){ |
nzupcic | 0:31203426e0fb | 11 | Serial::putc(key); |
nzupcic | 0:31203426e0fb | 12 | } |
nzupcic | 0:31203426e0fb | 13 | |
nzupcic | 0:31203426e0fb | 14 | string Bluetooth::GetStringInput(void){ |
nzupcic | 0:31203426e0fb | 15 | char value[100]; |
nzupcic | 0:31203426e0fb | 16 | int index=0; |
nzupcic | 0:31203426e0fb | 17 | char ch; |
nzupcic | 0:31203426e0fb | 18 | do{ |
nzupcic | 0:31203426e0fb | 19 | if (Serial::readable()){ |
nzupcic | 0:31203426e0fb | 20 | ch = Serial::getc(); |
nzupcic | 0:31203426e0fb | 21 | if (index<100) |
nzupcic | 0:31203426e0fb | 22 | value[index++]=ch; |
nzupcic | 0:31203426e0fb | 23 | } |
nzupcic | 0:31203426e0fb | 24 | } while (ch!='\r'); |
nzupcic | 0:31203426e0fb | 25 | value[index]='\x0'; |
nzupcic | 0:31203426e0fb | 26 | return (value); |
nzupcic | 0:31203426e0fb | 27 | } |
nzupcic | 0:31203426e0fb | 28 | |
nzupcic | 0:31203426e0fb | 29 | void Bluetooth::Init(void){ |
nzupcic | 0:31203426e0fb | 30 | state = false; |
nzupcic | 0:31203426e0fb | 31 | Bluetooth::printf("\nEnter ON to turn on the light!\n"); |
nzupcic | 0:31203426e0fb | 32 | } |
nzupcic | 0:31203426e0fb | 33 | |
nzupcic | 0:31203426e0fb | 34 | void Bluetooth::AskUser(string msgString){ |
nzupcic | 0:31203426e0fb | 35 | if (msgString == "ON\r" || msgString == "on\r" || msgString == "On\r"){ |
nzupcic | 0:31203426e0fb | 36 | state = true; |
nzupcic | 0:31203426e0fb | 37 | } |
nzupcic | 0:31203426e0fb | 38 | else if(msgString == "OFF\r" || msgString == "off\r" || msgString == "Off\r" || msgString == "0\r"){ |
nzupcic | 0:31203426e0fb | 39 | state = false; |
nzupcic | 0:31203426e0fb | 40 | } |
nzupcic | 0:31203426e0fb | 41 | } |
nzupcic | 0:31203426e0fb | 42 | |
nzupcic | 0:31203426e0fb | 43 | float Bluetooth::ReturnAnswer(float value){ |
nzupcic | 0:31203426e0fb | 44 | if(state){ |
nzupcic | 0:31203426e0fb | 45 | Bluetooth::printf("\nEnter brightness level [%%]: "); |
nzupcic | 0:31203426e0fb | 46 | value = atof((Bluetooth::GetStringInput()).c_str()); |
nzupcic | 0:31203426e0fb | 47 | if(value < 0 || value > 100){ |
nzupcic | 0:31203426e0fb | 48 | Bluetooth::printf("\nWARNING: Brightness can be set from 1 to 100 [%%]!"); |
nzupcic | 0:31203426e0fb | 49 | Bluetooth::printf("\nEnter brightness level [%%]: "); |
nzupcic | 0:31203426e0fb | 50 | } |
nzupcic | 0:31203426e0fb | 51 | else if(value == 0){ |
nzupcic | 0:31203426e0fb | 52 | Bluetooth::printf("\nLight is switched off\n"); |
nzupcic | 0:31203426e0fb | 53 | Bluetooth::Init(); |
nzupcic | 0:31203426e0fb | 54 | return 0; |
nzupcic | 0:31203426e0fb | 55 | } |
nzupcic | 0:31203426e0fb | 56 | else{ |
nzupcic | 0:31203426e0fb | 57 | Bluetooth::printf("\nPress enter to change the brightness! or type Off or 0 to switch the light off\n"); |
nzupcic | 0:31203426e0fb | 58 | return value/100; |
nzupcic | 0:31203426e0fb | 59 | }; |
nzupcic | 0:31203426e0fb | 60 | } |
nzupcic | 0:31203426e0fb | 61 | else{ |
nzupcic | 0:31203426e0fb | 62 | Bluetooth::printf("\nLight is switched off\n"); |
nzupcic | 0:31203426e0fb | 63 | Bluetooth::Init(); |
nzupcic | 0:31203426e0fb | 64 | return 0; |
nzupcic | 0:31203426e0fb | 65 | }; |
nzupcic | 0:31203426e0fb | 66 | } |
nzupcic | 0:31203426e0fb | 67 | |
nzupcic | 0:31203426e0fb | 68 | float Bluetooth::State(void){ |
nzupcic | 0:31203426e0fb | 69 | float value; |
nzupcic | 0:31203426e0fb | 70 | Bluetooth::AskUser(Bluetooth::GetStringInput()); |
nzupcic | 0:31203426e0fb | 71 | return ReturnAnswer(value); |
nzupcic | 0:31203426e0fb | 72 | } |