diff --git a/Src/ask_decoder.c b/Src/ask_decoder.c new file mode 100644 index 0000000..f1227cd --- /dev/null +++ b/Src/ask_decoder.c @@ -0,0 +1,302 @@ +/* +------------------------------------------------------------------------------ +~ File : ask_decoder.c +~ Author : Base algorithm by Mohammad Mazarei, Edited by Majid Derhambakhsh +~ Version: V0.0.0 +~ Created: 12/20/2021 09:00:00 AM +~ Brief : +~ Support: + E-Mail : Majid.Derhambakhsh@gmail.com (subject : Embedded Library Support) + + Github : https://github.com/Majid-Derhambakhsh +------------------------------------------------------------------------------ +~ Description: + +~ Attention : + +~ Changes : + +------------------------------------------------------------------------------ +*/ + +#include "ask_decoder.h" + +/* ............. Global variables ............ */ +#ifdef PROGMEM + + const uint32_t Bit_Shift[32] PROGMEM = + { + 0x00000001,0x00000002,0x00000004,0x00000008, + 0x00000010,0x00000020,0x00000040,0x00000080, + 0x00000100,0x00000200,0x00000400,0x00000800, + 0x00001000,0x00002000,0x00004000,0x00008000, + 0x00010000,0x00020000,0x00040000,0x00080000, + 0x00100000,0x00200000,0x00400000,0x00800000, + 0x01000000,0x02000000,0x04000000,0x08000000, + 0x10000000,0x20000000,0x40000000,0x80000000, + }; + +#else + + #define pgm_read_dword(addr) (*(const uint32_t *)(addr)) + + const uint32_t Bit_Shift[32] = + { + 0x00000001,0x00000002,0x00000004,0x00000008, + 0x00000010,0x00000020,0x00000040,0x00000080, + 0x00000100,0x00000200,0x00000400,0x00000800, + 0x00001000,0x00002000,0x00004000,0x00008000, + 0x00010000,0x00020000,0x00040000,0x00080000, + 0x00100000,0x00200000,0x00400000,0x00800000, + 0x01000000,0x02000000,0x04000000,0x08000000, + 0x10000000,0x20000000,0x40000000,0x80000000, + }; + +#endif // PROGMEM + +/* ................ Functions ................ */ +/* + * Function: ASK_IsValidRemote + * --------------------------- + * Get remote detecting status + * + * Param : + * ASK: Pointer to ASK module type + * + * Returns: + * returns 0 on not detecting valid remote + * returns 1 on detecting valid remote + * + * Example: + * ASK_TypeDef RF433; + * + * if (ASK_IsValidRemote(&RF433)) + * {...} + * + */ +uint8_t ASK_IsValidRemote(ASK_TypeDef *ASK) +{ + return (ASK->SignalData.ReviseFlag == 1); +} + +/* + * Function: ASK_GetRemoteCode + * --------------------------- + * Get detected remote code + * + * Param : + * ASK: Pointer to ASK module type + * + * Returns: + * returns received remote code: 20bit[RemoteID] + 4bit[Key] + * + * Example: + * uint32_t RemoteCode = 0; + * + * ASK_TypeDef RF433; + * + * RemoteCode = ASK_GetRemoteCode(&RF433); + * ... + * + */ +uint32_t ASK_GetRemoteCode(ASK_TypeDef *ASK) +{ + + ASK->SignalData.ReviseFlag = 0; + + return ASK->ReceivedCode; + +} + +/* + * Function: ASK_GetRemoteInfo + * --------------------------- + * Get detected remote ID and Key + * + * Param : + * ASK : Pointer to ASK module type + * RemoteID : Pointer to get Remote ID + * PressedKey: Pointer to get Key code + * + * Returns: + * - + * + * Example: + * uint8_t RemoteKey = 0; + * uint32_t RemoteID = 0; + * + * ASK_TypeDef RF433; + * + * ASK_GetRemoteInfo(&RF433, &RemoteID, &RemoteKey); + * ... + * + */ +void ASK_GetRemoteInfo(ASK_TypeDef *ASK, uint32_t *RemoteID, uint8_t *PressedKey) +{ + + ASK->SignalData.ReviseFlag = 0; + + *RemoteID = (ASK->ReceivedCode >> 4) & 0xFFFFF; + *PressedKey = ASK->ReceivedCode & 0xF; + +} + +/* ::::: ISR Handling ::::: */ +/* + * Function: ASK_ISRHandle_Rising + * ------------------------------ + * Handling received signal on rising edge + * + * Param : + * ASK: Pointer to ASK module type + * + * Returns: + * - + * + * Example: + * ASK_TypeDef RF433; + * + * void GPIO_ISR_RisingEdge() + * { + * ASK_ISRHandle_Rising(&RF433); + * ... + * } + * ... + * + */ +void ASK_ISRHandle_Rising(ASK_TypeDef *ASK) +{ + + /* ----- Store falling time of signal ----- */ + ASK->SignalData.RisingTimeStart = _ASKTimer_GetTick(); + + if (ASK->SignalData.RisingTimeStart <= ASK->SignalData.FallingTimeStart) + { + ASK->SignalData.FallingTime = (_ASK_TIMER_MAX_TICK - ASK->SignalData.FallingTimeStart) + ASK->SignalData.RisingTimeStart; + } + else + { + ASK->SignalData.FallingTime = ASK->SignalData.RisingTimeStart - ASK->SignalData.FallingTimeStart; + } + + /* -------------- Check pulse ------------- */ + if(_ASK_SIGNAL_IS_SYNC_PULSE(ASK->SignalData.RisingTime, ASK->SignalData.FallingTime)) /* Start Sync*/ + { + + ASK->SignalData.SyncStarted = 1; + ASK->SignalData.BitIndex = 0; + ASK->SignalData.ReceivedData = 0; + + } + else + { + /* Start Received */ + if(ASK->SignalData.SyncStarted == 1) + { + + if(ASK->SignalData.BitIndex < 23) + { + + if(_ASK_SIGNAL_BIT_IS_ZERO(ASK->SignalData.RisingTime, ASK->SignalData.FallingTime)) + { + ASK->SignalData.BitIndex++; + } + else if(_ASK_SIGNAL_BIT_IS_ONE(ASK->SignalData.RisingTime, ASK->SignalData.FallingTime)) + { + + ASK->SignalData.ReceivedData |= pgm_read_dword(&Bit_Shift[(23 - ASK->SignalData.BitIndex)]); + ASK->SignalData.BitIndex++; + + } + else + { + + ASK->SignalData.SyncStarted = 0; + ASK->SignalData.BitIndex = 0; + + } + + } + else + { + + if(_ASK_SIGNAL_BIT_IS_ZERO(ASK->SignalData.RisingTime, ASK->SignalData.FallingTime)) + { + ASK->SignalData.BitIndex++; + } + else if(_ASK_SIGNAL_BIT_IS_ONE(ASK->SignalData.RisingTime, ASK->SignalData.FallingTime)) + { + ASK->SignalData.ReceivedData |= pgm_read_dword(&Bit_Shift[(23 - ASK->SignalData.BitIndex)]); + ASK->SignalData.BitIndex++; + } + else + { + + ASK->SignalData.SyncStarted = 0; + ASK->SignalData.BitIndex = 0; + + } + + if(ASK->SignalData.SyncStarted) + { + + /* Data bits received */ + ASK->SignalData.BitIndex = 0; + ASK->SignalData.SyncStarted = 0; + ASK->ReceivedCode = ASK->SignalData.ReceivedData; + ASK->SignalData.ReviseFlag = 1; + + } + + } + + } + /* End of start received */ + } + + /* ------------ Start sampling ------------ */ + //ASK->Control.RisingTimeStart = _Timer_GetTick(); + +} + +/* + * Function: ASK_ISRHandle_Falling + * ------------------------------- + * Handling received signal on falling edge + * + * Param : + * ASK: Pointer to ASK module type + * + * Returns: + * - + * + * Example: + * ASK_TypeDef RF433; + * + * void GPIO_ISR_FallingEdge() + * { + * ASK_ISRHandle_Falling(&RF433); + * ... + * } + * ... + * + */ +void ASK_ISRHandle_Falling(ASK_TypeDef *ASK) +{ + + /* ----- Store rising time of signal ----- */ + ASK->SignalData.FallingTimeStart = _ASKTimer_GetTick(); + + if (ASK->SignalData.FallingTimeStart <= ASK->SignalData.RisingTimeStart) + { + ASK->SignalData.RisingTime = (_ASK_TIMER_MAX_TICK - ASK->SignalData.RisingTimeStart) + ASK->SignalData.FallingTimeStart; + } + else + { + ASK->SignalData.RisingTime = ASK->SignalData.FallingTimeStart - ASK->SignalData.RisingTimeStart; + } + + /* ----------- Start sampling ------------ */ + //ASK->Control.FallingTimeStart = _Timer_GetTick(); + +} diff --git a/Src/ask_decoder.h b/Src/ask_decoder.h new file mode 100644 index 0000000..759c45e --- /dev/null +++ b/Src/ask_decoder.h @@ -0,0 +1,194 @@ +/* +------------------------------------------------------------------------------ +~ File : ask_decoder.h +~ Author : Base algorithm by Mohammad Mazarei, Edited by Majid Derhambakhsh +~ Version: V0.0.0 +~ Created: 12/20/2021 09:00:00 AM +~ Brief : +~ Support: + E-Mail : Majid.Derhambakhsh@gmail.com (subject : Embedded Library Support) + + Github : https://github.com/Majid-Derhambakhsh +------------------------------------------------------------------------------ +~ Description: + +~ Attention : + +~ Changes : + +------------------------------------------------------------------------------ +*/ + +#ifndef __ASK_DECODER_H_ +#define __ASK_DECODER_H_ + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Include ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ +#include <stdint.h> + +#include "ask_decoder_conf.h" + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Defines ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ +#ifdef _ASK_USE_LOW_SENSITIVITY + + /* Low Sensitivity */ + #define _ASK_SIGNAL_IS_SYNC_PULSE(T1,T2) (T2 > (T1*28) && T2 < (T1*34)) + #define _ASK_SIGNAL_BIT_IS_ZERO(T1,T2) (T2 > (T1*1) && T2 < (T1*5)) + #define _ASK_SIGNAL_BIT_IS_ONE(T1,T2) (T1 > (T2*1) && T1 < (T2*5)) + +#else + + /* High Sensitivity */ + #define _ASK_SIGNAL_IS_SYNC_PULSE(T1,T2) (T2 > (T1*29) && T2 < (T1*32)) + #define _ASK_SIGNAL_BIT_IS_ZERO(T1,T2) (T2 > (T1*2) && T2 < (T1*4)) + #define _ASK_SIGNAL_BIT_IS_ONE(T1,T2) (T1 > (T2*2) && T1 < (T2*4)) + +#endif // _ASK_USE_LOW_SENSITIVITY + +/* ---------------------------- Public ---------------------------- */ +/* ---------------------- Define by compiler ---------------------- */ +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Types ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ +typedef struct /* ASK Type */ +{ + + struct + { + + uint16_t FallingTimeStart; + uint16_t RisingTimeStart; + uint16_t FallingTime; + uint16_t RisingTime; + uint16_t SyncStarted; + uint32_t ReceivedData; + + uint8_t BitIndex; + volatile uint8_t ReviseFlag; + + }SignalData; + + volatile uint32_t ReceivedCode; + +}ASK_TypeDef; + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Variables ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Enum ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Struct ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Prototype ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ +/* + * Function: ASK_IsValidRemote + * --------------------------- + * Get remote detecting status + * + * Param : + * ASK: Pointer to ASK module type + * + * Returns: + * returns 0 on not detecting valid remote + * returns 1 on detecting valid remote + * + * Example: + * ASK_TypeDef RF433; + * + * if (ASK_IsValidRemote(&RF433)) + * {...} + * + */ +uint8_t ASK_IsValidRemote(ASK_TypeDef *ASK); + +/* + * Function: ASK_GetRemoteCode + * --------------------------- + * Get detected remote code + * + * Param : + * ASK: Pointer to ASK module type + * + * Returns: + * returns received remote code: 20bit[RemoteID] + 4bit[Key] + * + * Example: + * uint32_t RemoteCode = 0; + * + * ASK_TypeDef RF433; + * + * RemoteCode = ASK_GetRemoteCode(&RF433); + * ... + * + */ +uint32_t ASK_GetRemoteCode(ASK_TypeDef *ASK); + +/* + * Function: ASK_GetRemoteInfo + * --------------------------- + * Get detected remote ID and Key + * + * Param : + * ASK : Pointer to ASK module type + * RemoteID : Pointer to get Remote ID + * PressedKey: Pointer to get Key code + * + * Returns: + * - + * + * Example: + * uint8_t RemoteKey = 0; + * uint32_t RemoteID = 0; + * + * ASK_TypeDef RF433; + * + * ASK_GetRemoteInfo(&RF433, &RemoteID, &RemoteKey); + * ... + * + */ +void ASK_GetRemoteInfo(ASK_TypeDef *ASK, uint32_t *RemoteID, uint8_t *PressedKey); + +/* ::::: ISR Handling ::::: */ +/* + * Function: ASK_ISRHandle_Rising + * ------------------------------ + * Handling received signal on rising edge + * + * Param : + * ASK: Pointer to ASK module type + * + * Returns: + * - + * + * Example: + * ASK_TypeDef RF433; + * + * void GPIO_ISR_RisingEdge() + * { + * ASK_ISRHandle_Rising(&RF433); + * ... + * } + * ... + * + */ +void ASK_ISRHandle_Rising(ASK_TypeDef *ASK); + +/* + * Function: ASK_ISRHandle_Falling + * ------------------------------- + * Handling received signal on falling edge + * + * Param : + * ASK: Pointer to ASK module type + * + * Returns: + * - + * + * Example: + * ASK_TypeDef RF433; + * + * void GPIO_ISR_FallingEdge() + * { + * ASK_ISRHandle_Falling(&RF433); + * ... + * } + * ... + * + */ +void ASK_ISRHandle_Falling(ASK_TypeDef *ASK); + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ End of the program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ +#endif /* __ASK_DECODER_H_ */ diff --git a/Src/ask_decoder_conf.h b/Src/ask_decoder_conf.h new file mode 100644 index 0000000..29811e4 --- /dev/null +++ b/Src/ask_decoder_conf.h @@ -0,0 +1,45 @@ +/* +------------------------------------------------------------------------------ +~ File : ask_decoder_conf.h +~ Author : Base algorithm by Mohammad Mazarei, Edited by Majid Derhambakhsh +~ Version: V0.0.0 +~ Created: 12/20/2021 09:00:00 AM +~ Brief : +~ Support: + E-Mail : Majid.Derhambakhsh@gmail.com (subject : Embedded Library Support) + + Github : https://github.com/Majid-Derhambakhsh +------------------------------------------------------------------------------ +~ Description: + +~ Attention : + +~ Changes : + +------------------------------------------------------------------------------ +*/ + +#ifndef __ASK_DECODER_CONF_H_ +#define __ASK_DECODER_CONF_H_ + +/* ~~~~~~~~~~~~~~~ Include files ~~~~~~~~~~~~~~~ */ +/* PGMSPACE header */ +//#include <avr/pgmspace.h> + +/* Timer header */ +//#include "timer.h" + +/* HAL or MCU header */ +//#include <avr/io.h> +//#include "stm32f1xx_hal.h" + +/* ~~~~~~~~~~~~~~~~~~ Options ~~~~~~~~~~~~~~~~~~ */ +/* ~~~~~~~~ Timer ~~~~~~~~ */ +#define _ASKTimer_GetTick() Timer1_GetTick() +#define _ASK_TIMER_MAX_TICK UINT16_MAX + +/* ~~~ ASK sensitivity ~~~ */ +#define _ASK_USE_LOW_SENSITIVITY // Comment this line if you need to use high sensitivity + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ +#endif /* __ASK_DECODER_CONF_H_ */