IR-protocol/IR_config.h
DashyFox 7d9ac1420b Revert "v1.0.1 fix define conflict"
This reverts commit 9be22f1b55deade38f872af759c2bb57dc6e81d3.
2023-10-06 15:39:48 +03:00

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 bytePerPack 3 // колличество байтов в пакете
#define freeFrec true
/////////////////////////////////////////////////////////////////////////////////////
#define bitPerByte 8U // Колличество бит в байте
#define addrBytes 2
#define msgBytes 1
#define crcBytes 2
typedef uint16_t crc_t;
#define poly1 0x31
#define poly2 0x8C
#define syncBits 3U // количество битов синхронизации
#define dataByteSizeMax (msgBytes + addrBytes + addrBytes + bytePerPack + crcBytes)
// размер msg в битах // размер короткой посылки в битах
#define dataBitSize ((8 + syncBits) * dataByteSizeMax) // размер посылки с данными в битах
#define bufferBitSizeMax (dataBitSize) // Размер буффера в битах
//const auto x = bufferBitSizeMax;
#define preambPulse 3U
#define preambFronts (preambPulse*2) // количество фронтов преамбулы
#define carrierFrec 38000U // частота несущей
#define carrierPeriod (1000000U/carrierFrec) // период несущей в us
// В процессе работы значения будут отклонятся в соответствии с предыдущим битом
#define bitActiveTakts 25U // длительность активной части бита в тактах
#define bitPauseTakts 5U // длительность промежутков в тактах
#define bitTakts (bitActiveTakts+bitPauseTakts*2U) // Общая длительность бита в тактах
#define bitTime (bitTakts*carrierPeriod) // Общая длительность бита
//const auto viewValue = bitTime;
#define 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;
}
};