Пример #1
0
int libambit_htob(const char *hex_string, uint8_t *binary, size_t binary_size)
{
    int i = 0;
    uint8_t ch;
    size_t bytes_written = 0;

    if (hex_string[0] == '\0' || strlen(hex_string) % 2 != 0) {
        return -1;
    }

    while (bytes_written < binary_size && *hex_string != '\0') {
        if ((ch = hextob(*(hex_string++))) == 0xff)
            return -1;
        binary[i] = ch << 4;
        if ((ch = hextob(*(hex_string++))) == 0xff)
            return -1;
        binary[i++] |= ch;
    }

    return i;
}
Пример #2
0
/**
 * Must be called every couple of ms.
 */
void evtTask(void) {
    static TICK8 tEvt = 0;
    static BYTE heartbeat = 20;

    //Buffer
    char buf[64];
    BYTE buf2Tx;


    /////////////////////////////////////////////////
    //Events
    switch(smEvt) {
    //UDP Event port has not been initialized yet, still waiting for MAC and IP address of remote host
    case SM_EVT_INIT:
        //Is there any data waiting for us on the "UDP Event port"?
        //Because of the design of the Modtronix TCP/IP stack we have to consume all data sent to us
        //as soon as we detect it. Store all data to a buffer as soon as it is detected
        if (UDPIsGetReady(udpSocketEvt)) {
            UDPGetArray((BYTE *)buf, 2);    //Read first 2 bytes from UDP message, indicates what event ports are active. Bit0=UDP Event Port
            UDPDiscard();           //Not using receive, so discart msg
            if (hextob( (char *)buf, &activeEventPorts) == 0) {
                activeEventPorts = 0;     //If error, disable all event ports
            }

            smEvt = SM_EVT_INIT_MSG;
        }
        break;
    case SM_EVT_INIT_MSG:
        //UDP Event port is active
        if ( ((activeEventPorts & EVT_PORT_UDP)!=0) && UDPIsPutReady(udpSocketEvt) ) {
            strcpypgm2ram(buf, (ROM char*)"l40=1;");
            UDPPutArray((BYTE *)buf, strlen(buf));

            // Now transmit it.
            UDPFlush();
            smEvt = SM_EVT_IDLE;
        }
        break;
    case SM_EVT_IDLE:
        buf2Tx = 0;
        
        //Is there any data waiting for us on the "UDP Event port"?
        //Because of the design of the Modtronix TCP/IP stack we have to consume all data sent to us
        //as soon as we detect it. Store all data to a buffer as soon as it is detected
        if (UDPIsGetReady(udpSocketEvt)) {
            UDPGetArray((BYTE *)buf, 2);    //Read first 2 bytes from UDP message, indicates what event ports are active. Bit0=UDP Event Port
            UDPDiscard();           //Not using receive, so discart msg
            if (hextob( (char *)buf, &activeEventPorts) == 0) {
                activeEventPorts = 0;     //If error, disable all event ports
            }
            break;          //Have to break, so stack task can be called, and UDPDiscart is executed
        }

        //Enter every 50ms
        if ( TickGetDiff8bit(tEvt) >= ((TICK8)TICKS_PER_SECOND / (TICK8)20) )
        {
            tEvt = TickGet8bit();

            //Every second
            if (--heartbeat == 0) {
                heartbeat = 20;
                //UDP Event port is active
                if ( ((activeEventPorts & EVT_PORT_UDP)!=0) && UDPIsPutReady(udpSocketEvt) ) {
                    strcpypgm2ram(buf, (ROM char*)"l40=2;");
                    UDPPutArray((BYTE *)buf, strlen(buf));
                    buf2Tx |= EVT_PORT_UDP;     //Indicate that data was added to UDP event port, and it must be TXed
                }
            }

            //Check LCD display every 50ms
            buf2Tx |= chkLCD();

            //Check Expansion Board every 50ms
            buf2Tx |= chkXboard();
        }

        //Was anything added to UDP event port
        if (buf2Tx & EVT_PORT_UDP) {
            UDPFlush(); //Transmit contents of UDP buffer
        }
        break;
    }
}