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.

239 lines
8.5 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #include "hardware.hpp"
  2. #include "adc.h"
  3. #include "tim.h"
  4. #include "zsdk/zcanreceiver/zcanreceiver.hpp"
  5. #define TAG "HARD"
  6. using namespace iflytop;
  7. /***********************************************************************************************************************
  8. * EXT *
  9. ***********************************************************************************************************************/
  10. static osThreadId H2O2CaptureThreadId;
  11. static osThreadId AlarmLightThreadId;
  12. /***********************************************************************************************************************
  13. * FUNC *
  14. ***********************************************************************************************************************/
  15. static void c_onH2O2CaptureThread(void const* argument) { Hardware::ins().onH2O2CaptureThread(); }
  16. static void c_onAlarmLightThread(void const* argument) { Hardware::ins().onAlarmLightThread(); }
  17. int32_t Hardware::readSwitchGroup() {
  18. int32_t id = 0;
  19. id |= m_switch_group0.read() << 0;
  20. id |= m_switch_group1.read() << 1;
  21. id |= m_switch_group2.read() << 2;
  22. id |= m_switch_group3.read() << 3;
  23. id |= m_switch_group4.read() << 4;
  24. }
  25. void Hardware::setAlarmLight(bool r, bool g, bool y) {
  26. m_alarmLightR.write(r);
  27. m_alarmLightG.write(g);
  28. m_alarmLightY.write(y);
  29. }
  30. void Hardware::setAlarmLight(light_state_t state) { m_alarmLightState = state; }
  31. void Hardware::init() {
  32. m_alarmLightR.initAsOutput(PD7, kxs_gpio_pullup, true, false);
  33. m_alarmLightG.initAsOutput(PD9, kxs_gpio_pullup, true, false);
  34. m_alarmLightY.initAsOutput(PD8, kxs_gpio_pullup, true, false);
  35. m_switch_group0.initAsInput(PE0, kxs_gpio_nopull, kxs_gpio_no_irq, false);
  36. m_switch_group1.initAsInput(PE1, kxs_gpio_nopull, kxs_gpio_no_irq, false);
  37. m_switch_group2.initAsInput(PE3, kxs_gpio_nopull, kxs_gpio_no_irq, false);
  38. m_switch_group3.initAsInput(PE6, kxs_gpio_nopull, kxs_gpio_no_irq, false);
  39. m_switch_group4.initAsInput(PE8, kxs_gpio_nopull, kxs_gpio_no_irq, false);
  40. id_from_machine.initAsInput(PE8, kxs_gpio_nopull, kxs_gpio_no_irq, false);
  41. ZLOGI(TAG, "switch group:%d id_from_machine %d", readSwitchGroup(), id_from_machine.read());
  42. setAlarmLight(kdisconnected);
  43. /**
  44. * @brief HMP110
  45. */
  46. osDelay(2000); // 等待传感器上电
  47. #ifdef H2O2_SENSOR_TYPE_HMP110
  48. ZASSERT(huart2.Init.BaudRate == 19200);
  49. ZASSERT(huart2.Init.StopBits == UART_STOPBITS_2);
  50. m_H2o2Sensor_ModbusBlockHost.initialize(&huart2);
  51. m_H2o2Sensor_H2O2Adc.initialize(&hadc1, ADC_CHANNEL_10); //
  52. m_H2o2Sensor_HMP110.init(&m_H2o2Sensor_ModbusBlockHost);
  53. if (m_H2o2Sensor_HMP110.ping(1)) {
  54. m_h2o2sensor_detectId = 1;
  55. }
  56. if (m_H2o2Sensor_HMP110.ping(240)) {
  57. m_h2o2sensor_detectId = 240;
  58. }
  59. m_H2o2Sensor_HMP110.setid(m_h2o2sensor_detectId);
  60. ZLOGI(TAG, "H2O2 HMP110 Sensor detect id: %d", m_h2o2sensor_detectId);
  61. #endif
  62. #ifdef H2O2_SENSOR_TYPE_HPP272
  63. ZASSERT(huart3.Init.BaudRate == 19200);
  64. ZASSERT(huart3.Init.StopBits == UART_STOPBITS_2);
  65. m_H2o2Sensor_ModbusBlockHost.initialize(&huart3);
  66. m_H2o2Sensor_HPP272.init(&m_H2o2Sensor_ModbusBlockHost);
  67. if (m_H2o2Sensor_HPP272.ping(1)) {
  68. m_h2o2sensor_detectId = 1;
  69. }
  70. if (m_H2o2Sensor_HPP272.ping(240)) {
  71. m_h2o2sensor_detectId = 240;
  72. }
  73. m_H2o2Sensor_HPP272.setid(m_h2o2sensor_detectId);
  74. ZLOGI(TAG, "H2O2 HPP272 Sensor detect id: %d", m_h2o2sensor_detectId);
  75. #endif
  76. osThreadDef(H2O2CaptureThread, c_onH2O2CaptureThread, osPriorityNormal, 0, 1024);
  77. H2O2CaptureThreadId = osThreadCreate(osThread(H2O2CaptureThread), NULL);
  78. osThreadDef(AlarmLightThread, c_onAlarmLightThread, osPriorityNormal, 0, 1024);
  79. AlarmLightThreadId = osThreadCreate(osThread(AlarmLightThread), NULL);
  80. }
  81. /***********************************************************************************************************************
  82. * H2O2 *
  83. ***********************************************************************************************************************/
  84. bool Hardware::h2o2_sensor_is_online() {
  85. #ifdef H2O2_SENSOR_TYPE_HMP110
  86. if (m_h2o2sensor_detectId <= 0) return false;
  87. int32_t ecode = m_H2o2Sensor_HMP110.read_cache_errorcode();
  88. if (ecode == -1) return false;
  89. #endif
  90. #ifdef H2O2_SENSOR_TYPE_HPP272
  91. if (m_h2o2sensor_detectId <= 0) return false;
  92. int32_t ecode = m_H2o2Sensor_HPP272.read_cache_errorcode();
  93. if (ecode == -1) return false;
  94. #endif
  95. return true;
  96. }
  97. int32_t Hardware::h2o2_sensor_read_calibration_date(int32_t* year, int32_t* month, int32_t* day) { //
  98. *year = 1;
  99. *month = 2;
  100. *day = 3;
  101. return 0;
  102. }
  103. int32_t Hardware::h2o2_sensor_read_sub_ic_errorcode() { //
  104. #ifdef H2O2_SENSOR_TYPE_HPP272
  105. return m_H2o2Sensor_HPP272.read_cache_errorcode();
  106. #endif
  107. #ifdef H2O2_SENSOR_TYPE_HMP110
  108. return m_H2o2Sensor_HMP110.read_cache_errorcode();
  109. #endif
  110. }
  111. int32_t Hardware::h2o2_sensor_read_sub_ic_reg(int32_t add, uint16_t* val, size_t len) { //
  112. #ifdef H2O2_SENSOR_TYPE_HPP272
  113. if (m_H2o2Sensor_HPP272.read_reg(add, val, len)) {
  114. return 0;
  115. }
  116. #endif
  117. #ifdef H2O2_SENSOR_TYPE_HMP110
  118. if (m_H2o2Sensor_HMP110.read_reg(add, val, len)) {
  119. return 0;
  120. }
  121. #endif
  122. return kerr_subdevice_offline;
  123. }
  124. int32_t Hardware::h2o2_sensor_data(report_h2o2_data_t* readdata) {
  125. #ifdef H2O2_SENSOR_TYPE_HMP110
  126. int32_t ecode = m_H2o2Sensor_HMP110.read_cache_errorcode();
  127. int32_t h2o2adcVal = m_H2o2Sensor_H2O2Adc.getCacheVal();
  128. HMP110::hmp110_sensordata_t sensordata;
  129. m_H2o2Sensor_HMP110.read_cache_sensor_data(&sensordata);
  130. // float mv = adcv / 4095.0 * 3.3 * 1000;
  131. // float ma = mv / 150.0;
  132. // float ppm = (ma - 4) / (20 - 4) * 2000;
  133. int32_t h2o2ma = (h2o2adcVal / 4095.0 * 3.3 * 1000) / 150.0;
  134. int32_t h2o2ppm = (h2o2ma - 4) / (20 - 4) * 2000;
  135. readdata->sensor_error = ecode != 0;
  136. readdata->h2o2 = h2o2ppm;
  137. readdata->humid = sensordata.rh;
  138. readdata->temp = sensordata.temp;
  139. readdata->saturation = 0;
  140. ZLOGI(TAG, "ecode: %d ppm:%d, rh:%d, temp:%d, df_ptemp:%d, ah:%d, mr:%d, wbt:%d, eh:%d", //
  141. ecode, //
  142. h2o2ppm, //
  143. sensordata.rh, //
  144. sensordata.temp, //
  145. sensordata.df_ptemp, //
  146. sensordata.ah, //
  147. sensordata.mr, //
  148. sensordata.wet_bulb_temp, //
  149. sensordata.enthalpy);
  150. return 0;
  151. #endif
  152. #ifdef H2O2_SENSOR_TYPE_HPP272
  153. int32_t ecode = m_H2o2Sensor_HPP272.read_cache_errorcode();
  154. HPP272::hpp272_data_t sensordata;
  155. m_H2o2Sensor_HPP272.read_cache_sensor_data(&sensordata);
  156. readdata->sensor_error = ecode != 0;
  157. readdata->h2o2 = sensordata.hydrogen_peroxide_volume / 10.0;
  158. readdata->humid = sensordata.relative_humidity / 10.0;
  159. readdata->temp = sensordata.temperature1 / 10.0;
  160. readdata->saturation = sensordata.h2o_h2o2_rs / 10.0;
  161. ZLOGI(TAG, "h2o2 %d, humid %d, temp %d, sat %d",
  162. sensordata.hydrogen_peroxide_volume / 10, //
  163. sensordata.relative_humidity / 10, //
  164. sensordata.temperature1 / 10, //
  165. sensordata.h2o_h2o2_rs / 10);
  166. return 0;
  167. #endif
  168. }
  169. void Hardware::onH2O2CaptureThread() {
  170. while (1) {
  171. osDelay(1000);
  172. #ifdef H2O2_SENSOR_TYPE_HMP110
  173. if (m_h2o2sensor_detectId > 0) {
  174. m_H2o2Sensor_H2O2Adc.updateAdcValToCache();
  175. m_H2o2Sensor_HMP110.updateSensorDataAndErrorcode();
  176. }
  177. #endif
  178. #ifdef H2O2_SENSOR_TYPE_HPP272
  179. if (m_h2o2sensor_detectId > 0) {
  180. m_H2o2Sensor_HPP272.updateSensorDataAndErrorcode();
  181. }
  182. #endif
  183. }
  184. }
  185. void Hardware::onAlarmLightThread() {
  186. while (1) {
  187. osDelay(500);
  188. if (m_alarmLightState == kdisconnected) {
  189. static bool state;
  190. setAlarmLight(false, state, false);
  191. state = !state;
  192. } else if (m_alarmLightState == kconnected) {
  193. setAlarmLight(true, true, false);
  194. } else if (m_alarmLightState == kerror) {
  195. setAlarmLight(true, false, false);
  196. }
  197. }
  198. }