Exemplo n.º 1
0
/**
 * Function for performing MAC related tasks
 *
 * @preCondition    Must be called every couple of ms
 */
void MACTask(void)
{
    #if (DEBUG_MAC >= LOG_WARN)
    BYTE tmpSum;
    char buf[5];
    #endif

    #if defined(MAC_CNTR1_3)
    cntr0.Val += NICGet(CNTR0);
    cntr1.Val += NICGet(CNTR1);
    cntr2.Val += NICGet(CNTR2);
    #endif

    #if (DEBUG_MAC >= LOG_WARN)
    tmpSum = LSB(cntr0) + MSB(cntr0) + LSB(cntr1) + MSB(cntr1) + LSB(cntr2) + MSB(cntr2);
    //If any of the error counter have changed, print them out!
    if (cntrSum != tmpSum) {
        debugPutMsg(1);     //@mxd:1:Frame Alignment=%s, CRC=%s, Missed Packets=%s

        itoa(cntr0.Val, buf);
        debugPutString(buf);
        itoa(cntr1.Val, buf);
        debugPutString(buf);
        itoa(cntr2.Val, buf);
        debugPutString(buf);

        cntrSum = tmpSum;
    }
    #endif
}
Exemplo n.º 2
0
BOOL FTPVerify(char *login, char *password)
{
    #if (DEBUG_MAIN >= LOG_INFO)
        debugPutMsg(4); //@mxd:4:Received FTP Login (%s) and Password (%s)
        debugPutString(login);
        debugPutString(password);
    #endif

    if (strcmpee2ram(login, APPCFG_USERNAME0) == 0) {
        if (strcmpee2ram(password, APPCFG_PASSWORD0) == 0) {
            return TRUE;
        }
    }
    return FALSE;
}
Exemplo n.º 3
0
/**
 * This function is a "callback" from HTTPServer task. Whenever a remote node performs interactive
 * GET task on page that was served, HTTPServer calls this functions. Use HTTPGetParam()
 * to get all name-value parameters.
 *
 * @param httpInfo  Socket that is currently receiving this HTTP command
 * @param rqstRes   Name of the Requested resource - GET command's action. All characters are
 *                  in uppercase!
 */
void HTTPExecGetCmd(HTTP_INFO* httpInfo, BYTE* rqstRes) {
    BYTE param[64];
    WORD wUpdate;   //Contains CMD_UD_XXX flags indicating what has to be updated

    //Used as input AND output parameter for HTTPGetParams.
    // - Input parameter indicates size of param buffer
    // - On return of HTTPGerParam() valueIdx will contain index of value string in param
    BYTE valueIdx;
    BYTE moreParams;
    BYTE paramCount;    //The number of parameters (name-value pairs) we have received
    BYTE user;
    BYTE tmp;
    BYTE executed;      //Indicates if the command has been executed already or not

    #if (DEBUG_HTTPEXEC >= LOG_DEBUG)
    debugPutMsg(1);     //@mxd:1:HTTPExecGetCmd() called for file %s
    debugPutString(rqstRes);
    #endif

    wUpdate = 0;    //Clear all update flags
    paramCount = 0;
    
    //Get the current user logged in for this HTTP connection
    user = HTTPGetCurrentUser(httpInfo);

    //Get next name-value parameter
    do {
        valueIdx = (BYTE)sizeof(param);   //Input parameter is size of param buffer

        //Get name-value parameters. Returns true if there are more name-value parameters to follow
        //- Pointer to Name parameter = &param[0]
        //- Pointer to Value parameter = &param[valueIdx]
        moreParams = HTTPGetParam(httpInfo->socket, param, &valueIdx);
        paramCount++;

        executed = 0;   //Current name-value command has not been executed yet

        /////////////////////////////////////////////////
        //We received a general command with value
        if (param[0] == CMDGROUP_GENERAL)
        {
            #if (DEBUG_CMD >= LOG_DEBUG)
            debugPutMsg(8);     //@mxd:8:Received General Command
            #endif

            //The second character of the name part of the name-value pair will be the "Command Code".
            switch(param[1]) {
                case CMDCODE_GEN_PASSWORD:
                    executed = 1;   //Indicates that the command has been executed
            
                    // This command is NOT used any more, seeing that we now use HTTP Authentication
                    break;
                case CMDCODE_GEN_USERNAME:
                    executed = 1;   //Indicates that the command has been executed

                    // This command is NOT used any more, seeing that we now use HTTP Authentication
                    break;
            }
        }
        /////////////////////////////////////////////////
        //We received a general command without value
        else if (param[0] == CMDGROUP_GENERAL_NOVAL)
        {
            #if (DEBUG_CMD >= LOG_DEBUG)
            debugPutMsg(10);     //@mxd:10:Received General Command without value
            #endif

            /////////////////////////////////////////////////
            // The following commands can ONLY be executed by "Admin" or "Super User"
            if (user != USER_GUEST) {
                //The first character of the value part of the name-value pair will be the "Command Code".
                switch(param[valueIdx]) {

                    //There is no logout command anymore with HTTP Authentication!
                    case CMDCODE_GENNOVAL_LOGOUT:
                        executed = 1;   //Indicates that the command has been executed
                        
                        #if (DEBUG_CMD >= LOG_INFO)
                        debugPutMsg(14);     //@mxd:14:Received Log Off command
                        #endif

                        //There is no logout command anymore with HTTP Authentication!
                        //Update HTTP_INFO structure's user information
                        //httpInfo->flags.bits.bUserLoggedIn = FALSE;
                        
                        break;
                }
            }

            //The first character of the value part of the name-value pair will be the "Command Code".
            switch(param[valueIdx]) {
                //Causes requested to log in, if not already Authenticated
                case CMDCODE_GENNOVAL_LOGIN:
                    executed = 1;   //Indicates that the command has been executed
                    
                    httpInfo->flags.bits.bLoginReq = TRUE;
                    break;
            }
        }
        
        //If this command has not been executed yet, pass it on to the execNaveValueCmd function
        if ( !executed) {
            //Execute the given name-value command
            wUpdate |= execNameValueCmd(param, &param[valueIdx], user);
        }
    } while (moreParams);


    //Network settings have changed
    if (wUpdate & CMD_UD_NETWORK) {
        //Reconfigure USART with new value
        //appcfgNetwork();
    }
    else if (wUpdate & CMD_UD_USART) {
        //Reconfigure USART with new value
        appcfgUSART();
        appcfgUSART2();
    }
    else if (wUpdate & CMD_UD_CPU_IO) {
        //Reconfigure IO ports
        appcfgCpuIO();
    }
    else if (wUpdate & CMD_UD_ADC) {
        //Reconfigure IO ports
        appcfgADC();
    }
    else if (wUpdate & CMD_UD_PWM) {
        //Reconfigure PWM
        appcfgPWM();
    }
    else if (wUpdate & CMD_UD_XBOARD) {
        //Reconfigure Expansion Board
        appcfgXboard();
    }
    else if (wUpdate & CMD_UD_BUSBUF) {
        //Reconfigure Bus Buffers
        busInfoInit();
    }
    else if (wUpdate & CMD_UD_BUSNET) {
        //Reconfigure UDP Buses
        busNetInit();
    }
}
/* ----------------------------------------------------------------------------*
 *
 *  Function Name : readGesture(void)
 *
 *  Description  :Processes a gesture event and returns best guessed gesture
 *
 *  Input : None
 *
 *  Output : None
 *
 *  Return : Number corresponding to gesture. -1 on error.
 * ----------------------------------------------------------------------------*
 * Authors: Sarath S
 * Date: May 17, 2017
 * ---------------------------------------------------------------------------*/
int readGesture(void)
{
    uint8_t fifo_level = 0;
    int bytes_read = 0;
    uint8_t fifo_data[128];
    uint8_t gstatus;
    int motion;
    int i;
    
    /* Make sure that power and gesture is on and data is valid */
    if( !isGestureAvailable() || !(getMode() & 0x41) ) {
        return DIR_NONE;
    }
    
    /* Keep looping as long as gesture data is valid */
    while(1) {
    
        /* Wait some time to collect next batch of FIFO data */
        delayms(FIFO_PAUSE_TIME);
        
        /* Get the contents of the STATUS register. Is data still valid? */
        if( !i2c1_read(APDS9960_GSTATUS, &gstatus,1) ) {
            return ERROR;
        }
        
        /* If we have valid data, read in FIFO */
        if( (gstatus & APDS9960_GVALID) == APDS9960_GVALID ) {
        
            /* Read the current FIFO level */
            if( !i2c1_read(APDS9960_GFLVL, &fifo_level,1) ) {
                return ERROR;
            }

#if DEBUGPRINT
            debugPutString("FIFO Level: ");
            debugPutChar(fifo_level);
#endif

            /* If there's stuff in the FIFO, read it into our data block */
            if( fifo_level > 0) {
                bytes_read = i2c1_read(APDS9960_GFIFO_U, 
                                                fifo_data, 
                                                (fifo_level * 4) );
                if( bytes_read == -1 ) {
                    return ERROR;
                }
#if DEBUGPRINT
                debugPutString("FIFO Dump: ");
                for ( i = 0; i < bytes_read; i++ ) {
                    debugPutChar(fifo_data[i]);
                    debugPutString(" ");
                }
                debugPutString("\r\n");
#endif

                /* If at least 1 set of data, sort the data into U/D/L/R */
                if( bytes_read >= 4 ) {
                    for( i = 0; i < bytes_read; i += 4 ) {
                        gesture_data_.u_data[gesture_data_.index] = \
                                                            fifo_data[i + 0];
                        gesture_data_.d_data[gesture_data_.index] = \
                                                            fifo_data[i + 1];
                        gesture_data_.l_data[gesture_data_.index] = \
                                                            fifo_data[i + 2];
                        gesture_data_.r_data[gesture_data_.index] = \
                                                            fifo_data[i + 3];
                        gesture_data_.index++;
                        gesture_data_.total_gestures++;
                    }
                    
#if DEBUGPRINT
                debugPutString("Up Data: ");
                for ( i = 0; i < gesture_data_.total_gestures; i++ ) {
                    debugPutChar(gesture_data_.u_data[i]);
                    debugPutString(" ");
                }
                debugPutString("\r\n");
#endif

                    /* Filter and process gesture data. Decode near/far state */
                    if( processGestureData() ) {
                        if( decodeGesture() ) {
                            //***TODO: U-Turn Gestures
#if DEBUGPRINT
                           debugPutChar(gesture_motion_);
#endif
                        }
                    }
                    
                    /* Reset data */
                    gesture_data_.index = 0;
                    gesture_data_.total_gestures = 0;
                }
            }
        } else {
    
            /* Determine best guessed gesture and clean up */
            delayms(FIFO_PAUSE_TIME);
            decodeGesture();
            motion = gesture_motion_;
#if DEBUGPRINT
            debugPutString("END: ");
            debugPutChar(gesture_motion_);
#endif
            resetGestureParameters();
            return motion;
        }
    }
}
/* ----------------------------------------------------------------------------*
 *
 *  Function Name : apds9960init
 *
 *  Description  : Configures I2C communications and initializes registers to defaults
 *
 *  Input : None
 *
 *  Output : None
 *
 *  Return : True if initialized successfully. False otherwise
 * ----------------------------------------------------------------------------*
 * Authors: Sarath S
 * Date: May 17, 2017
 * ---------------------------------------------------------------------------*/
int apds9960init(void)
{
    uint8_t id;
    //ledSetLeftLed(LED_ON);  

    /* Initialize I2C */
    I2C1_init();
    delayms(700);
    /* Read ID register and check against known values for APDS-9960 */
    if( !i2c1_read(APDS9960_ID, &id,1) ) {
       ledSetRightLed(LED_ON);  
      return false;
        
    }
    ledSetLeftLed(LED_ON);  
    if( !(id == APDS9960_ID_1 || id == APDS9960_ID_2) ) {
        return false;
    }
     
    /* Set ENABLE register to 0 (disable all features) */
    if( !setMode(ALL, OFF) ) {
        return false;
    }
    
    /* Set default values for ambient light and proximity registers */
    if( !i2c1_write(APDS9960_ATIME, DEFAULT_ATIME) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_WTIME, DEFAULT_WTIME) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_PPULSE, DEFAULT_PROX_PPULSE) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_POFFSET_UR, DEFAULT_POFFSET_UR) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_POFFSET_DL, DEFAULT_POFFSET_DL) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_CONFIG1, DEFAULT_CONFIG1) ) {
        return false;
    }
    if( !setLEDDrive(DEFAULT_LDRIVE) ) {
        return false;
    }
    if( !setProximityGain(DEFAULT_PGAIN) ) {
        return false;
    }
    if( !setAmbientLightGain(DEFAULT_AGAIN) ) {
        return false;
    }
    if( !setProxIntLowThresh(DEFAULT_PILT) ) {
        return false;
    }
    if( !setProxIntHighThresh(DEFAULT_PIHT) ) {
        return false;
    }
    if( !setLightIntLowThreshold(DEFAULT_AILT) ) {
        return false;
    }
    if( !setLightIntHighThreshold(DEFAULT_AIHT) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_PERS, DEFAULT_PERS) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_CONFIG2, DEFAULT_CONFIG2) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_CONFIG3, DEFAULT_CONFIG3) ) {
        return false;
    }
    
    /* Set default values for gesture sense registers */
    if( !setGestureEnterThresh(DEFAULT_GPENTH) ) {
        return false;
    }
    if( !setGestureExitThresh(DEFAULT_GEXTH) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_GCONF1, DEFAULT_GCONF1) ) {
        return false;
    }
    if( !setGestureGain(DEFAULT_GGAIN) ) {
        return false;
    }
    if( !setGestureLEDDrive(DEFAULT_GLDRIVE) ) {
        return false;
    }
    if( !setGestureWaitTime(DEFAULT_GWTIME) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_GOFFSET_U, DEFAULT_GOFFSET) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_GOFFSET_D, DEFAULT_GOFFSET) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_GOFFSET_L, DEFAULT_GOFFSET) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_GOFFSET_R, DEFAULT_GOFFSET) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_GPULSE, DEFAULT_GPULSE) ) {
        return false;
    }
    if( !i2c1_write(APDS9960_GCONF3, DEFAULT_GCONF3) ) {
        return false;
    }
    if( !setGestureIntEnable(DEFAULT_GIEN) ) {
        return false;
    }
    
#if 0
    /* Gesture config register dump */
    uint8_t reg;
    uint8_t val;
  
    for(reg = 0x80; reg <= 0xAF; reg++) {
        if( (reg != 0x82) && \
            (reg != 0x8A) && \
            (reg != 0x91) && \
            (reg != 0xA8) && \
            (reg != 0xAC) && \
            (reg != 0xAD) )
        {
            i2c1_read(reg, val,1);
            //debugPutChar(reg);
            debugPutString(": 0x");
            //debugPutChar(val);

        }
    }

    for(reg = 0xE4; reg <= 0xE7; reg++) {
        i2c1_read(reg, val,1);
        //debugPutChar(reg);
        debugPutString(": 0x");
        //debugPutChar(val);
    }
#endif

    return true;
}/* End of this function */
/* ----------------------------------------------------------------------------*
 *
 *  Function Name : processGestureData(void)
 *
 *  Description  :Processes the raw gesture data to determine swipe direction
 *
 *  Input : None
 *
 *  Output : None
 *
 *  Return : True if near or far state seen. False otherwise.
 * ----------------------------------------------------------------------------*
 * Authors: Sarath S
 * Date: May 17, 2017
 * ---------------------------------------------------------------------------*/
int processGestureData(void)
{
    uint8_t u_first = 0;
    uint8_t d_first = 0;
    uint8_t l_first = 0;
    uint8_t r_first = 0;
    uint8_t u_last = 0;
    uint8_t d_last = 0;
    uint8_t l_last = 0;
    uint8_t r_last = 0;
    int ud_ratio_first;
    int lr_ratio_first;
    int ud_ratio_last;
    int lr_ratio_last;
    int ud_delta;
    int lr_delta;
    int i;

    /* If we have less than 4 total gestures, that's not enough */
    if( gesture_data_.total_gestures <= 4 ) {
        return false;
    }
    
    /* Check to make sure our data isn't out of bounds */
    if( (gesture_data_.total_gestures <= 32) && \
        (gesture_data_.total_gestures > 0) ) {
        
        /* Find the first value in U/D/L/R above the threshold */
        for( i = 0; i < gesture_data_.total_gestures; i++ ) {
            if( (gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) &&
                (gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) &&
                (gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) &&
                (gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) ) {
                
                u_first = gesture_data_.u_data[i];
                d_first = gesture_data_.d_data[i];
                l_first = gesture_data_.l_data[i];
                r_first = gesture_data_.r_data[i];
                break;
            }
        }
        
        /* If one of the _first values is 0, then there is no good data */
        if( (u_first == 0) || (d_first == 0) || \
            (l_first == 0) || (r_first == 0) ) {
            
            return false;
        }
        /* Find the last value in U/D/L/R above the threshold */
        for( i = gesture_data_.total_gestures - 1; i >= 0; i-- ) {
#if DEBUGPRINT
            debugPutString("Finding last: ");
            debugPutString("U:");
            debugPutChar(gesture_data_.u_data[i]);
            debugPutString(" D:");
            debugPutChar(gesture_data_.d_data[i]);
            debugPutString(" L:");
            debugPutChar(gesture_data_.l_data[i]);
            debugPutString(" R:");
            debugPutChar(gesture_data_.r_data[i]);
            debugPutString("\r\n");
#endif
            if( (gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) &&
                (gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) &&
                (gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) &&
                (gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) ) {
                
                u_last = gesture_data_.u_data[i];
                d_last = gesture_data_.d_data[i];
                l_last = gesture_data_.l_data[i];
                r_last = gesture_data_.r_data[i];
                break;
            }
        }
    }
    
    /* Calculate the first vs. last ratio of up/down and left/right */
    ud_ratio_first = ((u_first - d_first) * 100) / (u_first + d_first);
    lr_ratio_first = ((l_first - r_first) * 100) / (l_first + r_first);
    ud_ratio_last = ((u_last - d_last) * 100) / (u_last + d_last);
    lr_ratio_last = ((l_last - r_last) * 100) / (l_last + r_last);
       
#if DEBUGPRINT
    debugPutString("Last Values: ");
    debugPutString("U:");
    debugPutChar(u_last);
    debugPutString(" D:");
    debugPutChar(d_last);
    debugPutString(" L:");
    debugPutChar(l_last);
    debugPutString(" R:");
    debugPutChar(r_last);
    debugPutString("\r\n");

    debugPutString("Ratios: ");
    debugPutString("UD Fi: ");
    debugPutChar(ud_ratio_first);
    debugPutString(" UD La: ");
    debugPutChar(ud_ratio_last);
    debugPutString(" LR Fi: ");
    debugPutChar(lr_ratio_first);
    debugPutString(" LR La: ");
    debugPutChar(lr_ratio_last);
    debugPutString("\r\n");

#endif
       
    /* Determine the difference between the first and last ratios */
    ud_delta = ud_ratio_last - ud_ratio_first;
    lr_delta = lr_ratio_last - lr_ratio_first;
    
#if DEBUGPRINT
    debugPutString("Deltas: ");
    debugPutString("UD: ");
    debugPutChar(ud_delta);
    debugPutString(" LR: ");
    debugPutChar(lr_delta);
    debugPutString("\r\n");
#endif

    /* Accumulate the UD and LR delta values */
    gesture_ud_delta_ += ud_delta;
    gesture_lr_delta_ += lr_delta;
    
#if DEBUGPRINT
    debugPutString("Accumulations: ");
    debugPutString("UD: ");
    debugPutChar(gesture_ud_delta_);
    debugPutString(" LR: ");
    debugPutChar(gesture_lr_delta_);
    debugPutString("\r\n");
#endif
    
    /* Determine U/D gesture */
    if( gesture_ud_delta_ >= GESTURE_SENSITIVITY_1 ) {
        gesture_ud_count_ = 1;
    } else if( gesture_ud_delta_ <= -GESTURE_SENSITIVITY_1 ) {
        gesture_ud_count_ = -1;
    } else {
        gesture_ud_count_ = 0;
    }
    
    /* Determine L/R gesture */
    if( gesture_lr_delta_ >= GESTURE_SENSITIVITY_1 ) {
        gesture_lr_count_ = 1;
    } else if( gesture_lr_delta_ <= -GESTURE_SENSITIVITY_1 ) {
        gesture_lr_count_ = -1;
    } else {
        gesture_lr_count_ = 0;
    }
    
    /* Determine Near/Far gesture */
    if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == 0) ) {
        if( (abs(ud_delta) < GESTURE_SENSITIVITY_2) && \
            (abs(lr_delta) < GESTURE_SENSITIVITY_2) ) {
            
            if( (ud_delta == 0) && (lr_delta == 0) ) {
                gesture_near_count_++;
            } else if( (ud_delta != 0) || (lr_delta != 0) ) {
                gesture_far_count_++;
            }
            
            if( (gesture_near_count_ >= 10) && (gesture_far_count_ >= 2) ) {
                if( (ud_delta == 0) && (lr_delta == 0) ) {
                    gesture_state_ = NEAR_STATE;
                } else if( (ud_delta != 0) && (lr_delta != 0) ) {
                    gesture_state_ = FAR_STATE;
                }
                return true;
            }
        }
    } else {
        if( (abs(ud_delta) < GESTURE_SENSITIVITY_2) && \
            (abs(lr_delta) < GESTURE_SENSITIVITY_2) ) {
                
            if( (ud_delta == 0) && (lr_delta == 0) ) {
                gesture_near_count_++;
            }
            
            if( gesture_near_count_ >= 10 ) {
                gesture_ud_count_ = 0;
                gesture_lr_count_ = 0;
                gesture_ud_delta_ = 0;
                gesture_lr_delta_ = 0;
            }
        }
    }
    
#if DEBUGPRINT
    debugPutString("UD_CT: ");
    debugPutChar(gesture_ud_count_);
    debugPutString(" LR_CT: ");
    debugPutChar(gesture_lr_count_);
    debugPutString(" NEAR_CT: ");
    debugPutChar(gesture_near_count_);
    debugPutString(" FAR_CT: ");
    debugPutChar(gesture_far_count_);
    debugPutString("----------");
#endif
    
    return false;
}/* End of this function */