void receiveCommands() { if (usbComRxAvailable() && usbComTxAvailable() >= 64) { uint8 XDATA response[64]; uint8 responseLength; uint8 byte, rand; byte = usbComRxReceiveByte(); // By default, echo back the byte that was send. response[0] = byte; responseLength = 1; // By default, stop blinking the yellow LED because it will // affect the sequence of random numbers reported to the COM port. blinkYellow = 0; switch(byte) { case 'Y': blinkYellow = 1; break; case 's': randomSeedFromSerialNumber(); break; case 'a': randomSeedFromAdc(); break; case '0': randomSeed(0,0); break; case '1': randomSeed(0xFF, 0xFF); break; case '8': randomSeed(0x80, 0x03); break; case 'r': rand = randomNumber(); // Wait for the NEXT random number to finish so we can read RNDH and RNDL. while(ADCCON1 & 0x0C); response[responseLength++] = ','; response[responseLength++] = nibbleToAscii(rand >> 4); response[responseLength++] = nibbleToAscii(rand); response[responseLength++] = ','; response[responseLength++] = (rand & 0x80) ? '1' : '0'; response[responseLength++] = (rand & 0x40) ? '1' : '0'; response[responseLength++] = (rand & 0x20) ? '1' : '0'; response[responseLength++] = (rand & 0x10) ? '1' : '0'; response[responseLength++] = (rand & 0x08) ? '1' : '0'; response[responseLength++] = (rand & 0x04) ? '1' : '0'; response[responseLength++] = (rand & 0x02) ? '1' : '0'; response[responseLength++] = (rand & 0x01) ? '1' : '0'; response[responseLength++] = ','; response[responseLength++] = nibbleToAscii(RNDH >> 4); response[responseLength++] = nibbleToAscii(RNDH); response[responseLength++] = nibbleToAscii(RNDL >> 4); response[responseLength++] = nibbleToAscii(RNDL); response[responseLength++] = '\r'; response[responseLength++] = '\n'; break; default: response[0] = '?'; break; } usbComTxSend(response, responseLength); }
void intToHexa(unsigned long nb) { short i; unsigned char byte; for(i=3 ; i>=0 ; i--) { byte=nb>>(unsigned short)8*i; nb-=byte<<(unsigned short)8*i; serial_putc(nibbleToAscii(byte>>4)); serial_putc(nibbleToAscii(byte&0x0F)); } }
void handleOneWire(void) { int i; int newtemp = 0; int air_temp_c = 0; unsigned int decimals; if (getMs() > ds1820_time + 1000) { air_temp_c = read_DS1820(); newtemp++; start_DS1820(); ds1820_time = getMs(); } if ((newtemp || respondstr) && usbComTxAvailable() >= 64) { const char *cp; uint8 XDATA response[64]; uint8 responseLength = 0; decimals = (air_temp_c & 0xf) * 100; responseLength = sprintf(response, "%d.%02d", air_temp_c / 16, decimals / 16); response[responseLength++] = ','; for (i = 0; i < 8; i++) { response[responseLength++] = nibbleToAscii(DS1820_addr[i] >> 4); response[responseLength++] = nibbleToAscii(DS1820_addr[i]); } response[responseLength++] = ','; i = sizeof(response) - responseLength - 2; if (respondstr) { for (cp = respondstr; *respondstr && i--; respondstr++) { response[responseLength++] = *cp; } } respondstr = NULL; response[responseLength++] = '\r'; response[responseLength++] = '\n'; usbComTxSend(response, responseLength); } }