tabr

CybergunProtoClass.cpp

Jul 4th, 2022 (edited)
388
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * CybergunProtoClass.cpp
  3. *
  4. * Created: 03.06.2022 13:21:22
  5. * Author: tabr
  6. */
  7.  
  8.  
  9. #include "CybergunProtoClass.hpp"
  10.  
  11. void CybergunProtoClass::rx_ISR(uint8_t data){
  12.     #ifdef USE_UART_DEBUG_COMMANDS
  13.         if (this->rx_buffer_ptr < CG_PACKET_STUCTURE_CRC16H){
  14.             if (data == UART_COMMAND_PRORO_RESET){
  15.                 this->reset();
  16.                 return;
  17.             } else if (data == UART_COMMAND_DUMMY){
  18.                 UDR = UART_COMMAND_DUMMY;
  19.                 return;
  20.             } else if (data == UART_COMMAND_SHOT){
  21.                 Cybergun.shot_detected();
  22.                 return;
  23.             }
  24.             if (this->rx_buffer_ptr >= CG_PACKET_STUCTURE_PACKET_SIZE){
  25.                 return;
  26.             }
  27.         }
  28.     #endif //USE_UART_DEBUG_COMMANDS
  29.     //ibi(PORTD,4);
  30.     this->rx_buffer[this->rx_buffer_ptr] = data;
  31.     this->rx_buffer_ptr++;//can't be more than CG_PACKET_STUCTURE_PACKET_SIZE-1
  32.     //UDR = (this->buffer_ptr+48);
  33.     if (this->is_packet_ready_to_proceed()){
  34.         if (!(this->is_packet_valid())){//reset if packet invalid
  35.             this->reset();
  36.             return;
  37.         }
  38.     }
  39. }
  40. void CybergunProtoClass::ms_ISR(void){
  41.     //if (this->rx_buffer_ptr >= CG_PACKET_STUCTURE_PACKET_SIZE){
  42.         //sbi(PORTD,6);
  43.     //}
  44.     if (this->antistuck_counter < STUCK_THRESHOLD && this->rx_buffer_ptr != 0){
  45.         this->antistuck_counter++;
  46.     } else {
  47.         this->antistuck_counter = 0;
  48.     }
  49. }
  50. void CybergunProtoClass::clear_rx_bufer(void){
  51.     memset(this->rx_buffer, 0, CG_PACKET_STUCTURE_PACKET_SIZE);
  52. }
  53. void CybergunProtoClass::clear_tx_bufer(void){
  54.     memset(this->tx_buffer, 0, CG_PACKET_STUCTURE_PACKET_SIZE);
  55. }
  56. void CybergunProtoClass::reset(void){
  57.     this->clear_rx_bufer();
  58.     this->clear_tx_bufer();
  59.     this->rx_buffer_ptr = 0;
  60.     //this->tx_buffer_ptr = 0;
  61.     this->current_status = STATUS_AWAINING_REQUEST;
  62.     this->antistuck_counter = 0;
  63.     //sbi(PORTD, 4);
  64.     //sbi(PORTD, 6);
  65.     //_delay_ms(100);
  66.     //cbi(PORTD, 4);
  67.     //cbi(PORTD, 6);
  68.     //UDR = 90;
  69.    
  70. }
  71. bool CybergunProtoClass::is_packet_valid(void){
  72.     if (this->rx_buffer[CG_PACKET_STUCTURE_HEAD] >= CG_PACKET_STUCTURE_HEAD_TYPE_SIZE){
  73.         //UDR = 'A';
  74.         this->send_error(CG_PACKET_STUCTURE_ERROR_UNKNOWN_PACKET_HEAD);
  75.         return false;
  76.     }
  77.     if (this->rx_buffer_ptr != CG_PACKET_STUCTURE_PACKET_SIZE){
  78.         //UDR = 'B';
  79.         this->send_error(CG_PACKET_STUCTURE_ERROR_INTERNAL_ERROR_BUFFER_PTR);
  80.         return false;
  81.     }
  82.     switch (this->rx_buffer[CG_PACKET_STUCTURE_HEAD])
  83.     {
  84.         case CG_PACKET_STUCTURE_HEAD_TYPE_REQUEST:
  85.         case CG_PACKET_STUCTURE_HEAD_TYPE_ANSWER:
  86.         if (this->rx_buffer[CG_PACKET_STUCTURE_HEAD] >= CG_PACKET_STUCTURE_REQUEST_SIZE){
  87.             //UDR = 'C';
  88.             this->send_error(CG_PACKET_STUCTURE_ERROR_UNKNOWN_REQUEST);
  89.             return false;
  90.         }
  91.         break;
  92.     }
  93.     #ifndef CRC_CALC_BYPASS
  94.     uint16_t crc16 = crc16ccitt(this->rx_buffer, CG_PACKET_STUCTURE_PACKET_SIZE - 2 );
  95.     if (crc16 != this->get_packet_crcr16()){
  96.         //UDR = 'D';
  97.         this->send_error(CG_PACKET_STUCTURE_ERROR_CRC_ERROR);
  98.         return false;
  99.     }
  100.     #endif // CRC_CALC_BYPASS
  101.     return true;
  102. }
  103. uint16_t CybergunProtoClass::get_packet_crcr16(void){
  104.     return (this->rx_buffer[CG_PACKET_STUCTURE_CRC16H] << 8) | this->rx_buffer[CG_PACKET_STUCTURE_CRC16L];
  105. }
  106. bool CybergunProtoClass::is_packet_ready_to_proceed(void){
  107.     return this->rx_buffer_ptr == CG_PACKET_STUCTURE_PACKET_SIZE;
  108. }
  109. void CybergunProtoClass::main(void){
  110.     if (this->is_packet_ready_to_proceed()){
  111.         if (this->is_packet_valid()){
  112.             switch (this->rx_buffer[CG_PACKET_STUCTURE_HEAD]){
  113.                 case CG_PACKET_STUCTURE_HEAD_TYPE_REQUEST:
  114.                 if (this->rx_buffer[CG_PACKET_STUCTURE_SUBHEAD] == CG_PACKET_STUCTURE_REQUEST_SET_STORAGE){
  115.                     Cybergun.Storage.set_raw_MSB(this->rx_buffer[CG_PACKET_STUCTURE_DATA0], this->rx_buffer[CG_PACKET_STUCTURE_DATA1]);
  116.                     Cybergun.update_status();
  117.                     this->send_answer_operation_successfull();
  118.                     this->reset();
  119.                     return;
  120.                 } else if (this->rx_buffer[CG_PACKET_STUCTURE_SUBHEAD] == CG_PACKET_STUCTURE_REQUEST_GET_STORAGE){
  121.                     this->send_answer(Cybergun.Storage.get_raw_MSB());
  122.                     this->reset();
  123.                     return;
  124.                 } else if (this->rx_buffer[CG_PACKET_STUCTURE_SUBHEAD] == CG_PACKET_STUCTURE_REQUEST_UPTIME){
  125.                     this->send_answer(uptime);
  126.                     this->reset();
  127.                     return;
  128.                 }
  129.                 break;
  130.             }
  131.         }
  132.     }
  133.     if (this->antistuck_counter >= this->STUCK_THRESHOLD){
  134.         //this->send_error(this->CG_PACKET_STUCTURE_ERROR_CRC_ERROR);
  135.         this->send_mcuirq_command(CybergunProtoClass::MCUIRQ_COMMANDS::MCUIRQ_COMMAND_ERROR_DETECTED,  CybergunProtoClass::CG_PACKET_STUCTURE_ERRORS::CG_PACKET_STUCTURE_ERROR_STUCK_DETECTED);
  136.         this->reset();
  137.         return;
  138.     }
  139.    
  140. }
  141. CybergunProtoClass::CybergunProtoClass(void){
  142.     this->reset();
  143. };
  144. void CybergunProtoClass::send_answer(uint16_t dataMSB){
  145.     this->send_answer((dataMSB >> 8), (dataMSB & 0xFF));
  146. }
  147. void CybergunProtoClass::send_answer(uint32_t dataMSB){
  148.     this->send_answer((dataMSB >> 24), (dataMSB >> 16), (dataMSB >> 8), (dataMSB & 0xFF));
  149. }
  150. void CybergunProtoClass::send_answer_operation_successfull(void){
  151.     this->send_answer(0,0);
  152. }
  153. void CybergunProtoClass::send_answer(uint8_t data0, uint8_t data1){
  154.     this->clear_tx_bufer();
  155.     this->tx_buffer[this->CG_PACKET_STUCTURE_HEAD]      = this->CG_PACKET_STUCTURE_HEAD_TYPE_ANSWER;
  156.     this->tx_buffer[this->CG_PACKET_STUCTURE_SUBHEAD]   = 0;
  157.     this->tx_buffer[this->CG_PACKET_STUCTURE_DATA0]     = data0;
  158.     this->tx_buffer[this->CG_PACKET_STUCTURE_DATA1]     = data1;
  159.     this->refresh_tx_packet_crc();
  160.     this->send_buffer();
  161. }
  162. void CybergunProtoClass::send_answer(uint8_t data0, uint8_t data1, uint8_t data2, uint8_t data3){
  163.     this->clear_tx_bufer();
  164.     this->tx_buffer[this->CG_PACKET_STUCTURE_HEAD]      = this->CG_PACKET_STUCTURE_HEAD_TYPE_ANSWER;
  165.     this->tx_buffer[this->CG_PACKET_STUCTURE_SUBHEAD]   = 0;
  166.     this->tx_buffer[this->CG_PACKET_STUCTURE_DATA0]     = data0;
  167.     this->tx_buffer[this->CG_PACKET_STUCTURE_DATA1]     = data1;
  168.     this->tx_buffer[this->CG_PACKET_STUCTURE_DATA2]     = data2;
  169.     this->tx_buffer[this->CG_PACKET_STUCTURE_DATA3]     = data3;
  170.     this->refresh_tx_packet_crc();
  171.     this->send_buffer();
  172. }
  173.  
  174. //void CybergunProtoClass::send_mcuirq_command(MCUIRQ_COMMANDS command){
  175. //  this->send_mcuirq_command(command, 0);
  176. //}
  177. void CybergunProtoClass::send_mcuirq_command(MCUIRQ_COMMANDS command, uint8_t data){
  178.  
  179.     this->tx_buffer[this->CG_PACKET_STUCTURE_HEAD]      = this->CG_PACKET_STUCTURE_HEAD_TYPE_MCUIRQ;
  180.     this->tx_buffer[this->CG_PACKET_STUCTURE_SUBHEAD]   = command;
  181.     this->tx_buffer[this->CG_PACKET_STUCTURE_DATA0]     = data;
  182.     this->refresh_tx_packet_crc();
  183.     this->send_buffer();
  184. }
  185.  
  186. void CybergunProtoClass::send_error(CG_PACKET_STUCTURE_ERRORS error){
  187.     this->clear_tx_bufer();
  188.     this->tx_buffer[this->CG_PACKET_STUCTURE_HEAD]      = this->CG_PACKET_STUCTURE_HEAD_TYPE_ERROR;
  189.     this->tx_buffer[this->CG_PACKET_STUCTURE_SUBHEAD]   = this->PACKET_COMMAND_EMPTY;
  190.     this->tx_buffer[this->CG_PACKET_STUCTURE_DATA0]     = error;
  191.     this->tx_buffer[this->CG_PACKET_STUCTURE_DATA1]     = 0;
  192.     this->refresh_tx_packet_crc();
  193.     this->send_buffer();
  194. }
  195. void CybergunProtoClass::refresh_tx_packet_crc(void){
  196.     uint16_t crc = this->calculate_tx_packet_crc();
  197.     this->tx_buffer[this->CG_PACKET_STUCTURE_CRC16H] = crc >> 8;
  198.     this->tx_buffer[this->CG_PACKET_STUCTURE_CRC16L] = crc & 0xFF;
  199. }
  200. uint16_t CybergunProtoClass::calculate_tx_packet_crc(void){
  201.     return crc16ccitt(this->tx_buffer, this->CG_PACKET_STUCTURE_PACKET_SIZE - this->CRC_LENGTH_BYTES);
  202. }
  203. void CybergunProtoClass::send_buffer(void){
  204.     TRX_environment::send_data(this->tx_buffer, CG_PACKET_STUCTURE_PACKET_SIZE);
  205.     /*
  206.     this->tx_buffer_ptr = 0;
  207.     while (this->tx_buffer_ptr < this->CG_PACKET_STUCTURE_PACKET_SIZE){
  208.         UDR=this->tx_buffer[this->tx_buffer_ptr];
  209.         while ((UCSRA & DATA_REGISTER_EMPTY)==0);//wait for TX complete
  210.         this->tx_buffer_ptr++;
  211.     }
  212.     */
  213. }
RAW Paste Data Copied