Ejemplo n.º 1
0
Archivo: main.c Proyecto: nesl/nilmbee
int main(void)
{
    //uint8_t last_is_zero = 1;

    uint8_t i, is_active;
    uint8_t adcv, adcmax, adcmin;
    uint8_t score = 0, score_shifter = 0;

    pin_init();
    adc_init();

    while(1) {
        for (i=0, adcmax=0, adcmin=255; i<100; i++) {
            adc_start();
            while (!(ADCSRA & (1<<ADIF)));
            adcv = ADCL;
            if (adcv > adcmax) adcmax = adcv;
            if (adcv < adcmin) adcmin = adcv;
        }
        rand_counter ++ ;

        tx_word(adcmax - adcmin);
        _delay_ms(400);
    }
}
Ejemplo n.º 2
0
/************************************************************************
 * void udp_send(WORD datalength)
 * This function is called by the application to send a UDP
 * packet. Values from global variables, common memory
 * space and the passed data length are used.
 * Optionally, a pseudo header is constructed and the UDP
 * checksum calculated.
 ***********************************************************************/
void udp_send(WORD datalength, BYTE ephemeral)
{
	WORD cssum;
	
	ipstat.udptx++;
	/* total size of IP datagram is the size of IP header */
	/* plus UDP header, plus UDP data */
	ip.totallen=IPSIZE+UDPHSIZE+datalength;
	/* place IP header and start of trasnmit buffer */
	txpos=0;
	ip_header(PROTUDP);
	/* use an ephemeral source port, this depends on the
	 * application, and must be specified when calling
	 * the function */
	if (ephemeral) tx_word(local_port());
	else tx_word(udph.dest);
	/* destination port, as source port from incoming packet */
	tx_word(udph.src);
	/* UDP length, header length plus data length */
	tx_word(UDPHSIZE+datalength);
	/* UDP checksum */
	tx_word(0x00);
	
	/* UDP checksumming is specified at compile time
	 * if checksumming is disabled, a zero value is sent
	 * otherwise the pseudo header is added and checksummed
	 * but NOT sent */
	#ifdef UPDCHECKSUM
		txpos+=datalength;
		tx_byte(IP1);
		tx_byte(IP2);
		tx_byte(IP3);
		tx_byte(IP4);
		tx_byte(ip.destaddr[0]);
		tx_byte(ip.destaddr[1]);
		tx_byte(ip.destaddr[2]);
		tx_byte(ip.destaddr[3]);
		tx_byte(0x00);
		tx_byte(PROTUDP);
		tx_word(10+udpdatal);
		
		cssum = chksm(&txbuffer[20],8+datalength+12);
		put_checksum(cssum,&txbuffer[IPSIZE+6]);
	#endif
	
	/* call SLIP to send out the packet */
	slip_send(txbuffer,IPSIZE+UDPHSIZE+datalength);
}
Ejemplo n.º 3
0
/************************************************************************
 * void tftp()
 * Simple application layer server to return data
 * The only functionality provided is reading, and only one 'file'.
 * this function doesn't need to know anything about buffer
 * positions, it simply uses functions provided by the IP layer.
 ***********************************************************************/
void tftp()
{
	WORD opcode;
	/* read the operation code */
	copy_rx_word(&opcode);
	/* read request */
	if (opcode == TFTP_READ) {
		/* check the requested filename is 'stats' */
		if (rx_byte() == 's' && rx_byte() == 't' && rx_byte() == 'a' &&
			rx_byte() == 't' && rx_byte() == 's' && rx_byte() == 0x00)
		{
			tx_word(TFTP_DATA);	/* op code */
			tx_word(0x0001);	/* block number */
			q_string("   IP Statistics   \n\n",21);
			q_string("Received:\t",10);
			q_num(ipstat.rxpackets);		
			q_string("\nTransmitted:\t",14);
			q_num(ipstat.txpackets);
			q_string("\nDropped:\t",10);
			q_num(ipstat.dropped);
			q_string("\n\n",2);
		}
		/* requested a different file, not found */
		else {
			tx_word(TFTP_ERR);
			tx_word(0x0001);
			q_string("File Not Found.",15);
			tx_byte(0x00);
		}
		udp_send(txpos-IPSIZE-UDPHSIZE,1);
	}
	/* write request, send error code and message */
	else if (opcode == TFTP_WRITE) {
		tx_word(TFTP_ERR);
		tx_word(0x0002);
		q_string("Access Violation.",17);
		tx_byte(0x00);
		udp_send(txpos-IPSIZE-UDPHSIZE,1);
	}
}