Exemplo n.º 1
0
uint16_t adc::read_average (uint8_t channel, uint8_t samples)
{
	uint16_t sum = 0;
	uint8_t count = 0;
	
	//throw away first value
	read_once(channel);
	
	for(count = 0; count<samples && count < MAX_SAMPLES ;count++)
	{   
	    sum += read_once(channel);
	}
	//Average the taken readings. Keep the result in the sum variable.
	return sum/count;
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
	assert(argc == 4);
	int epoll_fd = epoll_create(100);
	start_conn(epoll_fd, atoi(argv[3]), argv[1], atoi(argv[2]));
	epoll_event events[10000];
	char buffer[2048];
	while(1) {
		int fds = epoll_wait(epoll_fd, events, 10000, 2000);
		for(int i = 0; i < fds; i ++) {
			int sockfd = events[i].data.fd;
			if(events[i].events & EPOLLIN) {
				if(!read_once(sockfd, buffer, 2048)) {
					close_conn(epoll_fd, sockfd);
				}
				struct epoll_event event;
				event.events = EPOLLOUT | EPOLLET | EPOLLERR;
				event.data.fd = sockfd;
				epoll_ctl(epoll_fd, EPOLL_CTL_MOD, sockfd, &event);
			} else if(events[i].events & EPOLLOUT) {
				if(!write_nbytes(sockfd, request, strlen(request))) {
					close_conn(epoll_fd, sockfd);
				}
				struct epoll_event event;
				event.events = EPOLLIN |EPOLLET | EPOLLERR;
				event.data.fd = sockfd;
				epoll_ctl(epoll_fd, EPOLL_CTL_MOD, sockfd, &event);
			} else if(events[i].events & EPOLLERR) {
				close_conn(epoll_fd, sockfd);
			}
		}
	}
}
Exemplo n.º 3
0
uint16_t adc::read_oversampled (uint8_t channel, uint8_t samples)
{
	DBG (ptr_to_serial, "All your readings are belong to us" << endl);
	
	sampleavg = 0;
	for(int i = 0; i < samples;i ++)
	{
		sampleavg += read_once(channel);
	}
	sampleavg /= samples;
	
	return (sampleavg);
}
Exemplo n.º 4
0
uint16_t adc::read_median (uint8_t channel, uint8_t num_samples)
{
	uint16_t samples[MAX_SAMPLES] ;
	//uint16_t sorted[MAX_SAMPLES];
	
	if (num_samples > MAX_SAMPLES ) num_samples=MAX_SAMPLES ;
	
	uint8_t i = 0;
	
	//throw away first value
	read_once(channel);
	
	for(i = 0; i < num_samples ;i++)
	{
	    samples[i] = read_once(channel);
	}
	
	qsort( samples, num_samples, sizeof(uint16_t), &compare );
	
	return samples[num_samples/2];
	
}
Exemplo n.º 5
0
uint16_t adc::read_once (uint8_t channel)
{
	uint16_t result = 0;
	static uint8_t oldChannel = 0;
	
	ADMUX &= 0xF8; // by masking the upper 5 bits, clear the selected channel
	ADMUX |= (channel & 0x07); // set new channel, limit to 0-7 by ignoring higher bits

	ADCSRA |= (0x01 << ADSC); // start a conversion
	
	while (ADCSRA & (0x01 << ADSC)) {} // while ADSC is 1, conversion is in progress, so wait.
	if (channel != oldChannel) // if we've changed channels since the last read,
	{
	  oldChannel = channel; // note that this has happened
	  result = read_once(channel); // throw away the current value, take a second sample
	}
	else result = ((uint16_t)(ADCL) | (uint16_t)(ADCH << 8)); // 16 bit number: ADCH:ADCL
	return result;
}
Exemplo n.º 6
0
uint16_t adc::read_oversampled (uint8_t channel, uint8_t samples)
{
        int i;               // tracks samples taken
        uint16_t oldSum = 0; // previous sum of samples
	uint16_t newSum = 0; // current loop's sum of samples
        
        for(i = 0; i <= samples; i++)
	{
           newSum = oldSum + read_once(channel); // add in the latest sample
	   if(newSum < oldSum) // if overflow has occured
	   {
	      newSum = oldSum; // revert to the previous value
	      *ptr_to_serial << "sample overflow caught" << endl;
	      break; // stop taking samples
	   }
	   oldSum = newSum; // update the sum
	}
	//*ptr_to_serial << "sample: " << (newSum / i) << endl;
	return (newSum / i); // return the average of the samples
}
Exemplo n.º 7
0
void Reader::read()
{
    for( ; !m_shutdown && read_once(); ++m_num_points );
    std::cerr << "view-points: end of " << options.filename << "; read " << m_num_points << " record(s)" << std::endl;
    m_shutdown = true;
}
Exemplo n.º 8
0
void CameraReader::read()
{
    while( !m_shutdown && read_once() );
    std::cerr << "view-points: end of camera stream " << options.filename << std::endl;
    m_shutdown = true;
}
Exemplo n.º 9
0
int main(int argc, char* argv[])
{
	signal(SIGPIPE,SIG_IGN);
	signal_exit_handler();

	int background = 0;
	if(background)
	{
		daemonize();
	}
	
	assert(argc == 5);
	int maxclients = atoi(argv[3])  + CONFIG_FDSET_INCR;
	adjustOpenFilesLimit(maxclients);

	int epoll_fd = epoll_create(1024);
	start_conn(epoll_fd,atoi(argv[3]), argv[1],atoi(argv[2]),atoi(argv[4]));
	epoll_event *events = (epoll_event*)malloc(sizeof(struct epoll_event) * (maxclients) );

	char buffer[2048];
	while(!stop)
	{
		int fds = epoll_wait(epoll_fd,events,maxclients,2000);
		for(int i = 0; i < fds; i++)
		{
			int sockfd = events[i].data.fd;
			if(events[i].events & EPOLLIN)
			{
				if(!read_once(sockfd, buffer,2048))
				{
					close_conn(epoll_fd,sockfd);
				}
				struct epoll_event event;
				event.events = EPOLLOUT | EPOLLET | EPOLLERR;
				event.data.fd = sockfd;
				epoll_ctl(epoll_fd,EPOLL_CTL_MOD,sockfd,&event);
			}
			else if(events[i].events & EPOLLOUT)
			{
				if(!write_nbytes(sockfd,request ,strlen(request)))
				{
					close_conn(epoll_fd,sockfd);
				}
				struct epoll_event event;
				event.events = EPOLLIN | EPOLLERR;
				event.data.fd = sockfd;
				epoll_ctl(epoll_fd,EPOLL_CTL_MOD,sockfd,&event);
			}
			else if(events[i].events & EPOLLERR)
			{
				close_conn(epoll_fd,sockfd);
			}
		}
	}

	close(epoll_fd);
	if(events)
	{
		free(events);
	}
	printf("exit!\n");
}
Exemplo n.º 10
0
int cmd_gpio(t_hydra_console *con, t_tokenline_parsed *p)
{
	uint16_t gpio[3] = { 0 };

	int mode, pull, state, port, pin, read, period, continuous, t, max;
	bool mode_changed, pull_changed;
	char *str, *s;

	mode_changed = false;
	pull_changed = false;
	t = 1;
	mode = MODE_CONFIG_DEV_GPIO_IN;
	pull = MODE_CONFIG_DEV_GPIO_NOPULL;
	state = 0;
	period = 100;
	read = FALSE;
	continuous = FALSE;
	while (p->tokens[t]) {
		switch (p->tokens[t]) {
		case T_MODE:
			switch (p->tokens[++t]) {
			case T_IN:
				mode = MODE_CONFIG_DEV_GPIO_IN;
				break;
			case T_OUT:
				mode = MODE_CONFIG_DEV_GPIO_OUT_PUSHPULL;
				break;
			case T_OPEN_DRAIN:
				mode = MODE_CONFIG_DEV_GPIO_OUT_OPENDRAIN;
				break;
			}
			mode_changed = true;
			break;
		case T_PULL:
			switch (p->tokens[++t]) {
			case T_UP:
				pull = MODE_CONFIG_DEV_GPIO_PULLUP;
				break;
			case T_DOWN:
				pull = MODE_CONFIG_DEV_GPIO_PULLDOWN;
				break;
			case T_FLOATING:
				pull = MODE_CONFIG_DEV_GPIO_NOPULL;
				break;
			}
			pull_changed = true;
			break;
		case T_ON:
		case T_OFF:
			if (state) {
				cprintf(con, "Please choose one of 'on' or 'off'.\r\n");
				return FALSE;
			}
			state = p->tokens[t];
			break;
		case T_READ:
			read = TRUE;
			break;
		case T_PERIOD:
			t += 2;
			memcpy(&period, p->buf + p->tokens[t], sizeof(int));
			break;
		case T_CONTINUOUS:
			continuous = TRUE;
			break;
		case T_ARG_STRING:
			str = p->buf + p->tokens[++t];
			if (strlen(str) < 3) {
				cprintf(con, str_pin_error, str);
				return FALSE;
			}
			/* Allow case-insensitive pin names. */
			if (str[0] > 0x60)
				str[0] -= 0x20;
			if (str[1] > 0x60)
				str[1] -= 0x20;
			if (str[0] != 'P') {
				cprintf(con, str_pin_error, str);
				return FALSE;
			}
			if (str[1] < 'A' || str[1] > 'C') {
				cprintf(con, str_pin_error, str);
				return FALSE;
			}
			port = str[1] - 'A';
			if (str[2] == '*') {
				if (port == 1)
					 /* 0 to 11 for port B */
					gpio[port] = 0x0FFF;
				else
					 /* 0 to 15 */
					gpio[port] = 0xFFFF;
			} else {
				pin = strtoul(str + 2, &s, 10);
				if ((*s != 0 && *s != '-') || pin < 0 || pin > 15
				    || (port == 1 && pin > 11)) {
					cprintf(con, str_pin_error, str);
					return FALSE;
				}
				gpio[port] |= 1 << pin;
				if (*s == '-') {
					/* Range */
					max = strtoul(s + 1, &s, 10);
					if (max <= pin || max > 15 ||
							(port == 1 && max > 11)) {
						cprintf(con, str_pin_error, str);
						return FALSE;
					}
					while (pin <= max)
						gpio[port] |= 1 << pin++;
				}
			}
			break;
		}
		t++;
	}

	if (!gpio[0] && !gpio[1] && !gpio[2]) {
		cprintf(con, "Please select at least one GPIO pin.\r\n");
		return FALSE;
	}

	if (!state && !read) {
		cprintf(con, "Please select either 'read' or on/off.\r\n");
		return FALSE;
	}

	if((mode_changed == true) || (pull_changed == true)) {
		for (port = 0; port < 3; port++) {
			for (pin = 0; pin < 16; pin++) {
				if (!(gpio[port] & (1 << pin)))
					continue;
				bsp_gpio_init(ports[port], pin, mode, pull);
			}
		}
	}

	if (!state) {
		if (continuous)
			read_continuous(con, gpio, period);
		else
			read_once(con, gpio);
	} else {
		if (state == T_ON)
			cprintf(con, "Setting ");
		else
			cprintf(con, "Clearing ");
		for (port = 0; port < 3; port++) {
			if (!gpio[port])
				continue;
			for (pin = 0; pin < 16; pin++) {
				if (!(gpio[port] & (1 << pin)))
					continue;
				if (state == T_ON)
					bsp_gpio_set(ports[port], pin);
				else
					bsp_gpio_clr(ports[port], pin);
				cprintf(con, "P%c%d ", port + 'A', pin);
			}
		}
		cprint(con, "\r\n", 2);
	}

	return TRUE;
}