RGB_Controller.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include "RGB_Controller.h"
  2. void RGB_Response_Func(uint8_t* data);
  3. uint8_t RGB_BufCal(uint8_t srcid);
  4. void RGB_Alarm_Operate(void);
  5. void RGB_Data_Stack(uint8_t* rgb_buf);
  6. void RGB_Response_Func(uint8_t* data){
  7. RGB_CMD_T type = data[bluecell_type];
  8. #if 0
  9. for(uint8_t i = 0; i < 10; i++){
  10. printf("%02x ",data[i]);
  11. }
  12. #endif
  13. switch(type){
  14. case RGB_Status_Data_Request:
  15. Uart2_Data_Send(data,RGB_SensorDataRequest_Length);
  16. break;
  17. case RGB_ControllerID_SET:
  18. Uart1_Data_Send(data,RGB_ControllerID_SET_Length);
  19. break;
  20. case RGB_SensorID_SET:
  21. Uart2_Data_Send(data,RGB_SensorIDAutoSetRequest_Length);
  22. break;
  23. case RGB_Status_Data_Response:
  24. Uart1_Data_Send(data,RGB_SensorDataResponse_Length);
  25. break;
  26. case RGB_ControllerLimitSet:
  27. Uart1_Data_Send(data,data[bluecell_length] + 3);
  28. Flash_write(&data[0]);
  29. break;
  30. case RGB_Sensor_Start:
  31. case RGB_Sensor_Check:
  32. Uart2_Data_Send(data,RGB_SensorIDAutoSetRequest_Length);
  33. break;
  34. case RGB_Sensor_Ack:
  35. Uart2_Data_Send(data,data[bluecell_length] + 3);
  36. break;
  37. case RGB_Reset:
  38. case RGB_SensorID_SET_Success:
  39. break;
  40. }
  41. }
  42. uint8_t SensorID_Cnt = 0;
  43. uint8_t SensorID_buf[8] = {0,};
  44. uint16_t RGB_SensorRedLimit_Buf[9]={0,};
  45. uint16_t RGB_SensorGreenLimit_Buf[9]={0,};
  46. uint16_t RGB_SensorBlueLimit_Buf[9]={0,};
  47. uint16_t Sensor_red[9] = {0,};
  48. uint16_t Sensor_green[9] = {0,};
  49. uint16_t Sensor_blue[9] = {0,};
  50. uint8_t LED_Alarm[9] = {0,};
  51. void RGB_Sensor_LED_Alarm_ON(uint8_t id ){
  52. switch(id){
  53. case 0:// 모든 LED의 전원을 ON
  54. HAL_GPIO_WritePin(LED_CH1_GPIO_Port,LED_CH1_Pin,GPIO_PIN_RESET);
  55. HAL_GPIO_WritePin(LED_CH2_GPIO_Port,LED_CH2_Pin,GPIO_PIN_RESET);
  56. HAL_GPIO_WritePin(LED_CH3_GPIO_Port,LED_CH3_Pin,GPIO_PIN_RESET);
  57. HAL_GPIO_WritePin(LED_CH4_GPIO_Port,LED_CH4_Pin,GPIO_PIN_RESET);
  58. HAL_GPIO_WritePin(LED_CH5_GPIO_Port,LED_CH5_Pin,GPIO_PIN_RESET);
  59. HAL_GPIO_WritePin(LED_CH6_GPIO_Port,LED_CH6_Pin,GPIO_PIN_RESET);
  60. HAL_GPIO_WritePin(LED_CH7_GPIO_Port,LED_CH7_Pin,GPIO_PIN_RESET);
  61. HAL_GPIO_WritePin(LED_CH8_GPIO_Port,LED_CH8_Pin,GPIO_PIN_RESET);
  62. break;
  63. case 1:
  64. HAL_GPIO_WritePin(LED_CH1_GPIO_Port,LED_CH1_Pin,GPIO_PIN_RESET);
  65. break;
  66. case 2:
  67. HAL_GPIO_WritePin(LED_CH2_GPIO_Port,LED_CH2_Pin,GPIO_PIN_RESET);
  68. break;
  69. case 3:
  70. HAL_GPIO_WritePin(LED_CH3_GPIO_Port,LED_CH3_Pin,GPIO_PIN_RESET);
  71. break;
  72. case 4:
  73. HAL_GPIO_WritePin(LED_CH4_GPIO_Port,LED_CH4_Pin,GPIO_PIN_RESET);
  74. break;
  75. case 5:
  76. HAL_GPIO_WritePin(LED_CH5_GPIO_Port,LED_CH5_Pin,GPIO_PIN_RESET);
  77. break;
  78. case 6:
  79. HAL_GPIO_WritePin(LED_CH6_GPIO_Port,LED_CH6_Pin,GPIO_PIN_RESET);
  80. break;
  81. case 7:
  82. HAL_GPIO_WritePin(LED_CH7_GPIO_Port,LED_CH7_Pin,GPIO_PIN_RESET);
  83. break;
  84. case 8:
  85. HAL_GPIO_WritePin(LED_CH8_GPIO_Port,LED_CH8_Pin,GPIO_PIN_RESET);
  86. break;
  87. }
  88. }
  89. void RGB_Sensor_LED_Alarm_OFF(uint8_t id ){
  90. switch(id){
  91. case 0:// 모든 LED의 전원을 OFF
  92. HAL_GPIO_WritePin(LED_CH1_GPIO_Port,LED_CH1_Pin,GPIO_PIN_SET);
  93. HAL_GPIO_WritePin(LED_CH2_GPIO_Port,LED_CH2_Pin,GPIO_PIN_SET);
  94. HAL_GPIO_WritePin(LED_CH3_GPIO_Port,LED_CH3_Pin,GPIO_PIN_SET);
  95. HAL_GPIO_WritePin(LED_CH4_GPIO_Port,LED_CH4_Pin,GPIO_PIN_SET);
  96. HAL_GPIO_WritePin(LED_CH5_GPIO_Port,LED_CH5_Pin,GPIO_PIN_SET);
  97. HAL_GPIO_WritePin(LED_CH6_GPIO_Port,LED_CH6_Pin,GPIO_PIN_SET);
  98. HAL_GPIO_WritePin(LED_CH7_GPIO_Port,LED_CH7_Pin,GPIO_PIN_SET);
  99. HAL_GPIO_WritePin(LED_CH8_GPIO_Port,LED_CH8_Pin,GPIO_PIN_SET);
  100. break;
  101. case 1:
  102. HAL_GPIO_WritePin(LED_CH1_GPIO_Port,LED_CH1_Pin,GPIO_PIN_SET);
  103. break;
  104. case 2:
  105. HAL_GPIO_WritePin(LED_CH2_GPIO_Port,LED_CH2_Pin,GPIO_PIN_SET);
  106. break;
  107. case 3:
  108. HAL_GPIO_WritePin(LED_CH3_GPIO_Port,LED_CH3_Pin,GPIO_PIN_SET);
  109. break;
  110. case 4:
  111. HAL_GPIO_WritePin(LED_CH4_GPIO_Port,LED_CH4_Pin,GPIO_PIN_SET);
  112. break;
  113. case 5:
  114. HAL_GPIO_WritePin(LED_CH5_GPIO_Port,LED_CH5_Pin,GPIO_PIN_SET);
  115. break;
  116. case 6:
  117. HAL_GPIO_WritePin(LED_CH6_GPIO_Port,LED_CH6_Pin,GPIO_PIN_SET);
  118. break;
  119. case 7:
  120. HAL_GPIO_WritePin(LED_CH7_GPIO_Port,LED_CH7_Pin,GPIO_PIN_SET);
  121. break;
  122. case 8:
  123. HAL_GPIO_WritePin(LED_CH8_GPIO_Port,LED_CH8_Pin,GPIO_PIN_SET);
  124. break;
  125. }
  126. }
  127. void RGB_Alarm_Operate(void){
  128. uint8_t temp_warning = 0;
  129. for(uint8_t i = 1; i <= (SensorID_Cnt); i++){
  130. if(LED_Alarm[SensorID_buf[i]] == 1){
  131. HAL_GPIO_WritePin(LED_ALARM_GPIO_Port, LED_ALARM_Pin, GPIO_PIN_SET); //표지 LED
  132. RGB_Sensor_LED_Alarm_ON(SensorID_buf[i]);
  133. temp_warning = 1;
  134. }else{
  135. RGB_Sensor_LED_Alarm_OFF(SensorID_buf[i]);
  136. }
  137. }
  138. if(temp_warning == 0){ // 8개의 Sensor가 전부 정상일 때 만 동작
  139. HAL_GPIO_WritePin(LED_ALARM_GPIO_Port, LED_ALARM_Pin, GPIO_PIN_RESET); //표지 LED
  140. RGB_Sensor_LED_Alarm_OFF(0); //모든 Sensor가 정상일 때는 LED 가 켜지지 않는다.
  141. }
  142. }
  143. void RGB_Alarm_Check(uint8_t* data){
  144. Sensor_red[data[bluecell_srcid]] = ((data[bluecell_red_H + 2] << 8)| data[bluecell_red_L + 2]);
  145. Sensor_green[data[bluecell_srcid]] = ((data[bluecell_green_H + 2] << 8)| data[bluecell_green_L + 2]);
  146. Sensor_blue[data[bluecell_srcid]] = ((data[bluecell_blue_H + 2] << 8)| data[bluecell_blue_L + 2]);
  147. for(uint8_t i = 1; i <= (SensorID_Cnt); i++){
  148. if(RGB_SensorRedLimit_Buf[SensorID_buf[i]] >= Sensor_red[SensorID_buf[i]]
  149. || RGB_SensorGreenLimit_Buf[SensorID_buf[i]] >= Sensor_green[SensorID_buf[i]]
  150. || RGB_SensorBlueLimit_Buf[SensorID_buf[i]] >= Sensor_blue[SensorID_buf[i]]) {
  151. LED_Alarm[SensorID_buf[i]] = 1;
  152. }else{
  153. LED_Alarm[SensorID_buf[i]] = 0;
  154. }
  155. }
  156. RGB_Data_Stack(&LED_Alarm[1]);
  157. }
  158. uint8_t RGB_DeviceStatusCheck(void){
  159. uint8_t ret = 0;
  160. for(uint8_t i = 1; i <= SensorID_Cnt; i++){
  161. if(SensorID_buf[i] > 0){
  162. ret += 0x01 << (SensorID_buf[i] - 1);
  163. }
  164. }
  165. return ret;
  166. }
  167. uint8_t Lora_Buf[100] = {0,};
  168. #if 0 // PYJ.2019.04.14_BEGIN -- //Uart Value Data
  169. void RGB_Data_Stack(uint8_t* rgb_buf){
  170. uint8_t mynumcnt = RGB_BufCal(rgb_buf[bluecell_srcid]);
  171. Lora_Buf[bluecell_stx] = 0xbe;
  172. Lora_Buf[bluecell_type] = RGB_Lora_Data_Report;
  173. Lora_Buf[bluecell_length] = Lora_Max_Amount + 2; //length 1byte + type 1byte + RGB Data 60byte
  174. Lora_Buf[bluecell_srcid] = MyControllerID;
  175. Lora_Buf[mynumcnt] = rgb_buf[bluecell_srcid];
  176. Lora_Buf[mynumcnt + 1] = rgb_buf[bluecell_red_H + 2];
  177. Lora_Buf[mynumcnt + 2] = rgb_buf[bluecell_red_L + 2];
  178. Lora_Buf[mynumcnt + 3] = rgb_buf[bluecell_green_H + 2];
  179. Lora_Buf[mynumcnt + 4] = rgb_buf[bluecell_green_L + 2];
  180. Lora_Buf[mynumcnt + 5] = rgb_buf[bluecell_blue_H + 2];
  181. Lora_Buf[mynumcnt + 6] = rgb_buf[bluecell_blue_L + 2];
  182. LoraDataSendSet(1);
  183. }
  184. uint8_t RGB_BufCal(uint8_t srcid){
  185. uint8_t ret = 0;
  186. switch(srcid){
  187. case 1:ret = 4;break;
  188. case 2:ret = 11;break;
  189. case 3:ret = 18;break;
  190. case 4:ret = 25;break;
  191. case 5:ret = 32;break;
  192. case 6:ret = 39;break;
  193. case 7:ret = 46;break;
  194. case 8:ret = 53;break;
  195. }
  196. return ret;
  197. }
  198. #else //Uart Flag Data
  199. /*
  200. 현재 Controller 가지고 있는 RGB Sensor ID Check
  201. 현재 비정상적인 동작을 하는 Sensor 에대한 Flag 정보
  202. */
  203. void RGB_Data_Stack(uint8_t* rgb_buf){
  204. memset(&Lora_Buf[0],0x00,8);
  205. /*********************FIX DATA*************************************/
  206. Lora_Buf[bluecell_stx] = 0xbe;
  207. Lora_Buf[bluecell_srcid + 4] = 0xeb;
  208. Lora_Buf[bluecell_type] = RGB_Lora_Data_Report;
  209. Lora_Buf[bluecell_length] = Lora_Max_Amount;// RGB Data 5byte
  210. Lora_Buf[bluecell_srcid] = MyControllerID;
  211. /*********************FIX DATA*************************************/
  212. if(RGB_BufCal(SensorID_buf[1]) == 0){//아무런 Device가 존재 하지않을 때
  213. printf("Not Exist Device \n");
  214. return;
  215. }
  216. for(uint8_t i = 1; i <= (SensorID_Cnt); i++){
  217. Lora_Buf[bluecell_srcid + 1] |= 0x01 << (SensorID_buf[i] - 1);
  218. }
  219. for(uint8_t i = 0; i < 8; i++){
  220. Lora_Buf[bluecell_srcid + 2] |= rgb_buf[i] << i ;
  221. }
  222. Lora_Buf[bluecell_srcid + 3]= STH30_CreateCrc(&Lora_Buf[bluecell_type],Lora_Buf[bluecell_length]);
  223. LoraDataSendSet(1);
  224. }
  225. /*
  226. RGB_Data_Stack에 Lora에 Data를 보내기 위해 Buffer에 Data를 쌓을 때
  227. ID 마다 Location Cnt
  228. */
  229. uint8_t RGB_BufCal(uint8_t srcid){
  230. uint8_t ret = 0;
  231. switch(srcid){
  232. case 1:ret = 4;break;
  233. case 2:ret = 7;break;
  234. case 3:ret = 10;break;
  235. case 4:ret = 13;break;
  236. case 5:ret = 16;break;
  237. case 6:ret = 29;break;
  238. case 7:ret = 32;break;
  239. case 8:ret = 35;break;
  240. }
  241. return ret;
  242. }
  243. #endif // PYJ.2019.04.14_END --
  244. uint8_t datalosscnt[9] = {0,};
  245. void RGB_Controller_Func(uint8_t* data){
  246. RGB_CMD_T type = data[bluecell_type];
  247. // static uint8_t temp_sensorid;
  248. uint8_t Result_buf[100] = {0,};
  249. switch(type){
  250. case RGB_Status_Data_Request:
  251. datalosscnt[data[bluecell_srcid + 1]]++;
  252. if(datalosscnt[data[bluecell_srcid + 1]] > 3 && data[bluecell_srcid + 1] != 0){
  253. RGB_SensorIDAutoSet(1);
  254. memset(&SensorID_buf[0],0x00,8);
  255. }
  256. data[5] = STH30_CreateCrc(&data[bluecell_type],data[bluecell_length]);
  257. memcpy(&Result_buf[bluecell_stx],&data[bluecell_stx],RGB_SensorDataRequest_Length);
  258. break;
  259. case RGB_ControllerID_SET:
  260. memcpy(&Result_buf[bluecell_stx],&data[bluecell_stx],data[bluecell_length] + 3);
  261. Result_buf[7] = data[bluecell_srcid]; // �긽��諛⑹쓽 SRC ID�뒗 �굹�쓽 DST ID�씠�떎.
  262. MyControllerID = Result_buf[7] = data[7];//�긽��諛⑹쓽 DST ID �뒗 �굹�쓽 ID �씠�떎.
  263. break;
  264. case RGB_SensorID_SET:
  265. RGB_SensorIDAutoSet(1);
  266. memcpy(&Result_buf[bluecell_stx],&data[bluecell_stx],data[bluecell_length] + 3);
  267. Result_buf[5] = STH30_CreateCrc(&Result_buf[bluecell_type],Result_buf[bluecell_length]);
  268. break;
  269. case RGB_SensorID_SET_Success:
  270. SensorID_Cnt++;
  271. SensorID_buf[SensorID_Cnt] = data[bluecell_length + 1];
  272. break;
  273. case RGB_Status_Data_Response:
  274. datalosscnt[data[bluecell_srcid]] = 0;
  275. data[bluecell_length] += 1;// Device On OFF status Send byte
  276. data[bluecell_srcid + 9] = RGB_DeviceStatusCheck();// Device On OFF status Send byte
  277. memcpy(&Result_buf[bluecell_stx],&data[bluecell_stx],data[bluecell_length] + 3);
  278. Result_buf[5] = STH30_CreateCrc(&Result_buf[bluecell_type],Result_buf[bluecell_length]);
  279. RGB_Alarm_Check(&data[bluecell_stx]);
  280. break;
  281. case RGB_ControllerLimitSet:
  282. memcpy(&Result_buf[bluecell_stx],&data[bluecell_stx],data[bluecell_length] + 3);
  283. RGB_SensorRedLimit_Buf[data[bluecell_dstid]] = ((data[bluecell_red_H] << 8) |data[bluecell_red_L]);
  284. RGB_SensorGreenLimit_Buf[data[bluecell_dstid]] = ((data[bluecell_green_H] << 8) |data[bluecell_green_L]);
  285. RGB_SensorBlueLimit_Buf[data[bluecell_dstid]] = ((data[bluecell_blue_H] << 8) |data[bluecell_blue_L]);
  286. Result_buf[bluecell_crc] = STH30_CreateCrc(&Result_buf[bluecell_type],Result_buf[bluecell_length]);
  287. break;
  288. case RGB_Reset:
  289. NVIC_SystemReset();
  290. break;
  291. case RGB_ID_Allocate_Request:
  292. break;
  293. default:
  294. break;
  295. }
  296. RGB_Response_Func(&Result_buf[bluecell_stx]);
  297. return;
  298. }