//***************************************************************************** // //! \brief Prints a line. //! //! \param pcMsg is the message to print //! //! \details Prints a line. //! //! \return None. // //***************************************************************************** static void PrintLine( char *pcMsg ) { Print(pcMsg); IOPut('\r'); IOPut('\n'); }
/**************************************************************************** FUNCTION HTTP_IO_RESULT HTTPExecuteGet(void) This function processes every GET request from the pages. In the example, it processes only the leds.cgi function, but you can add code to process other GET requests. *****************************************************************************/ HTTP_IO_RESULT HTTPExecuteGet(void) { BYTE *ptr; BYTE filename[20]; // STEP #1: // The function MPFSGetFilename retrieves the name of the requested cgi, // in this case "leds.cgi" and puts it inside the filename variable. // Make sure BYTE filename[] above is large enough for your longest name MPFSGetFilename(curHTTP.file, filename, 20); // STEP #2: // Handling of the cgi requests, in this case we have only "leds.cgi" but // it would be possible to have any other cgi request, depending on the webpage if(!memcmp(filename, "leds.cgi", 8)) // Is the requested file name "leds.cgi"? { // STEP #3: // The complete request is contained inside the system variable curHTTP.data. // Using the function HTTPGetArg is possible to read the arguments // of the cgi request from curHTTP.data. In this case we are reading the // argument "led" from the request "leds.cgi?led=x" and we assign it to ptr. ptr = HTTPGetArg(curHTTP.data, (BYTE *)"led"); // The requested led is toggled switch(*ptr) { case '0': IOPut(p4, toggle); REGTOREAD = CONFIG; break; case '1': IOPut(p6, toggle); REGTOREAD = AWATT; break; case '2': IOPut(p17, toggle); break; case '3': IOPut(p19, toggle); break; case '4': IOPut(p21, toggle); break; } } return HTTP_IO_DONE; }
/** * Function to stop a SPI transaction on the given device. * This is used to set the SlaveSelect to idle after a transaction occurred thus releasing the bus. * \param pointer - SPIContext pointer * \return none */ void SPIStop(SPIContext * obj) { vTaskSuspendAll(); #ifndef SPI2_FLASH #ifdef SPI_DBG uartwrite(1,"[SPI_DBG] SPI stop, checking SS\n"); #endif if(obj->ss_pin != SPI_OPT_NO_SS) { #ifdef SPI_DBG uartwrite(1,"[SPI_DBG] Pulling SS up\n"); #endif if(SPI2STATbits.SPITBF) Delay10us(obj->delay); IOPut(obj->ss_pin,on); } #else if(SPI2STATbits.SPITBF) Delay10us(obj->delay); LATDbits.LATD6=1; #endif xTaskResumeAll(); }
//***************************************************************************** // //! \brief Prints a line without final end-of-line. //! //! \param pcMsg is the message to print //! //! \details Prints a line without final end-of-line. //! //! \return None. // //***************************************************************************** static void Print(char *pcMsg ) { while (*pcMsg != '\0') { IOPut(*pcMsg++); } }
void OnRegistration(BYTE Status) { #if defined(STACK_USE_UART) UARTWrite(1,"Event: On Registration\r\n"); switch(Status) { case 0: UARTWrite(1, "Not registered\r\n"); IOPut(p21, off); break; case 1: UARTWrite(1, "Registered on Network\r\n"); IOPut(p21, on); break; case 2: UARTWrite(1, "Searching for new operator\r\n"); IOPut(p21, off); break; case 3: UARTWrite(1, "Registration denied\r\n"); IOPut(p21, off); break; case 4: UARTWrite(1, "Unkown registration status\r\n"); IOPut(p21, off); break; case 5: UARTWrite(1, "Roaming\r\n"); IOPut(p21, off); break; } #endif }
void _485Write(const unsigned char* Data, short len) { char loggBuff[256]; sprintf(loggBuff, "\r\n_485Write... going to write %d bytes to port # %d", (int)len, (int)Port_); UARTWrite(1, loggBuff); IOPut((int)DE_485, on); IOPut((int)RE_485, on);// RE is inverted, so, ON value turns it off vTaskDelay(1); short ind = len; do { UARTWriteCh(Port_, Data[ind - len]); }while(--len); vTaskDelay(1); UARTWrite(1, "\r\n_SerialWrite... enabling reciever"); IOPut((int)DE_485, off); IOPut((int)RE_485, off); }
/** * Function to start a SPI transaction on the given device. * This is used to set the SlaveSelect to active before clocking out data. * \param pointer - SPIContext pointer * \return none */ void SPIStart(SPIContext * obj) { #ifndef SPI2_FLASH #ifdef SPI_DBG uartwrite(1,"[SPI_DBG] SPI start, checking SS\n"); #endif if(obj->ss_pin != SPI_OPT_NO_SS) { #ifdef SPI_DBG uartwrite(1,"[SPI_DBG] Pulling SS down\n"); #endif IOPut(obj->ss_pin,off); } #else LATDbits.LATD6=0; #endif }
long FTPStreamRead(char dest[], int len, BYTE timeout) { long count = 0; int toRead; DWORD tick1, tick2; if ( (FTPisConn(dataSocket)) && (streamStat == FTP_STREAM_READING) ) { tick1 = TickGetDiv64K(); while (count < len) { toRead = FTPRxLen(dataSocket); // Resizing bytes to read according to buffer size if (toRead > (len - count)) toRead = len - count; FTPRead(dataSocket, &dest[count], toRead); count += toRead; streamRBytes += toRead; // No data to read, checking timeout and chars read if (toRead == 0) { // check on file ending, if file is not finished, checking timeout if (streamRBytes == streamLen) { debug("FTPStreamRead: EOF reached\n"); streamStat = FTP_STREAM_EOF; return count; } tick2 = TickGetDiv64K(); IOPut(o4, toggle); if ( (tick2 -tick1) > timeout) { // Timeout occured during reading, a check on data connection is required if (!FTPisConn(dataSocket)) { debug("FTPStreamRead: timeout by server disconnection\n"); errHandling(&dataSocket); streamStat = FTP_STREAM_NOT_CONN; return count; } else { debug("FTPStreamRead: timeout\n"); return count; } } } /* else { sprintf(dbg, "count:%d\n", count); UARTWrite(1, dbg); sprintf(dbg, "toRead:%d\n", toRead); UARTWrite(1, dbg); } */ } return count; } else if (!FTPisConn(dataSocket)) return FTP_DATA_NO_CONNECTED; else return FTP_STREAM_INVALID_OP; }
void FlyportTask() { vTaskDelay(100); UARTWrite(1,"LHINGS on GROVE NEST example!\r\n"); // Action & Status vars. char mEventMsg[] = "PIR_Picus: Someone is moving here!"; BOOL LedStatus = FALSE; int mCont = 0; // GROVE board void *board = new(GroveNest); // GROVE devices // Digital Input/outputs void *button = new(Dig_io, IN); // Button/PIR sensor attachToBoard(board, button, DIG1); void *led = new(Dig_io, OUT); // Led attachToBoard(board, led, DIG2); // Connection to Network #if defined (FLYPORT) WFConnect(WF_DEFAULT); while (WFStatus != CONNECTED); #endif #if defined (FLYPORTETH) while(!MACLinked); #endif UARTWrite(1,"Flyport connected!\r\n"); vTaskDelay(200); //Turn on/off onboard leds IOPut(D4Out, on); IOPut(D5Out, off); // ** INITIALIZE LHINGS ** // Init. Lhings engine if(!LhingsBegin(LHINGS_DEVNAME, LHINGS_DEVUUID, LHINGS_USERNAME, LHINGS_APIKEY)) UARTWrite(1,"ERROR: Lhings access vars. out of range\r\n"); // Initialize Led status set(led, OFF); LedStatus = FALSE; // Set ** LHINGS STATUS vars. ** // Set the Value of the Status vars. defined above in Lhings_StatusList[] LhingsStatusWrite("Led status", "off"); while(1) { // ** Lhings LOOP ** LhingsLoop(); // Check pressure of button / motion detected by PIR sensor if((get(button) != 0) && (mCont==0)) { // Event detected: Publish it in Lhings with a Payload message LhingsEventWrite_withPayload("motion", mEventMsg, strlen(mEventMsg), EVT_PAYLOAD); UARTWrite(1, "DIG1 motion detected!\r\n"); vTaskDelay(20); // Initialize detection delay counter for PIR sensor mCont = 0x0025; if(LedStatus){ set(led, OFF); LedStatus = FALSE; // Set ** LHINGS STATUS vars. ** // Set the Value of the Status vars. defined above in Lhings_StatusList[] LhingsStatusWrite("Led status", "off"); } else{ set(led, ON); LedStatus = TRUE; // Set ** LHINGS STATUS vars. ** // Set the Value of the Status vars. defined above in Lhings_StatusList[] LhingsStatusWrite("Led status", "on"); } } // Process PIR sensor delay if(mCont != 0){ vTaskDelay(5); mCont--; } // Check ** LHINGS ACTION ** reception of "switch" action if(LhingsActionAvailable("switch")){ UARTWrite(1,"Action received: switch\r\n"); if(LedStatus){ set(led, OFF); LedStatus = FALSE; // Set ** LHINGS STATUS vars. ** // Set the Value of the Status vars. defined above in Lhings_StatusList[] LhingsStatusWrite("Led status", "off"); } else{ set(led, ON); LedStatus = TRUE; // Set ** LHINGS STATUS vars. ** // Set the Value of the Status vars. defined above in Lhings_StatusList[] LhingsStatusWrite("Led status", "on"); } } } }
/** * Function to set the SPI module. * \param obj - SPIContext pointer * \param options - SPI options * \param pin - SlaveSelect pin, use SPI_OPT_NO_SS if not necessary * \param speed - desired connection speed. System will automatically calculate the nearest possible speed * \return - 0 the operation is successful * \return - 1 the operation is failed. Check internal error for more details */ BOOL SPIConfig(SPIContext * obj, int options, int pin, long speed) { double tout=0; obj->delay=100; obj->SPICON1=0; obj->SPICON2=0; obj->SPISTAT=0; obj->primary=0; obj->secondary=0; obj->ss_pin=0; obj->SPIIE=0; obj->SPFIE=0; obj->mode16 = FALSE; int index=0; #ifdef SPI_DBG_CFG uartwrite(1,"[SPI_DBG] Initiating configuration\n"); #endif if(speed > _SPI_MAX_SPD) { #ifdef SPI_DBG_CFG uartwrite(1,"[SPI_DBG] Speed too fast during config, quitting.\n"); #endif _intSPIerr = SpdTooFast; return 1; } unsigned int divider = (unsigned int)ceil(16000000.0/(double)speed); #ifdef SPI_DBG_CFG sprintf(spi_dbg_msg,"[SPI_DBG] Calculated divider: %d\n",divider); UARTWrite(1,spi_dbg_msg); #endif index = _spi_find_divider_index(divider); obj->primary = _spi_divider_primary[index]; obj->secondary = _spi_divider_secondary[index]; obj->ss_pin = pin; #ifdef SPI_DBG_CFG sprintf(spi_dbg_msg,"[SPI_DBG] primary %d\n[SPI_DBG] secondary %d\n",_spi_divider_primary_value[obj->primary],_spi_divider_secondary_value[obj->secondary]); uartwrite(1,spi_dbg_msg); #endif tout = ((float)_spi_divider_primary_value[obj->primary] * (float)_spi_divider_secondary_value[obj->secondary])/16; // time (in us) per bit shifted out if (options & SPI_OPT_MODE16) { obj->mode16 = TRUE; obj->SPICON1 |= SPI_MSK_MODE16; obj->delay = ceil(32*tout/10); } else obj->delay = ceil(16*tout/10); if (obj->delay < 1) obj->delay = 1; #ifdef SPI_DBG_CFG sprintf(spi_dbg_msg,"[SPI_DBG] Requested speed %.2f KHz\n[SPI_DBG] Nearest speed %.2f KHz\n",(double)speed/1000,1000/tout); uartwrite(1,spi_dbg_msg); #endif if((options & SPI_OPT_MODE_0) || (!(options & (SPI_OPT_MODE_0 | SPI_OPT_MODE_1 | SPI_OPT_MODE_2 | SPI_OPT_MODE_3)))) { #ifdef SPI_DBG_CFG uartwrite(1,"[SPI_DBG] Selecting MODE 0\n"); #endif obj->SPICON1 |= SPI_MSK_CPHA; //288 // bit5=1 master mode + bit8=1 data changes on clock trailing edge obj->SPICON1 &= ~SPI_MSK_CPOL; } if(options & SPI_OPT_MODE_1) { #ifdef SPI_DBG_CFG uartwrite(1,"[SPI_DBG] Selecting MODE 1\n"); #endif obj->SPICON1 &= ~SPI_MSK_CPHA; obj->SPICON1 &= ~SPI_MSK_CPOL; //32// bit5=1 master mode } if(options & SPI_OPT_MODE_2) { #ifdef SPI_DBG_CFG uartwrite(1,"[SPI_DBG] Selecting MODE 2\n"); #endif obj->SPICON1 |= SPI_MSK_CPHA | SPI_MSK_CPOL; //352 // bit5=1 master mode + bit8=1 data changes on clock trailing edge + bit6=1 clock polarity reversed } if(options & SPI_OPT_MODE_3) { #ifdef SPI_DBG_CFG uartwrite(1,"[SPI_DBG] Selecting MODE 3\n"); #endif obj->SPICON1 |= SPI_MSK_CPOL; obj->SPICON1 &= ~SPI_MSK_CPHA; //96 // bit5=1 master mode + bit6=1 clock polarity } obj->SPICON1 |= obj->primary&0b00000011; obj->SPICON1 |= (obj->secondary&0b00011100)<<2; if(options & SPI_OPT_MASTER) obj->SPICON1 |= SPI_MSK_MASTER; if(options & SPI_OPT_SLAVE) obj->SPICON1 &= ~SPI_MSK_MASTER; //0b1111111111011111; if(options & SPI_OPT_SLAVE_SELECT) obj->SPICON1 |= SPI_MSK_SSEN; if ((options & (SPI_OPT_SLAVE | SPI_OPT_MASTER)) == 0) { #ifdef SPI_DBG_CFG uartwrite(1,"[SPI_DBG] No mode (Master/Slave) selected, quitting\n"); #endif _intSPIerr=NoModeSpecified; obj->SPICON1=0; obj->SPICON2=0; obj->SPISTAT=0; obj->primary=0; obj->secondary=0; obj->ss_pin=0; return 1; } /*#if(GroveNest == Board) obj->SPICON1 |= 0b1000000; #endif*/ obj->SPISTAT = 0x00; obj->SPICON2 = 0x00; obj->SPIIE = 0; obj->SPFIE = 0; obj->IP = 1; obj->FIP = 1; #ifndef SPI2_FLASH if(pin != SPI_OPT_NO_SS) { #ifdef SPI_DBG_CFG uartwrite(1,"[SPI_DBG] SS pin selected, configuring\n"); #endif obj->ss_pin = pin; IOInit(pin,out); vTaskDelay(1); IOPut(pin,on); #ifdef SPI_DBG_CFG sprintf(spi_dbg_msg,"[SPI_DBG] SS us of delay %u\n",obj->delay*10); UARTWrite(1,spi_dbg_msg); #endif } else { #ifdef SPI_DBG_CFG uartwrite(1,"[SPI_DBG] SS pin cleared, not using\n"); #endif obj->ss_pin=SPI_OPT_NO_SS; } #else TRISDbits.TRISD6=0; #endif if(options & SPI_OPT_DI_SAMPLE_END) { #ifdef SPI_DBG_CFG uartwrite(1,"[SPI_DBG] DI sample set to end of DO clock time\n"); #endif obj->SPICON1 |= 0b1000000000; } _intSPIerr=SPI_NO_ERR; return 0; }