9 changed files with 297 additions and 1 deletions
@ -0,0 +1,23 @@ |
|||||
|
/*
|
||||
|
* pump_control.h |
||||
|
* |
||||
|
* Created on: Jul 27, 2021 |
||||
|
* Author: Andreas Berthoud |
||||
|
*/ |
||||
|
|
||||
|
#ifndef INC_PUMP_CONTROL_H_ |
||||
|
#define INC_PUMP_CONTROL_H_ |
||||
|
|
||||
|
#include <stdint.h> |
||||
|
|
||||
|
#ifdef __cplusplus |
||||
|
extern "C" { |
||||
|
#endif |
||||
|
|
||||
|
uint8_t pump_init(); |
||||
|
|
||||
|
#ifdef __cplusplus |
||||
|
} |
||||
|
#endif |
||||
|
|
||||
|
#endif /* INC_PUMP_CONTROL_H_ */ |
||||
@ -0,0 +1,21 @@ |
|||||
|
/*
|
||||
|
* pump_control.hpp |
||||
|
* |
||||
|
* Created on: Jul 27, 2021 |
||||
|
* Author: Andreas Berthoud |
||||
|
*/ |
||||
|
|
||||
|
#ifndef INC_PUMP_CONTROL_H_ |
||||
|
#define INC_PUMP_CONTROL_H_ |
||||
|
|
||||
|
#include <stdint.h> |
||||
|
|
||||
|
namespace pump { |
||||
|
|
||||
|
void turn_on(uint16_t timeout); |
||||
|
uint8_t is_on(); |
||||
|
void turn_off(); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
#endif /* INC_PUMP_CONTROL_H_ */ |
||||
@ -0,0 +1,65 @@ |
|||||
|
/*
|
||||
|
* pump_control.cpp |
||||
|
* |
||||
|
* Created on: Jul 27, 2021 |
||||
|
* Author: Andreas Berthoud |
||||
|
*/ |
||||
|
|
||||
|
#include "pump_control.h" |
||||
|
#include "pump_control.hpp" |
||||
|
#include "main.h" |
||||
|
#include "stm32wbxx_hal.h" |
||||
|
#include "hw_if.h" |
||||
|
#include "LogCommand.hpp" |
||||
|
|
||||
|
|
||||
|
namespace pump { |
||||
|
|
||||
|
typedef struct { |
||||
|
bool is_on; |
||||
|
uint8_t timer_id; |
||||
|
} pump_state_t; |
||||
|
|
||||
|
pump_state_t pump_state { |
||||
|
.is_on=false |
||||
|
}; |
||||
|
|
||||
|
void turn_on(uint16_t timeout) { |
||||
|
|
||||
|
uint32_t ticks = (timeout*1000*1000/CFG_TS_TICK_VAL); |
||||
|
if (ticks > 0xFFFF0000) { |
||||
|
log_error("pump::turn_on", "max timout exceeded", 0); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
HW_TS_Start(pump_state.timer_id, ticks); |
||||
|
HAL_GPIO_WritePin(RELAY_12V_OUT1_GPIO_Port, RELAY_12V_OUT1_Pin, GPIO_PIN_SET); |
||||
|
pump_state.is_on = true; |
||||
|
log_debug("pump", "turned on", 0); |
||||
|
} |
||||
|
|
||||
|
uint8_t is_on() { |
||||
|
return (uint8_t)pump_state.is_on; |
||||
|
} |
||||
|
|
||||
|
void turn_off() { |
||||
|
pump_state.is_on = false; |
||||
|
HAL_GPIO_WritePin(RELAY_12V_OUT1_GPIO_Port, RELAY_12V_OUT1_Pin, GPIO_PIN_RESET); |
||||
|
HW_TS_Stop(pump_state.timer_id); |
||||
|
log_debug("pump", "turned off", 0); |
||||
|
} |
||||
|
|
||||
|
uint8_t init() { |
||||
|
HW_TS_ReturnStatus_t return_status = HW_TS_Create(CFG_TIM_PROC_ID_ISR, &(pump_state.timer_id), hw_ts_SingleShot, turn_off); |
||||
|
if (return_status == hw_ts_Failed) { |
||||
|
log_error("pump::init", "Creating hardware timer failed", 0); |
||||
|
return 1; |
||||
|
} |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
uint8_t pump_init() { |
||||
|
return pump::init(); |
||||
|
} |
||||
@ -0,0 +1,59 @@ |
|||||
|
/*
|
||||
|
* PumpCommand.cpp |
||||
|
* |
||||
|
* Created on: Jul 27, 2021 |
||||
|
* Author: Andreas Berthoud |
||||
|
*/ |
||||
|
|
||||
|
#include "PumpCommand.hpp" |
||||
|
#include "pump_control.hpp" |
||||
|
#include "main.h" |
||||
|
|
||||
|
|
||||
|
PumpResponse::PumpResponse(com_channel_type_t com_channel_type, uint16_t response_identifier, bool was_successful, bool is_on) |
||||
|
: Response(com_channel_type, response_identifier) { |
||||
|
uint8_t * next_free_payload = this->payload_ptr + this->get_payload_size(); |
||||
|
next_free_payload[0] = (uint8_t)was_successful; |
||||
|
next_free_payload[1] = (uint8_t)is_on; |
||||
|
this->add_to_payload_size(2); |
||||
|
} |
||||
|
|
||||
|
PumpRequest::PumpRequest(com_channel_type_t com_channel_type, uint8_t * payload_ptr, uint16_t size) |
||||
|
: Request(com_channel_type, payload_ptr, size) { |
||||
|
uint16_t expected_length = this->buffer_offset + 3; |
||||
|
if (expected_length != size) { |
||||
|
this->has_error = true; |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
uint8_t * data_ptr = payload_ptr + this->buffer_offset; |
||||
|
this->pump_command = static_cast<PumpCommand>(data_ptr[0]); |
||||
|
this->timeout = data_ptr[1] << 8 | data_ptr[2]; |
||||
|
} |
||||
|
|
||||
|
PumpResponse * PumpRequest::execute_request(com_channel_type_t com_channel_type, uint16_t response_identifier) { |
||||
|
bool was_successful = true; |
||||
|
|
||||
|
|
||||
|
if (this->pump_command >= pump_command_max) { |
||||
|
was_successful = false; |
||||
|
} else { |
||||
|
switch (this->pump_command) |
||||
|
{ |
||||
|
case pump_command_turn_off: |
||||
|
pump::turn_on(this->timeout); |
||||
|
break; |
||||
|
case pump_command_turn_on: |
||||
|
pump::turn_off(); |
||||
|
break; |
||||
|
case pump_command_get_state: |
||||
|
break; |
||||
|
default: |
||||
|
was_successful = false; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
bool is_on = pump::is_on(); |
||||
|
|
||||
|
return new PumpResponse(com_channel_type, response_identifier, was_successful, is_on); |
||||
|
} |
||||
@ -0,0 +1,48 @@ |
|||||
|
/*
|
||||
|
* PumpCommand.hpp |
||||
|
* |
||||
|
* Created on: Jul 27, 2021 |
||||
|
* Author: Andreas Berthoud |
||||
|
*/ |
||||
|
|
||||
|
#ifndef PUMPCOMMAND_HPP_ |
||||
|
#define PUMPCOMMAND_HPP_ |
||||
|
|
||||
|
#include "Request.hpp" |
||||
|
#include "Response.hpp" |
||||
|
|
||||
|
|
||||
|
class PumpResponse : public Response { |
||||
|
public: |
||||
|
PumpResponse(com_channel_type_t com_channel_type, uint16_t response_identifier, bool was_successful, bool is_on); |
||||
|
|
||||
|
CommandId get_command_id() override { return COMMAND_PUMP_RESPONSE; } |
||||
|
|
||||
|
private: |
||||
|
bool was_successful = false; |
||||
|
bool is_on = false; |
||||
|
}; |
||||
|
|
||||
|
class PumpRequest : public Request { |
||||
|
public: |
||||
|
PumpRequest(com_channel_type_t com_channel_type, uint8_t * payload_ptr, uint16_t size); |
||||
|
|
||||
|
PumpResponse * execute_request(com_channel_type_t com_channel_type, uint16_t response_identifier) override; |
||||
|
|
||||
|
CommandId get_command_id() override { return COMMAND_PUMP_REQUEST; } |
||||
|
|
||||
|
private: |
||||
|
enum PumpCommand { |
||||
|
pump_command_turn_on, |
||||
|
pump_command_turn_off, |
||||
|
pump_command_get_state, |
||||
|
pump_command_max, |
||||
|
}; |
||||
|
|
||||
|
bool has_error = false; |
||||
|
PumpCommand pump_command = pump_command_turn_off; |
||||
|
uint16_t timeout; /* timeout in seconds until the pump is turned off again */ |
||||
|
|
||||
|
}; |
||||
|
|
||||
|
#endif /* PUMPCOMMAND_HPP_ */ |
||||
Loading…
Reference in new issue