This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func handleNilWithoutGuardMethod1(str: String?){ | |
if str == nil { | |
print("str is nil") | |
return | |
} | |
let s = str! | |
print("s in not nill here \(s)") | |
//do other things here | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func handleNilWithoutGuardMethod2(str: String?){ | |
if let s = str { | |
print("s in not nil here \(s)") | |
//do other things here | |
}else{ | |
print("str is nil") | |
} | |
} | |
handleNilWithoutGuardMethod2(str: nil) | |
handleNilWithoutGuardMethod2(str: "hello") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func guardMe(str: String?){ | |
guard let s = str else{ | |
print("str is nil") | |
return | |
} | |
print("s in not nil here \(s)") | |
//do other things here | |
} | |
guardMe(str: nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func guardMe2(str: String?){ | |
guard let s = str, s == "hello" else{ | |
print("str is nil or not equal to hello") | |
return | |
} | |
print("s is equal to hello \(s)") | |
//do other things here | |
} | |
guardMe2(str: nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while (mySerial.available()) { | |
Serial.write(mySerial.read()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void sendCommand(const char * command){ | |
Serial.print("Command send :"); | |
Serial.println(command); | |
mySerial.println(command); | |
//wait some time | |
delay(100); | |
char reply[100]; | |
int i = 0; | |
while (mySerial.available()) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void setup() { | |
// put your setup code here, to run once: | |
mySerial.begin(9600); | |
Serial.begin(9600); | |
sendCommand("AT"); | |
sendCommand("AT+ROLE0"); | |
sendCommand("AT+UUID0xFFE0"); | |
sendCommand("AT+CHAR0xFFE1"); | |
sendCommand("AT+NAMEbluino"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void readSerial(){ | |
char reply[50]; | |
int i = 0; | |
while (mySerial.available()) { | |
reply[i] = mySerial.read(); | |
i += 1; | |
} | |
//end the string | |
reply[i] = '\0'; | |
if(strlen(reply) > 0){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void loop() { | |
readSerial(); | |
delay(500); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void writeToBLE(char value) { | |
Serial.print("Writing hex :"); | |
Serial.println(value, HEX); | |
mySerial.write(value); | |
} |
OlderNewer