IR-protocol/IR_config.h

117 lines
8.8 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <Arduino.h>
/*//////////////////////////////////////////////////////////////////////////////////////
Адресация с 1 до 65 499
65 500 ~ 65 535 - широковещательные пакеты (всем), возможно разделить на 35 типов
Адрес 0 запрещен и зарезервирован под NULL
IR_MSG_ACCEPT с адреса 0 воспринимается всеми устройствами
/```````````````````````````````````````````````` data pack `````````````````````````````````````````````\                                  
                                                                                                         
{``````````} [````````````````````````] [````````````````````] [````````````````````````] [``````````````]
{ msg type } [ addr_self uint16_t ] [ addr_to uint16_t ] [ data bytes ] [ CRC Bytes ]
{..........} [........................] [....................] [........................] [..............]
                                                                                                          
{ aka size } [addr_self_H][addr_self_L] [addr_to_H][addr_to_L] [data_H][data_n..][data_L] [ crc1 ][ crc2 ]
|     0           1            2              3         4          5                         |       |    
\____________________________________________________________________________________________/       |    
|                                                                                                    |    
\____________________________________________________________________________________________________/    
msg type:
                                        //  __________
                                        // | 01234567 |
                                        //  ----------
                                        // | xxx..... | = тип сообщения
                                        // | ...xxxxx | = длина (максимум 32 бита)
                                        //  ---------- */
#define IR_MSG_ 0U // | 000..... | = = ??
#define IR_MSG_ACCEPT 1U // | 001..... | = */ /* = подтверждение
#define IR_MSG_REQUEST 2U // | 010..... | = */ /* = запрос
#define IR_MSG_ 3U // | 011..... | = */ /* = ??
#define IR_MSG_ 4U // | 100..... | = */ /* = ??
#define IR_MSG_ 5U // | 101..... | = */ /* = ??
#define IR_MSG_DATA_NOACCEPT 6U // | 110..... | = */ /* = данные, не требующие подтверждения
//                                      // | \\\xxxxx | = = L длина данных
#define IR_MSG_DATA_ACCEPT 7U // | 111..... | = */ /* = данные требующие подтверждения
//                                      // | \\\xxxxx | = = L длина данных
/*   // ----------
/```````````````````` подтверждение ```````````````````\      /``````````````````````````````````````` запрос ``````````````````````````````````\
                                                                                                                      
{``````````} [````````````````````````] [``````````````]      {``````````} [````````````````````````] [````````````````````````] [``````````````]
{ msg type } [ addr_self uint16_t ] [ CRC Bytes ]      { msg type } [ addr_self uint16_t ] [ addr_to uint16_t ] [ CRC Bytes ]
{..........} [........................] [..............]      {..........} [........................] [........................] [..............]
                                                                                                                                                 
{ 001..... } [addr_self_H][addr_self_L] [ crc1 ][ crc2 ]      { 010..... } [addr_self_H][addr_self_L] [addr_self_H][addr_self_L] [ crc1 ][ crc2 ]
|     0            1           2           3       4          |     0            1           2              3           4           5       6    
\__________________________________________/       |          \_____________________________________________________________________/       |    
|                                                  |          |                                                                             |    
\__________________________________________________/          \_____________________________________________________________________________/    
*/
#define IR_MASK_MSG_TYPE 0b00000111
#define IR_MASK_MSG_INFO 0b00011111
/*
/////////////////////////////////////////////////////////////////////////////////////*/
#define IR_bytePerPack 3 // колличество байтов в пакете
#define IR_freeFrec true
/////////////////////////////////////////////////////////////////////////////////////
#define IR_bitPerByte 8U // Колличество бит в байте
#define IR_addrBytes 2
#define IR_msgBytes 1
#define IR_crcBytes 2
typedef uint16_t crc_t;
#define IR_poly1 0x31
#define IR_poly2 0x8C
#define IR_syncBits 3U // количество битов синхронизации
#define IR_dataByteSizeMax (msgBytes + addrBytes + addrBytes + bytePerPack + crcBytes)
// размер msg в битах // размер короткой посылки в битах
#define IR_dataBitSize ((8 + syncBits) * dataByteSizeMax) // размер посылки с данными в битах
#define IR_bufferBitSizeMax (dataBitSize) // Размер буффера в битах
//const auto x = bufferBitSizeMax;
#define IR_preambPulse 3U
#define IR_preambFronts (preambPulse*2) // количество фронтов преамбулы
#define IR_carrierFrec 38000U // частота несущей
#define IR_carrierPeriod (1000000U/carrierFrec) // период несущей в us
// В процессе работы значения будут отклонятся в соответствии с предыдущим битом
#define IR_bitActiveTakts 25U // длительность активной части бита в тактах
#define IR_bitPauseTakts 5U // длительность промежутков в тактах
#define IR_bitTakts (bitActiveTakts+bitPauseTakts*2U) // Общая длительность бита в тактах
#define IR_bitTime (bitTakts*carrierPeriod) // Общая длительность бита
//const auto viewValue = bitTime;
#define IR_tolerance 300U
class IR_FOX {
private:
bool isSending = false;
public:
uint8_t crc8(uint8_t* data, uint8_t start, uint8_t end, uint8_t poly) { //TODO: сделать возможность межбайтовой проверки
uint8_t crc = 0xff;
size_t i, j;
for (i = start; i < end; i++) {
crc ^= data[i];
for (j = 0; j < 8; j++) {
if ((crc & 0x80) != 0)
crc = (uint8_t)((crc << 1) ^ poly);
else
crc <<= 1;
}
}
return crc;
}
};