deviceUlt.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #pragma once
  2. #include <iostream>
  3. #include "deviceBase.h"
  4. using namespace std;
  5. // 超声波驱鸟炮
  6. #define EXPEL_WRITE (0x15)
  7. #define EXPEL_READ (0x05)
  8. #define EXPEL_POWER_OFF (0)
  9. #define EXPEL_POWER_ON (1<<7)
  10. #define EXPEL_PLAY_DISPEL (1<<6)
  11. #define EXPEL_TURN_OFF_DISPEL (0)
  12. #define EXPEL_NOT_FIRE (0)
  13. #define EXPEL_SINGLE_FIRE (1<<4)
  14. #define EXPEL_BURST_FIRE (2<<4)
  15. #define EXPEL_TURN_LEFT_CANCEL (0)
  16. #define EXPEL_TURN_LEFT_ENABLE (1<<3)
  17. #define EXPEL_TURN_RIGHT_CANCEL (0)
  18. #define EXPEL_TURN_RIGHT_ENABLE (1<<2)
  19. #define EXPEL_TURN_UP_CANCEL (0)
  20. #define EXPEL_TURN_UP_ENABLE (1<<1)
  21. #define EXPEL_TURN_DOWN_CANCEL (0)
  22. #define EXPEL_TURN_DOWM_ENABLE (1)
  23. #define EXPEL_STAUS_ACK 0x05
  24. typedef struct{
  25. int deviceId;
  26. uint8_t cmd;
  27. }UltControl;
  28. class deviceUlt:public deviceBase
  29. {
  30. private:
  31. uint8_t pressure;
  32. uint8_t commamd;
  33. int16_t current;
  34. uint16_t voltage;
  35. enum RECV_STEP{
  36. STEP_HEAD = 0,
  37. STEP_ADDR = 1,
  38. STEP_LEN = 2,
  39. STEP_DIR = 3,
  40. STEP_DATA = 4
  41. };
  42. typedef struct {
  43. uint8_t dataCount;
  44. uint8_t dataLength;
  45. uint8_t dataCheckSum;
  46. uint8_t data[11];
  47. enum RECV_STEP step = STEP_HEAD;
  48. }ExpelComm;
  49. ExpelComm _comm;
  50. public:
  51. deviceUlt():deviceBase()
  52. {
  53. this->setDeviceToken(serial);
  54. this->setDeviceInfo("北京首都机场 民航大板协议");
  55. this->setBaudRate(115200);
  56. }
  57. ~deviceUlt(){}
  58. void serialization(){}
  59. bool deserialization(){
  60. return true;
  61. }
  62. void setPressure(uint8_t Pressure){
  63. pressure = Pressure;
  64. }
  65. void setCommamd(uint8_t Commamd){
  66. commamd = Commamd;
  67. }
  68. void setCurrent(int16_t Current){
  69. current = Current;
  70. }
  71. void setVoltage(uint16_t Voltage){
  72. voltage = Voltage;
  73. }
  74. uint8_t getPressure(){
  75. return pressure;
  76. }
  77. uint8_t getCommamd(){
  78. return commamd;
  79. }
  80. int16_t getCurrent(){
  81. return current;
  82. }
  83. uint16_t getVoltage(){
  84. return voltage;
  85. }
  86. static void serialization(UltControl ultControl, char *data, bool status = false){
  87. data[0] = 0xaa;
  88. data[1] = (uint8_t)ultControl.deviceId;
  89. data[2] = 0x06;
  90. if(status) data[3] = 0x05;
  91. else data[3] = 0x15;
  92. data[4] = ultControl.cmd;
  93. data[5] = 0;
  94. for (int i = 0; i < 5; i++) {
  95. data[5] += data[i];
  96. }
  97. }
  98. bool deserialization(const uint8_t *data, int len, int &msg_id){
  99. if(data[0] != 0xbb){
  100. return false;
  101. }
  102. uint8_t dataCheckSum = 0;
  103. for(int i = 0; i < len - 1; i++){
  104. dataCheckSum += data[i];
  105. }
  106. if(dataCheckSum != data[len - 1]){
  107. return false;
  108. }
  109. msg_id = data[3];
  110. if(msg_id == EXPEL_STAUS_ACK){
  111. this->setDeviceId(to_string(data[1]));
  112. this->pressure = (data[9]&0x80) >> 7;
  113. this->commamd = (data[4]);
  114. this->current = (int16_t)(data[8]<<8 || data[7]);
  115. this->voltage = uint16_t(data[6]<<8 || data[5]);
  116. }
  117. return true;
  118. }
  119. bool Consumer(uint8_t *data, int len, int &msg_id){
  120. bool ReceiveSuccessful = false;
  121. if(this->Type != 1 && len > 0) return true;
  122. for(int i = 0; i < len; i++){
  123. switch(_comm.step){
  124. case STEP_HEAD:
  125. if(data[i] == 0xbb){
  126. _comm.data[0] = 0xbb;
  127. _comm.step = STEP_ADDR;
  128. }
  129. break;
  130. case STEP_ADDR:
  131. _comm.data[1] = data[i];
  132. _comm.step = STEP_LEN;
  133. break;
  134. case STEP_LEN:
  135. if (data[i] == 0x0B) {
  136. _comm.data[2] = 0x0B;
  137. _comm.dataLength = 0x0B;
  138. _comm.step = STEP_DIR;
  139. } else {
  140. _comm.step = STEP_HEAD;
  141. }
  142. break;
  143. case STEP_DIR:
  144. if (data[i] == 0x05) {
  145. _comm.data[3] = 0x05;
  146. _comm.step = STEP_DATA;
  147. _comm.dataCount = 4;
  148. } else {
  149. _comm.step = STEP_HEAD;
  150. }
  151. break;
  152. case STEP_DATA:
  153. _comm.data[_comm.dataCount++] = data[i];
  154. if(_comm.dataCount == _comm.dataLength){
  155. ReceiveSuccessful = true;
  156. _comm.step = STEP_HEAD;
  157. }
  158. break;
  159. default:
  160. _comm.step = STEP_HEAD;
  161. break;
  162. }
  163. }
  164. if(!ReceiveSuccessful) {
  165. return false;
  166. }
  167. return deserialization(_comm.data, _comm.dataLength, msg_id);
  168. }
  169. /**
  170. * @description: 打开
  171. * @param {int} deivceId
  172. * @return {*}
  173. */
  174. int32_t Open(std::string DeviceId, uint8_t* data, int& length){
  175. UltControl ultControl;
  176. ultControl.deviceId = std::atoi(DeviceId.c_str());
  177. ultControl.cmd = EXPEL_POWER_ON | EXPEL_PLAY_DISPEL | EXPEL_BURST_FIRE;
  178. length = 6;
  179. serialization(ultControl, (char*)data);
  180. return 0;
  181. }
  182. /**
  183. * @description: 关闭
  184. * @param {int} deivceId
  185. * @return {*}
  186. */
  187. int32_t Close(std::string DeviceId, uint8_t* data, int& length){
  188. UltControl ultControl;
  189. ultControl.deviceId = std::atoi(DeviceId.c_str());
  190. ultControl.cmd = EXPEL_POWER_OFF | EXPEL_TURN_OFF_DISPEL | EXPEL_NOT_FIRE;
  191. length = 6;
  192. serialization(ultControl, (char*)data);
  193. return 0;
  194. }
  195. /**
  196. * @description: 开炮
  197. * @param {int} deivceId
  198. * @return {*}
  199. */
  200. int32_t Fire(std::string DeviceId, uint8_t* data, int& length){
  201. return Open(DeviceId, data, length);
  202. }
  203. /**
  204. * @description: 状态
  205. * @param {int} deivceId
  206. * @return {*}
  207. */
  208. int32_t Status(std::string DeviceId, uint8_t* data, int& length){
  209. UltControl ultControl;
  210. ultControl.deviceId = std::atoi(DeviceId.c_str());
  211. ultControl.cmd = 0;
  212. length = 6;
  213. bool staus = true;
  214. serialization(ultControl, (char*)data, staus);
  215. return 0;
  216. }
  217. /**
  218. * @description: 判定msg是否为status
  219. * @param {int} &msg_id
  220. * @return {*}
  221. */
  222. bool getStatusMsg(int &msg_id){
  223. if(msg_id != EXPEL_STAUS_ACK) return false;
  224. return true;
  225. }
  226. /**
  227. * @description: 构建设备信息
  228. * @param {SendDevice&} sendDevice
  229. * @return {*}
  230. */
  231. void BuildDeviceInfo(SendDevice& sendDevice){
  232. sendDevice.deviceId = atoi(this->getDeviceId().c_str());
  233. sendDevice.commandCode = "1";
  234. sendDevice.status = 1;
  235. sendDevice.workStatus = 1;
  236. sendDevice.powerStatus = 1;
  237. sendDevice.batteryStatus = 1;
  238. sendDevice.num = 0;
  239. sendDevice.oxyPressure = 0;
  240. sendDevice.gasPressure = 0;
  241. sendDevice.remark = "";
  242. sendDevice.attr1 = std::to_string(this->getVoltage()); // 电压
  243. sendDevice.attr2 = std::to_string(this->getCurrent()); // 电流
  244. sendDevice.attr3 = std::to_string(this->getPressure()); // 声压
  245. }
  246. };