Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 61b6883

Browse files
committed
Initial version of transcriber, works but can't write and read :(
1 parent 8ec0b43 commit 61b6883

File tree

6 files changed

+178
-11
lines changed

6 files changed

+178
-11
lines changed

src/FirebaseObject.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ FirebaseObject::FirebaseObject(const char* data) : data_{data} {
2525
// See: https://github.com/bblanchon/ArduinoJson/issues/279
2626
}
2727

28-
bool FirebaseObject::getBool(const String& path) {
28+
bool FirebaseObject::getBool(const String& path) const {
2929
JsonVariant variant = getJsonVariant(path);
3030
if (!variant.is<bool>()) {
3131
error_ = "failed to convert to bool";
@@ -34,7 +34,7 @@ bool FirebaseObject::getBool(const String& path) {
3434
return static_cast<bool>(variant);
3535
}
3636

37-
int FirebaseObject::getInt(const String& path) {
37+
int FirebaseObject::getInt(const String& path) const {
3838
JsonVariant variant = getJsonVariant(path);
3939
if (!variant.is<int>() && !variant.is<float>()) {
4040
error_ = "failed to convert to number";
@@ -43,7 +43,7 @@ int FirebaseObject::getInt(const String& path) {
4343
return static_cast<int>(variant);
4444
}
4545

46-
float FirebaseObject::getFloat(const String& path) {
46+
float FirebaseObject::getFloat(const String& path) const {
4747
JsonVariant variant = getJsonVariant(path);
4848
if (!variant.is<float>() && !variant.is<int>()) {
4949
error_ = "failed to convert to number";
@@ -52,7 +52,7 @@ float FirebaseObject::getFloat(const String& path) {
5252
return static_cast<float>(variant);
5353
}
5454

55-
String FirebaseObject::getString(const String& path) {
55+
String FirebaseObject::getString(const String& path) const {
5656
JsonVariant variant = getJsonVariant(path);
5757
if (!variant.is<const char *>()) {
5858
error_ = "failed to convert to string";
@@ -61,7 +61,7 @@ String FirebaseObject::getString(const String& path) {
6161
return static_cast<const char*>(variant);
6262
}
6363

64-
JsonVariant FirebaseObject::getJsonVariant(const String& path) {
64+
JsonVariant FirebaseObject::getJsonVariant(const String& path) const {
6565
String key(path);
6666
char* start = &key[0];
6767
char* end = start + key.length();

src/FirebaseObject.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,35 @@ class FirebaseObject {
4040
* \param optional path in the JSON object.
4141
* \return result as a bool.
4242
*/
43-
bool getBool(const String& path = "");
43+
bool getBool(const String& path = "") const;
4444

4545
/**
4646
* Return the value as an int.
4747
* \param optional path in the JSON object.
4848
* \return result as an integer.
4949
*/
50-
int getInt(const String& path = "");
50+
int getInt(const String& path = "") const;
5151

5252
/**
5353
* Return the value as a float.
5454
* \param optional path in the JSON object.
5555
* \return result as a float.
5656
*/
57-
float getFloat(const String& path = "");
57+
float getFloat(const String& path = "") const;
5858

5959
/**
6060
* Return the value as a String.
6161
* \param optional path in the JSON object.
6262
* \return result as a String.
6363
*/
64-
String getString(const String& path = "");
64+
String getString(const String& path = "") const;
6565

6666
/**
6767
* Return the value as a JsonVariant.
6868
* \param optional path in the JSON object.
6969
* \return result as a JsonVariant.
7070
*/
71-
JsonVariant getJsonVariant(const String& path = "");
71+
JsonVariant getJsonVariant(const String& path = "") const;
7272

7373

7474
/**
@@ -93,7 +93,7 @@ class FirebaseObject {
9393
String data_;
9494
StaticJsonBuffer<FIREBASE_JSONBUFFER_SIZE> buffer_;
9595
JsonVariant json_;
96-
String error_;
96+
mutable String error_;
9797
};
9898

9999
#endif // FIREBASE_OBJECT_H

src/Transcriber.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "thing/Transcriber.h"

src/thing/Config.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace thing {
2+
3+
struct Config {
4+
std::string host;
5+
std::string auth;
6+
std::string path;
7+
8+
std::string wifi_ssid;
9+
std::string wifi_key;
10+
11+
int pin_digital_out;
12+
int pin_digital_in;
13+
int pin_analog_out;
14+
int pin_analog_in;
15+
};
16+
17+
}

src/thing/Transcriber.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
#include "thing/Transcriber.h"
3+
4+
namespace thing {
5+
namespace {
6+
const char* kSubPathDigitalOut = "/digital_out";
7+
const char* kSubPathDigitalIn = "/digital_in";
8+
const char* kSubPathAnalogOut = "/analog_out";
9+
const char* kSubPathAnalogIn = "/analog_in";
10+
} // namespace
11+
12+
13+
Transcriber::Transcriber(const Config& config) {
14+
Init(config);
15+
}
16+
17+
void Transcriber::UpdateConfig(const Config& config) {
18+
Init(config);
19+
}
20+
21+
void Transcriber::Init(const Config& config) {
22+
pin_digital_out_ = config.pin_digital_out;
23+
pin_digital_in_ = config.pin_digital_in;
24+
pin_analog_out_ = config.pin_analog_out;
25+
pin_analog_in_ = config.pin_analog_in;
26+
27+
fbase_.reset(new Firebase(config.host, config.auth));
28+
stream_ = fbase_->streamPtr(config.path);
29+
}
30+
31+
void Transcriber::Loop() {
32+
// Read incoming data
33+
if (stream_->available()) {
34+
std::string event_str;
35+
if (stream_->read(event_str) == FirebaseStream::PUT) {
36+
FirebaseObject update(event_str.c_str());
37+
if (update.success()) {
38+
if (update.getString("path") == "/") {
39+
ProcessInitialUpdate(update);
40+
} else {
41+
ProcessIncrementalUpdate(update);
42+
}
43+
}
44+
}
45+
}
46+
47+
// Send values to cloud
48+
int digital_in = digitalRead(pin_digital_in_);
49+
if (digital_in != digital_in_) {
50+
Serial.println(String(digital_in).c_str());
51+
FirebaseSet set = fbase_->set(kSubPathDigitalIn, String(digital_in).c_str());
52+
digital_in_ = digital_in;
53+
}
54+
55+
float analog_in = analogRead(pin_analog_in_);
56+
if (analog_in != analog_in_) {
57+
Serial.println(String(analog_in).c_str());
58+
FirebaseSet set = fbase_->set(kSubPathAnalogIn, String(analog_in).c_str());
59+
analog_in_ = analog_in;
60+
}
61+
}
62+
63+
void Transcriber::ProcessInitialUpdate(const FirebaseObject& update) {
64+
int digital_out = update.getInt((std::string("data") + kSubPathDigitalOut).c_str());
65+
if (!update.failed()) {
66+
digitalWrite(pin_digital_out_, digital_out);
67+
}
68+
69+
float analog_out = update.getFloat((std::string("data") + kSubPathAnalogOut).c_str());
70+
if (!update.failed()) {
71+
analogWrite(pin_analog_out_, analog_out);
72+
}
73+
}
74+
75+
void Transcriber::ProcessIncrementalUpdate(const FirebaseObject& update) {
76+
if (update.getString("path").equals(kSubPathDigitalOut)) {
77+
int digital_out = update.getInt("data");
78+
if (!update.failed()) {
79+
digitalWrite(pin_digital_out_, update.getInt("data"));
80+
}
81+
}
82+
83+
if (update.getString("path").equals(kSubPathAnalogOut)) {
84+
float analog_out = update.getFloat("data");
85+
if (!update.failed()) {
86+
analogWrite(pin_analog_out_, analog_out);
87+
}
88+
}
89+
}
90+
91+
} // thing

src/thing/Transcriber.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// Copyright 2016 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
#ifndef transcriber_h
18+
#define transcriber_h
19+
20+
#include <Arduino.h>
21+
#include <memory>
22+
#include <utility>
23+
#include <vector>
24+
#include "Firebase.h"
25+
#include "FirebaseObject.h"
26+
#include "third-party/arduino-json-5.3/include/ArduinoJson.h"
27+
#include "thing/Config.h"
28+
29+
namespace thing {
30+
31+
class Transcriber {
32+
public:
33+
Transcriber(const Config& config);
34+
void UpdateConfig(const Config& config);
35+
void Loop();
36+
37+
private:
38+
void Init(const Config& config);
39+
40+
void ProcessInitialUpdate(const FirebaseObject& update);
41+
void ProcessIncrementalUpdate(const FirebaseObject& update);
42+
43+
std::unique_ptr<Firebase> fbase_;
44+
std::unique_ptr<FirebaseStream> stream_;
45+
46+
int digital_in_ = 0;
47+
float analog_in_ = 0.0f;
48+
49+
int pin_digital_out_;
50+
int pin_digital_in_;
51+
int pin_analog_out_;
52+
int pin_analog_in_;
53+
};
54+
55+
}; // thing
56+
57+
58+
#endif // transcriber_h

0 commit comments

Comments
 (0)