You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.7 KiB

12 months ago
  1. #pragma once
  2. #include "base/appdep.hpp"
  3. namespace iflytop {
  4. using namespace transmit_disfection_protocol;
  5. class HeaterController {
  6. ZGPIO m_ctrlGpio;
  7. ZADC m_iAdc;
  8. ZADC m_tempAdc;
  9. bool m_isInitialized = false;
  10. public:
  11. void initialize(Pin_t ctrlGpio, ADC_HandleTypeDef* iadc, uint32_t ich, ADC_HandleTypeDef* tadc, uint32_t tch) {
  12. m_ctrlGpio.initAsOutput(ctrlGpio, kxs_gpio_nopull, true, false);
  13. m_iAdc.initialize("heater-idac", iadc, ich);
  14. m_tempAdc.initialize("heater-tadc", iadc, tch);
  15. m_isInitialized = true;
  16. AppPeriodTaskMgr::ins()->regTask("Heater-ADC", [this]() { periodTask(); }, 1000);
  17. BIND_FN(HeaterController, this, fn_heater_ctrl);
  18. BIND_FN(HeaterController, this, fn_heater_ctrl_safe_valve);
  19. BIND_FN(HeaterController, this, fn_heater_read_ei);
  20. BIND_FN(HeaterController, this, fn_heater_read_temperature_data);
  21. BIND_FN(HeaterController, this, fn_heater_read_ei_adc_raw);
  22. BIND_FN(HeaterController, this, fn_heater_read_temperature_data_adc_raw);
  23. BIND_FN(HeaterController, this, fn_heater_is_open);
  24. }
  25. bool isInitialized() { return m_isInitialized; }
  26. void heater_ctrl(int32_t val) { m_ctrlGpio.write(val); }
  27. void heater_ctrl_safe_valve(int32_t val) {}
  28. int32_t heater_read_temperature_data() { return heaterAdc2Temp(m_tempAdc.getCacheVal()); }
  29. int32_t heater_read_ei() { return hearterAdcToCurrent(m_iAdc.getCacheVal()); }
  30. int32_t heater_read_iadc() { return m_iAdc.getCacheVal(); }
  31. int32_t heater_read_tadc() { return m_tempAdc.getCacheVal(); }
  32. // PP
  33. void fn_heater_ctrl(ProcessContext* cxt) { //
  34. heater_ctrl(GET_PARAM(0));
  35. zcanbus_send_ack(cxt->packet, NULL, 0);
  36. }
  37. void fn_heater_ctrl_safe_valve(ProcessContext* cxt) { //
  38. heater_ctrl_safe_valve(GET_PARAM(0));
  39. zcanbus_send_ack(cxt->packet, NULL, 0);
  40. }
  41. void fn_heater_read_ei(ProcessContext* cxt) { //
  42. auto val = heater_read_ei();
  43. zcanbus_send_ack(cxt->packet, (uint8_t*)&val, sizeof(val));
  44. }
  45. void fn_heater_read_temperature_data(ProcessContext* cxt) { //
  46. auto val = heater_read_temperature_data();
  47. zcanbus_send_ack(cxt->packet, (uint8_t*)&val, sizeof(val));
  48. }
  49. void fn_heater_read_ei_adc_raw(ProcessContext* cxt) { //
  50. auto val = m_iAdc.getCacheVal();
  51. zcanbus_send_ack(cxt->packet, (uint8_t*)&val, sizeof(val));
  52. }
  53. void fn_heater_read_temperature_data_adc_raw(ProcessContext* cxt) { //
  54. auto val = m_tempAdc.getCacheVal();
  55. zcanbus_send_ack(cxt->packet, (uint8_t*)&val, sizeof(val));
  56. }
  57. void fn_heater_is_open(ProcessContext* cxt) { //
  58. zcanbus_send_ack(cxt->packet, m_ctrlGpio.read());
  59. }
  60. private:
  61. void periodTask() {
  62. m_iAdc.updateAdcValToCache();
  63. m_tempAdc.updateAdcValToCache();
  64. }
  65. };
  66. } // namespace iflytop