-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
358 additions
and
13 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
#include "blite.h" | ||
|
||
int Blite::getIO(const char * io){ | ||
if (io == "io1") { | ||
return IO1; | ||
} else if (io =="io2") { | ||
return IO2; | ||
} else if (io == "scl") { | ||
return I2C_SCL; | ||
} else if (io == "sda"){ | ||
return I2C_SDA; | ||
} | ||
return -1; | ||
|
||
} | ||
void Blite::reversePolarityM12(){ | ||
this->defineM12(false); | ||
} | ||
void Blite::reversePolarityM34(){ | ||
this->defineM34(false); | ||
} | ||
void Blite::moveForward(){ | ||
analogWrite(m1,speed); | ||
digitalWrite(m2,LOW); | ||
analogWrite(m4,speed); | ||
digitalWrite(m3,LOW); | ||
} | ||
void Blite::moveBackward(){ | ||
analogWrite(m2,speed); | ||
digitalWrite(m1,LOW); | ||
analogWrite(m3,speed); | ||
digitalWrite(m4,LOW); | ||
} | ||
void Blite::turnRight(){ | ||
digitalWrite(m1,LOW); | ||
digitalWrite(m2,LOW); | ||
analogWrite(m4,speed); | ||
digitalWrite(m3,LOW); | ||
} | ||
void Blite::turnLeft(){ | ||
analogWrite(m1,speed); | ||
digitalWrite(m2,LOW); | ||
digitalWrite(m3,LOW); | ||
digitalWrite(m4,LOW); | ||
} | ||
void Blite::setSpeed(int s){ | ||
this->speed = s ; | ||
} | ||
bool Blite::connectWiFi(const char *username, const char *password){ | ||
WiFi.disconnect(); | ||
WiFi.mode(WIFI_STA); | ||
int retry = 0; | ||
while (retry <= 20) { | ||
if (WiFi.status() != WL_CONNECTED) { | ||
Serial.println("Trying to connect to wifi"); | ||
WiFi.begin(username,password); | ||
WiFi.waitForConnectResult(); | ||
delay(1000); | ||
} else { | ||
Serial.println("connected to wifi"); | ||
Serial.println(WiFi.localIP()); | ||
return 1; | ||
} | ||
retry++; | ||
} | ||
return 0; | ||
} | ||
bool Blite::smartConnectWiFi(){ | ||
WiFi.disconnect(); | ||
WiFi.mode(WIFI_STA); | ||
WiFiManager wm; | ||
bool res; | ||
res = wm.autoConnect("Buildybee-smart-config","buildybee"); // password protected ap | ||
return res; | ||
} | ||
|
||
bool Blite::APServer() { | ||
if (WiFi.isConnected()){ | ||
if (WiFi.disconnect()){ | ||
return 0; | ||
} | ||
} | ||
const char* ssid = "buidybee_rc_car"; | ||
IPAddress local_IP(192, 168, 4, 1); | ||
// We set a Gateway IP address | ||
IPAddress gateway(192, 168, 4, 1); | ||
IPAddress subnet(255, 255, 255, 0); | ||
// Connecting WiFi | ||
WiFi.softAPConfig(local_IP,gateway,subnet); | ||
WiFi.mode(WIFI_AP); | ||
return WiFi.softAP(ssid); | ||
} | ||
bool Blite::buttonPressed() { | ||
return !digitalRead(SW1); | ||
} | ||
|
||
void Blite::setup(){ | ||
pinMode(SW1,INPUT_PULLUP); | ||
pinMode(M1,OUTPUT); | ||
pinMode(M2,OUTPUT); | ||
pinMode(M3,OUTPUT); | ||
pinMode(M4,OUTPUT); | ||
pinMode(LED_BUILTIN, OUTPUT); | ||
digitalWrite(LED_BUILTIN, HIGH); | ||
this->defineM12(true); | ||
this->defineM34(true); | ||
WiFi.disconnect(); | ||
WiFi.mode(WIFI_OFF); | ||
|
||
|
||
} | ||
|
||
void Blite::glowLed(bool s){ | ||
if (s){ | ||
digitalWrite(LED_BUILTIN, LOW); | ||
} else{ | ||
digitalWrite(LED_BUILTIN, HIGH); | ||
} | ||
|
||
} | ||
|
||
void Blite::blinkLed(int c){ | ||
for (int i=0;i<c;i++){ | ||
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); | ||
delay(500); | ||
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); | ||
delay(500); | ||
} | ||
} | ||
|
||
int Blite::readADC(){ | ||
return analogRead(ADC1); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#ifndef BLITE_H_ | ||
#define BLITE_H_ | ||
|
||
#if defined(ESP8266) | ||
|
||
#define IO1 12 | ||
#define IO2 13 | ||
#define SW1 16 | ||
|
||
#define M1 0 | ||
#define M2 2 | ||
#define M3 14 | ||
#define M4 15 | ||
|
||
#define I2C_SCL 5 | ||
#define I2C_SDA 4 | ||
|
||
#define ADC1 A0 | ||
|
||
#include <ESP8266WiFi.h> | ||
#include <ESP8266WebServer.h> | ||
#include <WiFiManager.h> | ||
|
||
class Blite { | ||
public: | ||
void setup(); | ||
bool APServer(); | ||
bool connectWiFi(const char * username, const char * password); | ||
bool smartConnectWiFi(); | ||
void moveForward(); | ||
void moveBackward(); | ||
void turnRight(); | ||
void turnLeft(); | ||
void setSpeed(int speed); | ||
|
||
void reversePolarityM12(); | ||
void reversePolarityM34(); | ||
|
||
int getIO(const char * io); | ||
bool buttonPressed(); | ||
void glowLed(bool s); | ||
void blinkLed(int c); | ||
int readADC(); | ||
|
||
private: | ||
int m1,m2,m3,m4,speed; | ||
void defineM12(bool polarity){ | ||
if (polarity){ | ||
this->m1 = M1; | ||
this->m2 = M2; | ||
} else { | ||
this->m2 = M1; | ||
this->m1 = M2; | ||
} | ||
}; | ||
void defineM34(bool polarity){ | ||
if (polarity){ | ||
this->m3 = M3; | ||
this->m4 = M4; | ||
} else { | ||
this->m4 = M3; | ||
this->m3 = M4; | ||
} | ||
}; | ||
}; | ||
#endif | ||
#endif |
Oops, something went wrong.