Skip to content

Commit

Permalink
Fix button change text
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi-EEE committed Jan 6, 2024
1 parent 58da5eb commit 40016e4
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 33 deletions.
46 changes: 34 additions & 12 deletions app/raspberry_pi/src/car/display/CarConsole.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ using namespace car::system;
using namespace ftxui;

namespace car::display {
static const ButtonOption button_style = ButtonOption::Animated();
static const ButtonOption animated_button_style = ButtonOption::Animated();
class CarConsole
{
public:
Component MainComponent(std::function<void()> start_car_system, std::function<void()> show_exit_modal) {
inline Component MainComponent(Component main_button, std::function<void()> show_exit_modal) {
auto component = Container::Vertical({
Button("Start Car Application", start_car_system, button_style),
Button("Quit", show_exit_modal, button_style),
main_button,
Button("Quit", show_exit_modal, animated_button_style),
});
// Polish how the two buttons are rendered:
component |= Renderer([&](Element inner) {
Expand All @@ -40,11 +40,11 @@ namespace car::display {
return component;
}

Component ExitModalComponent(std::function<void()> hide_exit_modal,
inline Component ExitModalComponent(std::function<void()> hide_exit_modal,
std::function<void()> exit) {
auto component = Container::Vertical({
Button("No", hide_exit_modal, button_style),
Button("Yes", exit, button_style),
Button("No", hide_exit_modal, animated_button_style),
Button("Yes", exit, animated_button_style),
});
// Polish how the two buttons are rendered:
component |= Renderer([&](Element inner) {
Expand All @@ -70,26 +70,48 @@ namespace car::display {

auto show_exit_modal = [&] { exit_modal_shown = true; };
auto hide_exit_modal = [&] { exit_modal_shown = false; };

auto start_car_system = [&] { this->car_system->start(); };


bool debounce = false;
bool button_pressed = false;
std::string main_button_text = "Start Car Application";

auto main_button_lambda = [&] {
if (debounce) return;
debounce = true;
button_pressed = !button_pressed;
std::cout << "Button pressed: " << button_pressed << std::endl;
if (button_pressed) {
main_button_text = "Connecting...";
this->car_system->start();
main_button_text = "Stop Car Application";
}
else {
main_button_text = "Disconnecting...";
this->car_system->stop();
main_button_text = "Start Car Application";
}
debounce = false;
};
auto main_button = Button(&main_button_text, main_button_lambda, animated_button_style);

auto exit = screen.ExitLoopClosure();

auto main_component = MainComponent(start_car_system, show_exit_modal);
auto main_component = MainComponent(main_button, show_exit_modal);
auto modal_component = ExitModalComponent(hide_exit_modal, exit);
main_component |= Modal(modal_component, &exit_modal_shown);

Loop loop(&screen, main_component);

this->car_system->initialize();

// The main loop:
while (!loop.HasQuitted()) {
this->car_system->update();
loop.RunOnce();
}

// Called after the loop ended.
this->car_system->terminate();
this->car_system->stop();
};

private:
Expand Down
30 changes: 16 additions & 14 deletions app/raspberry_pi/src/car/system/CarSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace car::system

CarSystem::~CarSystem()
{
this->terminate();
this->stop();
}

void CarSystem::initialize()
Expand All @@ -29,9 +29,21 @@ namespace car::system

void CarSystem::start()
{
this->messaging_system->start();
this->lidar_device->start();
this->running = true;
if (!this->running) {
this->messaging_system->start();
this->lidar_device->start();
this->running = true;
}
}

void CarSystem::stop()
{
if (!this->running)
{
this->messaging_system->stop();
this->lidar_device->stop();
this->running = false;
}
}

void CarSystem::update()
Expand All @@ -54,16 +66,6 @@ namespace car::system
this->messaging_system->sendMessage(output_json.dump());
}

void CarSystem::terminate()
{
if (!this->terminated)
{
this->terminated = true;
this->lidar_device->terminate();
this->messaging_system->terminate();
}
}

void CarSystem::move(const MoveCommand &move_command)
{
this->movement_system->move(move_command);
Expand Down
3 changes: 1 addition & 2 deletions app/raspberry_pi/src/car/system/CarSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ namespace car::system {

void initialize();
void start();
void stop();

void update();
void terminate();

void move(const MoveCommand& move_command);
void turn(const TurnCommand& turn_command);
Expand All @@ -39,7 +39,6 @@ namespace car::system {
std::unique_ptr<MovementSystem> movement_system;

bool running = false;
bool terminated = false;
};
}

Expand Down
2 changes: 1 addition & 1 deletion app/raspberry_pi/src/car/system/lidar/LidarDevice.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace car::system::lidar {
virtual void initialize() const = 0;
virtual void start() const = 0;
virtual std::vector<Measure> scan() const = 0;
virtual void terminate() const = 0;
virtual void stop() const = 0;

private:

Expand Down
2 changes: 1 addition & 1 deletion app/raspberry_pi/src/car/system/lidar/LidarDummy.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace car::system::lidar {
return measures;
};

void terminate() const override {};
void stop() const override {};

private:

Expand Down
2 changes: 1 addition & 1 deletion app/raspberry_pi/src/car/system/lidar/LidarScanner.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace car::system::lidar
return scanGenerator();
};

void terminate() const override
void stop() const override
{
this->lidar->stop();
this->lidar->stop_motor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ namespace car::system::messaging {
}
}

void terminate() {
void stop() {
this->websocket.stop();
ix::uninitNetSystem();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace car::system::movement {
{
}

void terminate() {
void stop() {
}

void move(const MoveCommand& move_command) {
Expand Down

0 comments on commit 40016e4

Please sign in to comment.