add
This commit is contained in:
161
john103C6T6NewVer/linkBlink/linkBlink.c
Normal file
161
john103C6T6NewVer/linkBlink/linkBlink.c
Normal file
@@ -0,0 +1,161 @@
|
||||
#include "linkBlink.h"
|
||||
|
||||
|
||||
static uint32_t GetTick(LedBlinker_HandleTypeDef* hblinker) {
|
||||
if (hblinker->htim != NULL) {
|
||||
return __HAL_TIM_GET_COUNTER(hblinker->htim);
|
||||
}
|
||||
return HAL_GetTick(); // Èñïîëüçóåì ñèñòåìíûé òàéìåð åñëè ñâîé íå çàäàí
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef LedBlinker_Init(LedBlinker_HandleTypeDef* hblinker,
|
||||
GPIO_TypeDef* led_port, uint16_t led_pin,
|
||||
GPIO_TypeDef* de_re_port, uint16_t de_re_pin,
|
||||
TIM_HandleTypeDef* htim) {
|
||||
if (hblinker == NULL || led_port == NULL) {
|
||||
return HAL_ERROR;
|
||||
}
|
||||
|
||||
// Èíèöèàëèçàöèÿ ïîëåé
|
||||
hblinker->led_port = led_port;
|
||||
hblinker->led_pin = led_pin;
|
||||
hblinker->de_re_port = de_re_port;
|
||||
hblinker->de_re_pin = de_re_pin;
|
||||
hblinker->htim = htim;
|
||||
hblinker->blink_duration = 100;
|
||||
hblinker->last_blink_tick = 0;
|
||||
hblinker->led_state = false;
|
||||
hblinker->blinking = false;
|
||||
hblinker->blink_count = 0;
|
||||
hblinker->target_blink_count = 1;
|
||||
hblinker->last_packet_tick = 0;
|
||||
|
||||
// Íàñòðîéêà ïèíà ñâåòîäèîäà
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_InitStruct.Pin = led_pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(led_port, &GPIO_InitStruct);
|
||||
|
||||
// Âûêëþ÷àåì ñâåòîäèîä
|
||||
HAL_GPIO_WritePin(led_port, led_pin, GPIO_PIN_RESET);
|
||||
|
||||
// Íàñòðîéêà ïèíà óïðàâëåíèÿ RS485 åñëè èñïîëüçóåòñÿ
|
||||
if (de_re_port != NULL && de_re_pin != 0) {
|
||||
GPIO_InitStruct.Pin = de_re_pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(de_re_port, &GPIO_InitStruct);
|
||||
|
||||
// Ðåæèì ïðèåìà ïî óìîë÷àíèþ
|
||||
HAL_GPIO_WritePin(de_re_port, de_re_pin, GPIO_PIN_RESET);
|
||||
}
|
||||
|
||||
return HAL_OK;
|
||||
}
|
||||
|
||||
void LedBlinker_Update(LedBlinker_HandleTypeDef* hblinker) {
|
||||
if (hblinker == NULL || !hblinker->blinking) return;
|
||||
|
||||
uint32_t current_tick = HAL_GetTick();
|
||||
|
||||
if ((current_tick - hblinker->last_blink_tick) >= hblinker->blink_duration) {
|
||||
hblinker->last_blink_tick = current_tick;
|
||||
|
||||
// Ïåðåêëþ÷àåì ñîñòîÿíèå ñâåòîäèîäà
|
||||
hblinker->led_state = !hblinker->led_state;
|
||||
HAL_GPIO_WritePin(hblinker->led_port, hblinker->led_pin,
|
||||
hblinker->led_state ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
||||
|
||||
// Åñëè ñâåòîäèîä âûêëþ÷èëñÿ, óìåíüøàåì ñ÷åò÷èê
|
||||
if (!hblinker->led_state) {
|
||||
hblinker->blink_count++;
|
||||
if (hblinker->blink_count >= hblinker->target_blink_count) {
|
||||
hblinker->blinking = false;
|
||||
HAL_GPIO_WritePin(hblinker->led_port, hblinker->led_pin, GPIO_PIN_RESET);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LedBlinker_ProcessPacket(LedBlinker_HandleTypeDef* hblinker,
|
||||
uint8_t* data, uint16_t len) {
|
||||
if (hblinker == NULL || data == NULL || len == 0) return;
|
||||
|
||||
hblinker->last_packet_tick = HAL_GetTick();
|
||||
|
||||
// Àíàëèç ïàêåòà è âûáîð ïàòòåðíà ìèãàíèÿ
|
||||
if (len >= 1) {
|
||||
// Ïðèìåð: ïåðâûé áàéò îïðåäåëÿåò êîëè÷åñòâî ìèãàíèé
|
||||
uint8_t blink_count = data[0];
|
||||
if (blink_count == 0) blink_count = 1;
|
||||
if (blink_count > 20) blink_count = 20; // Îãðàíè÷åíèå
|
||||
|
||||
// Âòîðîé áàéò ìîæåò îïðåäåëÿòü äëèòåëüíîñòü (åñëè åñòü)
|
||||
uint32_t duration = 100;
|
||||
if (len >= 2) {
|
||||
duration = 50 + (data[1] * 10);
|
||||
if (duration > 1000) duration = 1000;
|
||||
}
|
||||
|
||||
LedBlinker_BlinkPattern(hblinker, blink_count, duration);
|
||||
}
|
||||
|
||||
// Äîïîëíèòåëüíûå ïàòòåðíû â çàâèñèìîñòè îò äàííûõ
|
||||
if (len >= 4 && data[0] == 0xFF) {
|
||||
// Ñïåöèàëüíûé ïàòòåðí äëÿ êîìàíä
|
||||
LedBlinker_BlinkPattern(hblinker, 5, 50);
|
||||
}
|
||||
}
|
||||
|
||||
void LedBlinker_BlinkOnce(LedBlinker_HandleTypeDef* hblinker, uint32_t duration) {
|
||||
if (hblinker == NULL) return;
|
||||
|
||||
hblinker->blink_duration = duration;
|
||||
hblinker->target_blink_count = 1;
|
||||
hblinker->blink_count = 0;
|
||||
hblinker->blinking = true;
|
||||
hblinker->led_state = false;
|
||||
hblinker->last_blink_tick = GetTick(hblinker);
|
||||
|
||||
// Íåìåäëåííîå âêëþ÷åíèå
|
||||
HAL_GPIO_WritePin(hblinker->led_port, hblinker->led_pin, GPIO_PIN_SET);
|
||||
hblinker->led_state = true;
|
||||
}
|
||||
|
||||
void LedBlinker_BlinkPattern(LedBlinker_HandleTypeDef* hblinker,
|
||||
uint8_t count, uint32_t duration) {
|
||||
if (hblinker == NULL || count == 0) return;
|
||||
|
||||
hblinker->blink_duration = duration;
|
||||
hblinker->target_blink_count = count;
|
||||
hblinker->blink_count = 0;
|
||||
hblinker->blinking = true;
|
||||
hblinker->led_state = false;
|
||||
hblinker->last_blink_tick = HAL_GetTick();
|
||||
|
||||
// Íåìåäëåííîå âêëþ÷åíèå
|
||||
HAL_GPIO_WritePin(hblinker->led_port, hblinker->led_pin, GPIO_PIN_SET);
|
||||
hblinker->led_state = true;
|
||||
}
|
||||
|
||||
void LedBlinker_SetRS485Mode(LedBlinker_HandleTypeDef* hblinker, bool transmit) {
|
||||
if (hblinker == NULL || hblinker->de_re_port == NULL) return;
|
||||
|
||||
HAL_GPIO_WritePin(hblinker->de_re_port, hblinker->de_re_pin,
|
||||
transmit ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
||||
}
|
||||
|
||||
bool LedBlinker_IsBlinking(LedBlinker_HandleTypeDef* hblinker) {
|
||||
return (hblinker != NULL) ? hblinker->blinking : false;
|
||||
}
|
||||
|
||||
uint32_t LedBlinker_GetTimeSinceLastPacket(LedBlinker_HandleTypeDef* hblinker) {
|
||||
if (hblinker == NULL) return 0;
|
||||
|
||||
if (hblinker->last_packet_tick == 0) return 0;
|
||||
|
||||
return GetTick(hblinker) - hblinker->last_packet_tick;
|
||||
}
|
||||
90
john103C6T6NewVer/linkBlink/linkBlink.h
Normal file
90
john103C6T6NewVer/linkBlink/linkBlink.h
Normal file
@@ -0,0 +1,90 @@
|
||||
#ifndef LED_BLINKER_RS485_H
|
||||
#define LED_BLINKER_RS485_H
|
||||
|
||||
#include "stm32f1xx_hal.h" // Èçìåíèòå ïîä ñâîé êîíêðåòíûé ìèêðîêîíòðîëëåð
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct {
|
||||
GPIO_TypeDef* led_port; // Ïîðò ñâåòîäèîäà
|
||||
uint16_t led_pin; // Ïèí ñâåòîäèîäà
|
||||
GPIO_TypeDef* de_re_port; // Ïîðò óïðàâëåíèÿ RS485 (NULL åñëè íå èñïîëüçóåòñÿ)
|
||||
uint16_t de_re_pin; // Ïèí óïðàâëåíèÿ RS485
|
||||
TIM_HandleTypeDef* htim; // Òàéìåð äëÿ îòñ÷åòà âðåìåíè
|
||||
uint32_t blink_duration; // Äëèòåëüíîñòü ìèãàíèÿ â ìñ
|
||||
uint32_t last_blink_tick; // Âðåìÿ ïîñëåäíåãî ìèãàíèÿ (â òèêàõ)
|
||||
bool led_state; // Òåêóùåå ñîñòîÿíèå ñâåòîäèîäà
|
||||
bool blinking; // Ôëàã ìèãàíèÿ
|
||||
uint8_t blink_count; // Ñ÷åò÷èê ìèãàíèé
|
||||
uint8_t target_blink_count; // Öåëåâîå êîëè÷åñòâî ìèãàíèé
|
||||
uint32_t last_packet_tick; // Âðåìÿ ïîñëåäíåãî ïàêåòà
|
||||
} LedBlinker_HandleTypeDef;
|
||||
|
||||
/**
|
||||
* Èíèöèàëèçàöèÿ ñòðóêòóðû ìèãàëêè
|
||||
* @param hblinker - óêàçàòåëü íà ñòðóêòóðó
|
||||
* @param led_port - ïîðò ñâåòîäèîäà
|
||||
* @param led_pin - ïèí ñâåòîäèîäà
|
||||
* @param de_re_port - ïîðò óïðàâëåíèÿ RS485 (NULL åñëè íå èñïîëüçóåòñÿ)
|
||||
* @param de_re_pin - ïèí óïðàâëåíèÿ RS485
|
||||
* @param htim - òàéìåð äëÿ âðåìåííûõ îòñ÷åòîâ
|
||||
* @return HAL_OK èëè HAL_ERROR
|
||||
*/
|
||||
HAL_StatusTypeDef LedBlinker_Init(LedBlinker_HandleTypeDef* hblinker,
|
||||
GPIO_TypeDef* led_port, uint16_t led_pin,
|
||||
GPIO_TypeDef* de_re_port, uint16_t de_re_pin,
|
||||
TIM_HandleTypeDef* htim);
|
||||
|
||||
/**
|
||||
* Îáðàáîò÷èê ìèãàíèÿ (âûçûâàòü â îñíîâíîì öèêëå)
|
||||
* @param hblinker - óêàçàòåëü íà ñòðóêòóðó
|
||||
*/
|
||||
void LedBlinker_Update(LedBlinker_HandleTypeDef* hblinker);
|
||||
|
||||
/**
|
||||
* Îáðàáîòêà ïîëó÷åííîãî ïàêåòà
|
||||
* @param hblinker - óêàçàòåëü íà ñòðóêòóðó
|
||||
* @param data - óêàçàòåëü íà äàííûå
|
||||
* @param len - äëèíà äàííûõ
|
||||
*/
|
||||
void LedBlinker_ProcessPacket(LedBlinker_HandleTypeDef* hblinker,
|
||||
uint8_t* data, uint16_t len);
|
||||
|
||||
/**
|
||||
* Ïðîñòîå ìèãàíèå ïðè ïîëó÷åíèè ïàêåòà
|
||||
* @param hblinker - óêàçàòåëü íà ñòðóêòóðó
|
||||
* @param duration - äëèòåëüíîñòü ìèãàíèÿ â ìñ
|
||||
*/
|
||||
void LedBlinker_BlinkOnce(LedBlinker_HandleTypeDef* hblinker, uint32_t duration);
|
||||
|
||||
/**
|
||||
* Ìèãàíèå ñ îïðåäåëåííûì ïàòòåðíîì
|
||||
* @param hblinker - óêàçàòåëü íà ñòðóêòóðó
|
||||
* @param count - êîëè÷åñòâî ìèãàíèé
|
||||
* @param duration - äëèòåëüíîñòü îäíîãî ìèãàíèÿ â ìñ
|
||||
*/
|
||||
void LedBlinker_BlinkPattern(LedBlinker_HandleTypeDef* hblinker,
|
||||
uint8_t count, uint32_t duration);
|
||||
|
||||
/**
|
||||
* Óñòàíîâêà ðåæèìà RS485
|
||||
* @param hblinker - óêàçàòåëü íà ñòðóêòóðó
|
||||
* @param transmit - true äëÿ ïåðåäà÷è, false äëÿ ïðèåìà
|
||||
*/
|
||||
void LedBlinker_SetRS485Mode(LedBlinker_HandleTypeDef* hblinker, bool transmit);
|
||||
|
||||
/**
|
||||
* Ïðîâåðêà, ìèãàåò ëè ñåé÷àñ ñâåòîäèîä
|
||||
* @param hblinker - óêàçàòåëü íà ñòðóêòóðó
|
||||
* @return true åñëè ìèãàåò
|
||||
*/
|
||||
bool LedBlinker_IsBlinking(LedBlinker_HandleTypeDef* hblinker);
|
||||
|
||||
/**
|
||||
* Ïîëó÷åíèå âðåìåíè ñ ïîñëåäíåãî ïàêåòà
|
||||
* @param hblinker - óêàçàòåëü íà ñòðóêòóðó
|
||||
* @return âðåìÿ â ìñ ñ ïîñëåäíåãî ïàêåòà
|
||||
*/
|
||||
uint32_t LedBlinker_GetTimeSinceLastPacket(LedBlinker_HandleTypeDef* hblinker);
|
||||
|
||||
#endif // LED_BLINKER_RS485_H
|
||||
Reference in New Issue
Block a user