Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
13 kB
3
Indexable
Never
#ifndef _UART_AVR_
#define _UART_AVR_
#include "platform.hpp"
#include "packetParser.hpp"
#include "packetWritter.hpp"
#include "buv.hpp"
#include "sensor.hpp"
#include "teilAlarm.hpp"

// Cкорость работы по UART
// static const unsigned int UartSpeedMin = 4800;  // Минимальная
// static const unsigned int UartSpeedMax = 19200; // Максимальная
unsigned int _currUartKskSpeed; // Текущая скорость работы uart по Кск

#define SET_BAUD_RATE 0x02

using namespace Hal::Board::Avr;

UartDevice *Ud = Hal::Board::Avr::getModemUart();
CSensorState sensState;
PacketWritter Writter;

class sensStatus : public TaskBase
{

    virtual bool task()
    {
        if (sensLinkStatus)
        {
            return true;
        }
        else
        {
            Writter.writeCmd(*Ud, UCMD_DO, InterfaceIdSensor, InterfaceIdModem, (uint8_t *)DO_SHOW_STATUS, 1);
            // getLed()->Off();
        }
        return false;
    }

private:
    PacketWritter Writter;

public:
    bool sensLinkStatus = false;
};
template <uint8_t ID_TAIL>
class CheckStatusTails : public PinHandler
{
    virtual void onStateChanged(bool state)
    {
        if (ID_TAIL == 0)
        {
            sensState.Alarm_Tail_1 = state;
        }
        else
        {
            sensState.Alarm_Tail_2 = state;
        }
        uint8_t tailsChange[2] = {SENS_STATE_NOTIFY, *(uint8_t *)&sensState};
        Writter.writeCmd(*Ud, UCMD_SENDDEFF, InterfaceIdSensor, InterfaceIdModem, tailsChange, 2);
    }
};

class UartH : public UartHandler, public TaskBase
{
    virtual void onTxBufferEmpty()
    {
    }

    virtual void onDataReceived(uint8_t *data, size_t count)
    {
        for (size_t i = 0; i < count; i++)
        {
            Packet.add(data[i]);
        }
    }

    virtual void onError(UartError error)
    {
    }

    virtual bool task()
    {
        uint8_t cmd;

        unsigned char setType[2] = {0x08, 0};
        uint8_t mas[5] = {EXT_DEV_TIME_NOTIFY, 0, 0, 0, 0};
        // uint8_t timetestt[1] = {0x0E};
        // uint8_t temp;
        timerOffBUV temp;
        uint8_t mas1[6] = {0x0B, EXT_DEV_PARAM_NOTIFY, autoBuv, 0, 0, 0};
        // unsigned char mas[2] = {DO_SHOW_STATUS, 0};
        unsigned char stndby[2] = {DO_STBY_MODE, 0};
        unsigned char CheckAll[2] = {0x04, 0};

        while (Packet.packetDetect())
        {
            if (Packet.adressForSensor())
            {
                if (Packet.getCmd(&cmd))
                {
                    unsigned char data[128] = {0};
                    uint8_t sizeData = 0;
                    switch (cmd)
                    {
                    case UCMD_PONG:
                        getExtDevCtrl()->Toggle();

                        break;
                    case UMSG_PONG8:
                        getExtDevCtrl()->Toggle();
                        break;
                    case UMSG_TXT:
                        if ((sizeData = Packet.getData(data, 128)))
                        {
                            getExtDevCtrl()->Toggle();
                        }
                        break;
                    case UMSG_SYS:
                        switch (Packet.getCmdUmsgSys())
                        {
                        case SYSTYP_ASKXTYP:
                            memcpy(setType + 1, (unsigned char *)&sensConf, 1);
                            Writter.writeCmd(*Ud, UCMD_DO, InterfaceIdSensor, InterfaceIdModem, setType, 2);
                            getExtDevCtrl()->Toggle();
                            break;
                        case SYSTYP_MYINFO:
                            if ((sizeData = Packet.getData(data, 128)) == sizeof(LinkInfo))
                            {
                                memcpy(&sensLink, data, sizeof(LinkInfo));
                                // memcpy(setType + 1, (unsigned char *)&sensLink, 1);
                                getStatus.sensLinkStatus = true;
                                // Writter.writeCmd(*Ud, UCMD_DO, InterfaceIdSensor, InterfaceIdModem, setType, 2);
                            }
                            break;
                        }
                        break;
                    case UCMD_DATA:
                        if ((sizeData = Packet.getData(data, 128)))
                        {
                            switch (data[0])
                            {
                            case SET_SENS_CONF:
                                memcpy(&sensConf, data + 1, 1);
                                if (*(uint8_t *)&sensConf == 0)
                                {
                                    setType[1] = 0x0c;
                                }
                                else
                                {
                                    memcpy(setType + 1, (unsigned char *)&sensConf, 1);
                                }
                                // Packet.HeaderPacket.Request = 1;
                                Writter.writeCmd(*Ud, UCMD_DO, InterfaceIdSensor, InterfaceIdModem, setType, 2);
                                break;
                            case GET_SENS_CONFIG:
                                memcpy(setType + 1, (unsigned char *)&sensConf, 1);
                                Writter.writeCmd(*Ud, UCMD_DO, InterfaceIdSensor, InterfaceIdModem, setType, 2);
                                break;
                            case ON_OFF_EXT_DEV:
                                if (data[1])
                                {
                                    controlBuv.On(timerBuv.time);
                                    sensState.Alarm_EXT_DEV = 1;
                                }
                                else
                                {
                                    controlBuv.Off();
                                    sensState.Alarm_EXT_DEV = 1;
                                }
                                break;
                            case SET_EXT_DEV_TIME:
                                memcpy(&timerBuv.time, data + 1, 4);
                                timerBuv.time = timerBuv.time / 4;
                                break;
                            case GET_EXT_DEV_TIME:
                                temp = timerBuv;
                                temp.time *= 4;
                                memcpy(mas + 1, temp.timemass, 4);
                                Writter.writeCmd(*Ud, UCMD_SENDDEFF, InterfaceIdSensor, InterfaceIdModem, mas, 5);
                                break;
                            case EXT_DEV_PARAM:
                                switch (data[1])
                                {
                                case GET_EXT_DEV_PARAM:
                                    Writter.writeCmd(*Ud, UCMD_SENDDEFF, InterfaceIdSensor, InterfaceIdModem, mas1, 6);
                                    break;
                                case SET_EXT_DEV_PARAM:
                                    autoBuv = data[2];
                                    break;
                                }
                                break;
                            case GET_SENS_STATE:
                                Writter.writeCmd(*Ud, UCMD_SENDDEFF, InterfaceIdSensor, InterfaceIdModem, (uint8_t *)&sensState, 1);
                                break;
                            case SELF_TEST:
                                if ((sizeData = Packet.getData(data, 128)))
                                {
                                    uint8_t *self_cmd_test = nullptr;

                                    switch (data[1])
                                    {
                                    case VIBRO_ON:
                                        getExtDevCtrl()->On();
                                        *self_cmd_test = VIBRO_ON;
                                        break;
                                    case VIBRO_OFF:
                                        *self_cmd_test = VIBRO_OFF;
                                        getExtDevCtrl()->Off();
                                        break;
                                    case SHORT_SHUNT:
                                        *self_cmd_test = SHORT_SHUNT;
                                        break;
                                    case OPEN_SHUNT:
                                        *self_cmd_test = OPEN_SHUNT;
                                        break;
                                    case GET_CURRENT:
                                        *self_cmd_test = GET_CURRENT;
                                        break;
                                    case CHECK_ALL:
                                        Writter.writeCmd(*Ud, TEST_RESULT_NOTIFY, InterfaceIdSensor, InterfaceIdModem, CheckAll, 2);
                                        break;
                                    }
                                    if (self_cmd_test != nullptr)
                                    {
                                        if ((*self_cmd_test == SHORT_SHUNT) || (*self_cmd_test == OPEN_SHUNT) || (*self_cmd_test == GET_CURRENT))
                                        {
                                            Packet.HeaderPacket.fields.Request = 1;
                                            Writter.writeCmd(*Ud, Packet.HeaderPacket.Data[1], InterfaceIdSensor, (InterfaceId)Packet.HeaderPacket.fields.Source, self_cmd_test, 1);
                                        }
                                        else if ((*self_cmd_test = VIBRO_OFF) || (*self_cmd_test = VIBRO_ON))
                                        {
                                            Packet.HeaderPacket.Data[1] = 0x2A;
                                            Packet.HeaderPacket.fields.Request = 1;
                                            Writter.writeCmd(*Ud, Packet.HeaderPacket.Data[1], InterfaceIdSensor, (InterfaceId)Packet.HeaderPacket.fields.Source, self_cmd_test, 1);
                                        }
                                    }
                                }
                                break;
                            }
                        }
                        break;
                    };
                }
            }
            else
            {
                // проброс пакета, если пришло не сенсору
                unsigned char data[128] = {0};
                uint8_t sizeData = 0;
                switch (cmd)
                {
                case SET_BAUD_RATE:
                    memcpy(&_currUartKskSpeed, data + 1, sizeof(long));

                    break;
                }
            }
            Packet.free();
            // Writter.writeCmd(*Ud, UCMD_DO, InterfaceIdSensor, InterfaceIdModem, stndby, 2);
        }
        // if (testAlarm % 20 == 0 && sensConf.Ena_Break_Tails == 1)
        // {
        //     uint8_t alarm[2] = {SENS_STATE_NOTIFY, *(uint8_t *)&sensState};
        //     sensState.Alarm_Teil_1 = 1;
        //     Writter.writeCmd(*Ud, UCMD_SENDDEFF, InterfaceIdSensor, InterfaceIdModem, alarm, 2);
        //     testAlarm++;
        // }
        // testAlarm++;
        return false;
    };

public:
    void UartH_test()
    {
        uint8_t datatest1[11] = {0x21, 0x08, 0x06, 0xb8, 0x0c, 0x0c, 0x00, 0xa1, 0x10, 0x01, 0x82};
        uint8_t datatest[11] = {0x21, 0x08, 0x06, 0xb8, 0x0c, 0x0c, 0x00, 0xa1, 0x0e, 0x02, 0x81};
        for (int i = 0; i < 11; i++)
        {
            Packet.add(datatest[i]);
        }
        for (int i = 0; i < 11; i++)
        {
            Packet.add(datatest1[i]);
        }
    }
    UartH()
    {
        uint8_t type = SENSOR_RETR;
        memcpy(&sensConf, &type, 1);
        timerBuv.time = 5;
        Hal::Board::getTaskManager()->addTask(&getStatus, 5000);
        tail0->enableInput(&tail0Handler);
        tail1->enableInput(&tail1Handler);
        uint8_t tailsState[2] = {SENS_STATE_NOTIFY, *(uint8_t *)&sensState};
        Writter.writeCmd(*Ud, UCMD_SENDDEFF, InterfaceIdSensor, InterfaceIdModem, tailsState, 2);
    }

private:
    PacketParser Packet;
    Buv controlBuv = Buv::Instance();
    timerOffBUV timerBuv;
    CSensConf sensConf;
    uint8_t testAlarm = 0;
    // Pin *tail_1 =
    bool autoBuv = false;
    LinkInfo sensLink;
    // unsigned char dat;
    // bool sensLinkStatus = false;
    sensStatus getStatus;
    Pin *tail0 = getTail_0();
    Pin *tail1 = getTail_1();
    CheckStatusTails<0> tail0Handler;
    CheckStatusTails<1> tail0Handler;
};

#endif
Leave a Comment