Skip to content
This repository has been archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
Fix blinky (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartmscott committed Jan 15, 2023
1 parent 9508a04 commit 16e94a2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 44 deletions.
42 changes: 16 additions & 26 deletions samples/rpi/blinky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ int main(int argc, char **argv) {
Address spawner(argv[2]);
Machine m(name, socket, address, spawner);

const auto reader = "rpi/GPIOReader";
const auto writer = "rpi/GPIOWriter";
const auto buttonReader = "rpi/GPIOReader:button";
const auto led1Writer = "rpi/GPIOWriter:led1";
const auto led2Writer = "rpi/GPIOWriter:led2";

const auto buttonPin = 10;
const auto led1Pin = 11;
const auto led2Pin = 12;
const auto buttonPin = 17;
const auto led1Pin = 18;
const auto led2Pin = 23;

auto led1on = false;

std::map<const std::string, const Address &> addresses;
std::map<const std::string, const Address > addresses;

m.AddState(std::make_unique<State>(
// State Name
Expand All @@ -37,8 +38,9 @@ int main(int argc, char **argv) {
"",
// On Entry Action
[&]() {
m.Spawn(reader);
m.Spawn(writer);
m.Spawn(buttonReader, std::vector<std::string>{std::to_string(buttonPin), std::to_string(PUD_UP)});
m.Spawn(led1Writer, std::vector<std::string>{std::to_string(led1Pin)});
m.Spawn(led2Writer, std::vector<std::string>{std::to_string(led2Pin)});
m.GotoState("loop");
},
// On Exit Action
Expand Down Expand Up @@ -88,23 +90,14 @@ int main(int argc, char **argv) {
"main",
// On Entry Action
[&]() {
for (const auto &a : addresses) {
info() << "Address: " << a->first << '@' << a->second <<'\n' << std::flush;
}
// Request Button state
if (const auto &a = addresses.find(reader); a != addresses.end()) {
std::ostringstream oss;
oss << "read";
oss << buttonPin;
m.Send(a->second, oss.str());
if (const auto &a = addresses.find(buttonReader); a != addresses.end()) {
m.Send(a->second, "read");
}
// Periodically toggle LED1
led1on = !led1on;
if (const auto &a = addresses.find(writer); a != addresses.end()) {
std::ostringstream oss;
oss << (led1on ? "high " : "low ");
oss << led1Pin;
m.Send(a->second, oss.str());
if (const auto &a = addresses.find(led1Writer); a != addresses.end()) {
m.Send(a->second, (led1on ? "high" : "low"));
}
// Schedule message to be sent to self after 1s
m.SendAfter(address, "loop", std::chrono::seconds(1));
Expand All @@ -117,15 +110,12 @@ int main(int argc, char **argv) {
[&](const Address &sender, std::istream &args) {
int p;
args >> p;
if (const auto &a = addresses.find(writer);
if (const auto &a = addresses.find(led2Writer);
a != addresses.end() && p == buttonPin) {
// Switch LED2 based on Button state
int state;
args >> state;
std::ostringstream oss;
oss << (state ? "high " : "low ");
oss << led2Pin;
m.Send(a->second, oss.str());
m.Send(a->second, (state ? "high" : "low"));
}
}},
{"loop", [&](const Address &sender,
Expand Down
20 changes: 13 additions & 7 deletions samples/rpi/gpioreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include <Wink/state.h>

int main(int argc, char **argv) {
if (argc < 3) {
error() << "Incorrect parameters, expected <spawner> <address>\n"
if (argc < 4) {
error() << "Incorrect parameters, expected <spawner> <address> <pin> <pud>\n"
<< std::flush;
return -1;
}
Expand All @@ -21,23 +21,29 @@ int main(int argc, char **argv) {
Address spawner(argv[2]);
Machine m(name, socket, address, spawner);

wiringPiSetupGpio();

int pin = std::stoi(argv[3]);
pinMode(pin, INPUT);
int pud = PUD_OFF;
if (argc > 4) {
pud = std::stoi(argv[4]);
}
pullUpDnControl(pin, pud);

m.AddState(std::make_unique<State>(
// State Name
"main",
// Parent State
"",
// On Entry Action
[]() { wiringPiSetupGpio(); },
[]() {},
// On Exit Action
[]() {},
// Receivers
std::map<const std::string, Receiver>{
{"read",
[&](const Address &sender, std::istream &args) {
int pin;
args >> pin;
pinMode(pin, INPUT);

std::ostringstream oss;
oss << "gpio ";
oss << pin;
Expand Down
17 changes: 8 additions & 9 deletions samples/rpi/gpiowriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include <Wink/state.h>

int main(int argc, char **argv) {
if (argc < 3) {
error() << "Incorrect parameters, expected <spawner> <address>\n"
if (argc < 4) {
error() << "Incorrect parameters, expected <spawner> <address> <pin>\n"
<< std::flush;
return -1;
}
Expand All @@ -21,30 +21,29 @@ int main(int argc, char **argv) {
Address spawner(argv[2]);
Machine m(name, socket, address, spawner);

wiringPiSetupGpio();

int pin = std::stoi(argv[3]);
pinMode(pin, INPUT);

m.AddState(std::make_unique<State>(
// State Name
"main",
// Parent State
"",
// On Entry Action
[]() { wiringPiSetupGpio(); },
[]() {},
// On Exit Action
[]() {},
// Receivers
std::map<const std::string, Receiver>{
{"high",
[&](const Address &sender, std::istream &args) {
int pin;
args >> pin;
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
info() << "GPIO " << pin << " is HIGH\n" << std::flush;
}},
{"low",
[&](const Address &sender, std::istream &args) {
int pin;
args >> pin;
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
info() << "GPIO " << pin << " is LOW\n" << std::flush;
}},
Expand Down
5 changes: 3 additions & 2 deletions src/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,9 @@ void Machine::handleMessage(
std::string name;
iss >> name;
spawned.emplace(key, std::make_pair(name, now));
// Seek back to start of name to make it available to the message handler.
iss.seekg(-name.length(), std::ios_base::cur);
// Reset iss to start of name to make it available to the message handler.
iss = std::istringstream(buffer);
iss >> m;
} else if (m == "exited") {
spawned.erase(key);
} else if (m == "pulsed") {
Expand Down

0 comments on commit 16e94a2

Please # to comment.