|
| 1 | +/* |
| 2 | + This project is free software: you can redistribute it and/or modify |
| 3 | + it under the terms of the GNU General Public License as published by |
| 4 | + the Free Software Foundation, either version 3 of the License, or |
| 5 | + (at your option) any later version. |
| 6 | +
|
| 7 | + Multiprotocol is distributed in the hope that it will be useful, |
| 8 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + GNU General Public License for more details. |
| 11 | +
|
| 12 | + You should have received a copy of the GNU General Public License |
| 13 | + along with Multiprotocol. If not, see <http://www.gnu.org/licenses/>. |
| 14 | + */ |
| 15 | + |
| 16 | +#if defined(CG022_NRF24L01_INO) |
| 17 | + |
| 18 | +#include "iface_nrf24l01.h" |
| 19 | + |
| 20 | +#define CG022_PACKET_PERIOD 2315 |
| 21 | +#define CG022_BIND_COUNT 166 |
| 22 | +#define CG022_PACKET_SIZE 10 |
| 23 | +#define CG022_RF_CHANNEL_COUNT 8 |
| 24 | + |
| 25 | +static const uint8_t PROGMEM CG022_hop[] = { 0x0A, 0x32, 0x14, 0x3C, 0x1E, 0x46, 0x00, 0x28 }; |
| 26 | +static const uint8_t CG022_addr[] = { 0x5A, 0x5A, 0x00, 0x33 }; |
| 27 | + |
| 28 | +static uint8_t CG022_scale_channel(uint8_t channel) |
| 29 | +{ |
| 30 | + return (convert_channel_8b(channel) + 2) >> 2; |
| 31 | +} |
| 32 | + |
| 33 | +static void __attribute__((unused)) CG022_set_channel() |
| 34 | +{ |
| 35 | + NRF24L01_WriteReg(NRF24L01_05_RF_CH, pgm_read_byte_near(&CG022_hop[hopping_frequency_no])); |
| 36 | + hopping_frequency_no++; |
| 37 | + if (hopping_frequency_no >= CG022_RF_CHANNEL_COUNT) |
| 38 | + hopping_frequency_no = 0; |
| 39 | +} |
| 40 | + |
| 41 | +static void __attribute__((unused)) CG022_build_bind_packet() |
| 42 | +{ |
| 43 | + uint8_t id = rx_tx_addr[3] & 0x3F; |
| 44 | + |
| 45 | + packet[0] = 0x0A; |
| 46 | + packet[1] = id; |
| 47 | + packet[2] = rx_tx_addr[0]; |
| 48 | + packet[3] = rx_tx_addr[1]; |
| 49 | + packet[4] = rx_tx_addr[2]; |
| 50 | + packet[5] = 0x06; |
| 51 | + packet[6] = 0xAB; |
| 52 | + packet[7] = 0xFC; |
| 53 | + packet[8] = 0xAD; |
| 54 | + packet[9] = 0x00; |
| 55 | +} |
| 56 | + |
| 57 | +static void __attribute__((unused)) CG022_build_data_packet() |
| 58 | +{ |
| 59 | + uint8_t id = rx_tx_addr[3] & 0x3F; |
| 60 | + |
| 61 | + packet[0] = 0x0A; |
| 62 | + packet[1] = id; |
| 63 | + packet[2] = CG022_scale_channel(THROTTLE); |
| 64 | + packet[3] = CG022_scale_channel(ELEVATOR); |
| 65 | + packet[4] = CG022_scale_channel(RUDDER); |
| 66 | + packet[5] = CG022_scale_channel(AILERON); |
| 67 | + packet[6] = 0x20 | GET_FLAG(CH6_SW, 0x80); |
| 68 | + packet[7] = 0x20 | GET_FLAG(CH5_SW, 0x40) | GET_FLAG(CH7_SW, 0x80); |
| 69 | + packet[8] = 0x20; |
| 70 | + packet[9] = (packet[2] + packet[3] + packet[4] + packet[5] + packet[6] + packet[7] + packet[8]) & 0xFF; |
| 71 | +} |
| 72 | + |
| 73 | +static void __attribute__((unused)) CG022_send_packet() |
| 74 | +{ |
| 75 | + if (IS_BIND_IN_PROGRESS) |
| 76 | + CG022_build_bind_packet(); |
| 77 | + else |
| 78 | + CG022_build_data_packet(); |
| 79 | + |
| 80 | + CG022_set_channel(); |
| 81 | + NRF24L01_WritePayload(packet, CG022_PACKET_SIZE); |
| 82 | + NRF24L01_SetPower(); |
| 83 | +} |
| 84 | + |
| 85 | +static void __attribute__((unused)) CG022_RF_init() |
| 86 | +{ |
| 87 | + NRF24L01_Initialize(); |
| 88 | + NRF24L01_WriteReg(NRF24L01_03_SETUP_AW, 0x02); |
| 89 | + NRF24L01_WriteRegisterMulti(NRF24L01_0A_RX_ADDR_P0, (uint8_t *)CG022_addr, sizeof(CG022_addr)); |
| 90 | + NRF24L01_WriteRegisterMulti(NRF24L01_10_TX_ADDR, (uint8_t *)CG022_addr, sizeof(CG022_addr)); |
| 91 | + NRF24L01_WriteReg(NRF24L01_11_RX_PW_P0, CG022_PACKET_SIZE); |
| 92 | + NRF24L01_SetBitrate(NRF24L01_BR_1M); |
| 93 | + NRF24L01_WriteReg(NRF24L01_00_CONFIG, _BV(NRF24L01_00_PWR_UP)); |
| 94 | +} |
| 95 | + |
| 96 | +uint16_t CG022_callback() |
| 97 | +{ |
| 98 | + #ifdef MULTI_SYNC |
| 99 | + telemetry_set_input_sync(CG022_PACKET_PERIOD); |
| 100 | + #endif |
| 101 | + if (bind_counter) |
| 102 | + { |
| 103 | + bind_counter--; |
| 104 | + if (bind_counter == 0) |
| 105 | + BIND_DONE; |
| 106 | + } |
| 107 | + CG022_send_packet(); |
| 108 | + return CG022_PACKET_PERIOD; |
| 109 | +} |
| 110 | + |
| 111 | +void CG022_init(void) |
| 112 | +{ |
| 113 | + BIND_IN_PROGRESS; // autobind protocol |
| 114 | + bind_counter = CG022_BIND_COUNT; |
| 115 | + hopping_frequency_no = 0; |
| 116 | + CG022_RF_init(); |
| 117 | + packet_period = CG022_PACKET_PERIOD; |
| 118 | +} |
| 119 | + |
| 120 | +#endif |
0 commit comments