Skip to content

Commit

Permalink
comms between board and phone: speed value
Browse files Browse the repository at this point in the history
  • Loading branch information
Thalaivar committed May 28, 2018
1 parent db37ef3 commit bdad2c3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
24 changes: 23 additions & 1 deletion FatBoard_recv/FatBoard_recv.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ typedef enum conn_stat {

conn_status connection = NO_CONN;
bool hotspot = false;
int16_t speedValue = 1000;

WiFiServer board(port);
WiFiClient phone;
Expand All @@ -43,6 +44,19 @@ void setup() {
void loop() {
while(connection != CONN_VER) checkConnection();

while(connection == CONN_VER){
if(!phone.connected()) killBoard();
else{
int msg_size = phone.available();
yield();
if(msg_size > 0){
byte value_msg[2];
msg_size = phone.readBytes(value_msg, msg_size);
speedValue = (value_msg[1] << 8) | value_msg[0];
Serial.println(speedValue);
}
}
}
}

void setupWiFi() {
Expand All @@ -63,12 +77,20 @@ void checkConnection() {
msg_size = phone.readBytes(conn_msg_raw, msg_size);
yield();
int16_t conn_msg = (conn_msg_raw[1] << 8) | conn_msg_raw[0];
if(conn_msg == CONN_MSG) Serial.println("HI!");
if(conn_msg == CONN_MSG){
connection = CONN_VER;
phone.write("V");
}
phone.flush();
}
}
}
}
}

void killBoard(){
connection = NO_CONN;
speedValue = 1000;
Serial.println("KILL");
}

Binary file not shown.
8 changes: 7 additions & 1 deletion Fatboard/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ class SettingsViewController: UIViewController {
}

if board.status == 1{
sender.setImage(#imageLiteral(resourceName: "connected.png"), for: .normal)
board.checkConnection()
if board.status == 2{
sender.setImage(#imageLiteral(resourceName: "connected.png"), for: .normal)
}
}

if board.status == 2{
sender.setImage(#imageLiteral(resourceName: "connected.png"), for: .normal)
}

else{
sender.setImage(#imageLiteral(resourceName: "disconnected.png"), for: .normal)
}
Expand Down
30 changes: 29 additions & 1 deletion Fatboard/SpeedViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ extension Numeric {

class Board {
var speedValue:Int = 1000
/* STATUS --- VALUE
not connected --- 0
wifi connect --- 1
connected --- 2
board running --- 3
board stopped --- 4
*/
var status = 0

let port = Int32(345)
Expand All @@ -42,8 +49,28 @@ class Board {
}

func checkConnection(){
if self.status == 1 {
self.status = 1

if(self.status == 1) {
try! self.boardWiFi?.write(from: self.connectMessage.data)
var message = try! self.boardWiFi?.readString()
var tries = 0;
while(message != "V"){
message = try! self.boardWiFi?.readString()
tries += 1

if(tries > 1000){
self.status = 1
break
}
}
self.status = 2
}
}

func sendSpeedValue(value: Int16){
if(self.status == 2) {
try! self.boardWiFi?.write(from: value.data)
}
}
}
Expand Down Expand Up @@ -72,6 +99,7 @@ class SpeedViewController: UIViewController {

@IBAction func changeValue(_ sender: UISlider) {
speedLevel.text = String(Int(speedSlider.value))
board.sendSpeedValue(value: Int16(speedSlider.value))
}


Expand Down

0 comments on commit bdad2c3

Please sign in to comment.