From 42077af0734b747c5e2311b3b7186537b72cecdb Mon Sep 17 00:00:00 2001 From: mb Date: Sat, 18 Apr 2009 17:06:54 +0200 Subject: Turn off the device, if the control tool is shutdown Signed-off-by: mb --- pressure_control/firmware/main.c | 38 ++++++++++++++++++++++++++----------- pressure_control/firmware/main.h | 4 ++++ pressure_control/firmware/remote.c | 7 +++++++ pressure_control/firmware/remote.h | 2 ++ pressure_control/firmware/valves.c | 8 ++++++++ pressure_control/firmware/valves.h | 1 + pressure_control/remote/pctl-remote | 24 +++++++++++++++++++++-- 7 files changed, 71 insertions(+), 13 deletions(-) diff --git a/pressure_control/firmware/main.c b/pressure_control/firmware/main.c index d2d849d..a82a503 100644 --- a/pressure_control/firmware/main.c +++ b/pressure_control/firmware/main.c @@ -179,6 +179,16 @@ static void print_banner(void) print_sram(str); } +void prepare_turn_on(void) +{ + state.device_enabled = 1; +} + +void prepare_shutdown(void) +{ + state.device_enabled = 0; +} + int main(void) { uint8_t mcucsr; @@ -217,18 +227,24 @@ int main(void) remote_notify_restart(); while (1) { mb(); - if (state.sensor_trigger_cnt == 0) { - /* It's time for triggering another sensor measurement. */ - state.sensor_trigger_cnt = -1; - mb(); - sensor_trigger_read(); - } - if (state.needs_checking) { - check_pressure(); - /* Trigger another measurement in 25 milliseconds. */ - state.sensor_trigger_cnt = 25; + if (state.device_enabled) { + if (state.sensor_trigger_cnt == 0) { + /* It's time for triggering another + * sensor measurement. */ + state.sensor_trigger_cnt = -1; + mb(); + sensor_trigger_read(); + } + if (state.needs_checking) { + check_pressure(); + /* Trigger another measurement in + * a few milliseconds. */ + state.sensor_trigger_cnt = 35; + state.needs_checking = 0; + mb(); + } + } else { state.needs_checking = 0; - mb(); } remote_work(); wdt_reset(); diff --git a/pressure_control/firmware/main.h b/pressure_control/firmware/main.h index cf6978a..edb8694 100644 --- a/pressure_control/firmware/main.h +++ b/pressure_control/firmware/main.h @@ -16,6 +16,8 @@ struct pressure_config { }; struct pressure_state { + /* Sensing and adjustment logic enabled? */ + bool device_enabled; /* Current pressure in the tank (in mBar) */ uint16_t mbar; /* Reported pressure via RS232 */ @@ -33,5 +35,7 @@ struct pressure_state { void get_pressure_config(struct pressure_config *cfg); void set_pressure_config(struct pressure_config *cfg); void get_pressure_state(struct pressure_state *state); +void prepare_turn_on(void); +void prepare_shutdown(void); #endif /* MAIN_H_ */ diff --git a/pressure_control/firmware/remote.c b/pressure_control/firmware/remote.c index d51a58b..f41c9ed 100644 --- a/pressure_control/firmware/remote.c +++ b/pressure_control/firmware/remote.c @@ -211,6 +211,13 @@ static void handle_received_message(void) err = MSG_ERR_INVAL; break; } + case MSG_SHUTDOWN: + prepare_shutdown(); + valves_shutdown(); + break; + case MSG_TURNON: + prepare_turn_on(); + break; case MSG_INVALID: break; default: diff --git a/pressure_control/firmware/remote.h b/pressure_control/firmware/remote.h index ed739eb..85afd92 100644 --- a/pressure_control/firmware/remote.h +++ b/pressure_control/firmware/remote.h @@ -25,6 +25,8 @@ enum remote_message_id { MSG_SET_CONFIG_FLAGS, MSG_SET_VALVE, MSG_RESTARTED, + MSG_SHUTDOWN, + MSG_TURNON, }; enum remote_message_error { diff --git a/pressure_control/firmware/valves.c b/pressure_control/firmware/valves.c index 034dc99..75d4606 100644 --- a/pressure_control/firmware/valves.c +++ b/pressure_control/firmware/valves.c @@ -91,6 +91,14 @@ static inline void valves_ddr_setup(void) (1 << VALVE1_12) | (1 << VALVE1_14); } +void valves_shutdown(void) +{ + valves_global_switch(VALVES_FLOW_OUT); + valve_wait_toggle(); + valve0_switch(VALVE_STATE_IDLE); + valve1_switch(VALVE_STATE_IDLE); +} + void valves_emergency_state(void) { valves_init(); diff --git a/pressure_control/firmware/valves.h b/pressure_control/firmware/valves.h index aa71268..0929682 100644 --- a/pressure_control/firmware/valves.h +++ b/pressure_control/firmware/valves.h @@ -28,6 +28,7 @@ static inline void valve_wait_toggle(void) void valves_init(void); void valves_emergency_state(void); +void valves_shutdown(void); void valves_global_switch(uint8_t global_state); uint8_t valves_get_global_state(void); diff --git a/pressure_control/remote/pctl-remote b/pressure_control/remote/pctl-remote index ffe74fc..489a9df 100755 --- a/pressure_control/remote/pctl-remote +++ b/pressure_control/remote/pctl-remote @@ -1,6 +1,6 @@ #!/usr/bin/env python """ -# Copyright (C) 2008 Michael Buesch +# Copyright (C) 2008-2009 Michael Buesch # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License version 3 @@ -56,6 +56,8 @@ MSG_CONFIG_FLAGS = 14 MSG_SET_CONFIG_FLAGS = 15 MSG_SET_VALVE = 16 MSG_RESTARTED = 17 +MSG_SHUTDOWN = 18 +MSG_TURNON = 19 # Message error codes MSG_ERR_NONE = 0 # No error MSG_ERR_CHKSUM = 1 # Checksum error @@ -371,8 +373,21 @@ class MainWidget(QWidget): self.log.hostLog( "Failed to fetch active configuration "+\ "from device.") + error = remote.sendMessageSyncError(MSG_TURNON, 0, "") + if error: + self.log.hostLog("Failed to turn on device") + else: + self.log.hostLog("Device turned on") self.initialized = True + def shutdown(self): + if self.initialized: + error = remote.sendMessageSyncError(MSG_SHUTDOWN, 0, "") + if error != MSG_ERR_NONE: + QMessageBox.critical(self, + "Pressure Control", + "Failed to shutdown device") + def fetchState(self): # Get the current pressure reply = remote.sendMessageSyncReply(MSG_GET_CURRENT_PRESSURE, 0, "", @@ -494,6 +509,9 @@ class MainWindow(QMainWindow): def initializeState(self): self.centralWidget().initializeState() + def shutdown(self): + self.centralWidget().shutdown() + def about(self): QMessageBox.information(self, self.tr("About"), self.tr("Pneumatic pressure control\n" @@ -514,7 +532,9 @@ def main(): mainwnd.initializeState() mainwnd.show() - exit(app.exec_()) + result = app.exec_() + mainwnd.shutdown() + exit(result) if __name__ == "__main__": try: -- cgit v1.2.3