示例#1
0
文件: ftp.c 项目: AmesianX/php-src
/* {{{ ftp_nb_continue_read
 */
int
ftp_nb_continue_read(ftpbuf_t *ftp)
{
	databuf_t	*data = NULL;
	char		*ptr;
	int		lastch;
	size_t		rcvd;
	ftptype_t	type;

	data = ftp->data;

	/* check if there is already more data */
	if (!data_available(ftp, data->fd)) {
		return PHP_FTP_MOREDATA;
	}

	type = ftp->type;

	lastch = ftp->lastch;
	if ((rcvd = my_recv(ftp, data->fd, data->buf, FTP_BUFSIZE))) {
		if (rcvd == -1) {
			goto bail;
		}

		if (type == FTPTYPE_ASCII) {
			for (ptr = data->buf; rcvd; rcvd--, ptr++) {
				if (lastch == '\r' && *ptr != '\n') {
					php_stream_putc(ftp->stream, '\r');
				}
				if (*ptr != '\r') {
					php_stream_putc(ftp->stream, *ptr);
				}
				lastch = *ptr;
			}
		} else if (rcvd != php_stream_write(ftp->stream, data->buf, rcvd)) {
			goto bail;
		}

		ftp->lastch = lastch;
		return PHP_FTP_MOREDATA;
	}

	if (type == FTPTYPE_ASCII && lastch == '\r') {
		php_stream_putc(ftp->stream, '\r');
	}

	ftp->data = data = data_close(ftp, data);

	if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250)) {
		goto bail;
	}

	ftp->nb = 0;
	return PHP_FTP_FINISHED;
bail:
	ftp->nb = 0;
	ftp->data = data_close(ftp, data);
	return PHP_FTP_FAILED;
}
示例#2
0
file_prober_c::file_prober_c(main_window_c *parent)
  : m_parent(parent)
  , m_process(parent)
  , m_file(new input_file_c)
  , m_options_file(new QTemporaryFile)
{
  connect(&m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(data_available()));
}
示例#3
0
文件: hardware.cpp 项目: edga/lc3
  operator int16_t() const {
    static unsigned char prev_char;
    // Note: terminal is setup in Hardware class
    if (data_available(fd)) {
      int ret = read(fd,&prev_char, 1);
 //     printf("read(%d): %d = %d\n", fd, ret, prev_char);
    }
    return prev_char;
  }
示例#4
0
void read_all_clients(int in_test) {
	std::vector<int>::iterator it;
	frame_t f;
	for(it = clients.begin(); it<clients.end(); ++it) {
		if(data_available(*it,0,0)) {
			f = read_frame(*it, vars, NULL);
			switch(f.cmd) {
				case NW_CMD_BAI:
					printf("Client closed connection. ");
					send_frame(*it, no_addr, NW_CMD_BAI, vars);
					clients.erase(it);
					if(in_test) {
						test_results[completed_tests++].success = 0;
						printf("%d/%d done\n", completed_tests, active_clients);
					} else {
						printf("\n");
					}
					break;
				case NW_CMD_INVALID:
					printf("Invalid cmd recieved or client disconnected. ");
					clients.erase(it);
					if(in_test) {
						test_results[completed_tests++].success = 0;
						printf("%d/%d done\n", completed_tests, active_clients);
					} else {
						printf("\n");
					}
					break;
				case NW_CMD_ERROR:
					printf("Client responed with error: %s ", vars[0].str);
					if(in_test) {
						test_results[completed_tests++].success = 0;
						printf("%d/%d done\n", completed_tests, active_clients);
					} else {
						printf("\n");
					}
					break;
				if(in_test) {
					case NW_CMD_DOWNLOAD_COMPLETE:
						test_results[completed_tests].success = 1;
						test_results[completed_tests].speed = vars[0].i;
						test_results[completed_tests].filesize = vars[1].i;
						++completed_tests;
						printf("Client %d downloaded %.2fMb in %.2f Mbps, %d/%d done\n", completed_tests, to_mbytes(vars[1].i), to_mbps(vars[0].i), completed_tests, active_clients);
						break;
				}
				default:
					break;
			}
		}
	}
}
示例#5
0
文件: echo.cpp 项目: osipov19/test
void Echo::handle_answer()
{
  char buf[1024];
  if(data_available(5))
  {
    int r = read(fd(), buf, sizeof(buf));
    write(STDOUT_FILENO, buf, r);
  }
  else
  {
    throw std::runtime_error("Reading timeout expired");
  }
}
	bool cql_escape_sequences_remover_t::ends_with_character(const char c) {
		if(!data_available())
			return false;
	
		deque<char>::reverse_iterator it = _buffer.rbegin();
		for(; it != _buffer.rend(); ++it) {
			if(isspace(*it))
				continue;

			return *it == c;
		}

		return false;
	}
示例#7
0
		void proxy<arp_frame>::do_handle_frame(const_helper<ethernet_frame> ethernet_helper, const_helper<arp_frame> arp_helper)
		{
			if (arp_helper.operation() == ARP_REQUEST_OPERATION)
			{
				entry_map_type::const_iterator entry_it = m_entry_map.find(arp_helper.target_logical_address());

				ethernet_address_type eth_addr;

				bool should_answer = false;

				if (entry_it != m_entry_map.end())
				{
					eth_addr = entry_it->second;
					should_answer = true;
				}
				else
				{
					if (m_arp_request_callback)
					{
						should_answer = m_arp_request_callback(arp_helper.target_logical_address(), eth_addr);
					}
				}

				if (should_answer)
				{
					size_t payload_size;

					builder<arp_frame> arp_builder(response_buffer());

					payload_size = arp_builder.write(
					                   ARP_REPLY_OPERATION,
					                   boost::asio::buffer(eth_addr.data()),
					                   arp_helper.target_logical_address(),
					                   arp_helper.sender_hardware_address(),
					                   arp_helper.sender_logical_address()
					               );

					builder<ethernet_frame> ethernet_builder(response_buffer(), payload_size);

					payload_size = ethernet_builder.write(
					                   ethernet_helper.sender(),
					                   ethernet_helper.target(),
					                   ethernet_helper.protocol()
					               );

					data_available(get_truncated_response_buffer(payload_size));
				}
			}
		}
示例#8
0
/******************************************
 ***************   SLOTS   ****************
 *****************************************/
void Device::readData()
{
    data= socket->readAll();
    QString host_address=socket->peerAddress().toString();
    qDebug()<<host_address;

    qDebug()<<"data="<<data;
    if(data.contains("###STATUS"))
    {
        emit data_available(this->dev_id,data); ///< signal is emmitted when "###STATUS" is received
    }
    if(data.contains("#IPSUBNET CHANGED"))
    {
        emit update_me(this->dev_id);   ///< signal is emmitted when "#IPSUBNET CHANGED" is received
    }
}
示例#9
0
int main(void){	
	//whether to blink LED2
	uint8_t blink=1;
	//temporally UART data holder
	uint8_t byte=0;

	//initialize system
	SystemInit();

	//initialize UART5 with 8-N-1 settings, 57600 baudrate
	init_uart(UART5_BASE_PTR,periph_clk_khz,57600);

	//clear all interrupts and enable interrupts
	nvic->ICPR[2] |= 1 << (87 & 0x1F); //Clear Pending Interrupts
	nvic->ISER[2] |= 1 << (87 & 0x1F); //Enable Interrupts
	
	//initialize GPIO ports
	gpio_init();

	//Loop forever
	while(true)
	{
		//use polling method to echo back data when available
		if(data_available()){ 
			byte = uart_read();
			if(byte==0xD) puts((uint8_t *)"\r\n"); //send new line character
			else if ((byte >= 0x30)&&(byte <= 0x39)){
				byte = byte - (0x30); //Evaluate the hexadecimal that has been sent from terminal 0-9
				display(byte); //Display function is written in the gpio.h
			}
			else if ((byte >= 0x41)&&(byte <= 0x46)){
				byte = byte - (0x37); //Evaluate the hexadecimal sent from terminal Uppercase letters A-F
				display(byte);
			}
			else if ((byte >= 0x61)&&(byte <= 0x66)){
				byte = byte - (0x57); //Evaluate hexadecimal sent Lowercase letters a-f
				display(byte);
			}
			else{
				display(byte);  //Show nothing Hopefully
			}
		delay();
			
		}
	}
}
示例#10
0
void read() {
	frame_t f;
	if(data_available(sck,30,0)) {
		f = read_frame(sck, vars, NULL);
		switch(f.cmd) {
			case NW_CMD_BAI:
				printf("Connection closed\n");
				run = 0;
				break;
			case NW_CMD_INVALID:
				printf("Invalid cmd recieved or disconnected.\n");
				run = 0;
				break;
			case NW_CMD_ERROR:
				printf("Server  responed with error: %s\n", vars[0].str);
				break;
			case NW_CMD_DOWNLOAD:
				printf("Server order us to start tcp test %d!\n", vars[0].i);
				run_test(vars[0].i);
			default:
				break;
		}
	}
}
示例#11
0
void test_network() {
	void * nw = malloc(10);
	printf("Testing converting positive floats back and forth:\n");
	srand(time(NULL));
	int errors = 0;
	for(int i = 0; i<1000;++i) {
		int n = rand() % UINT16_MAX;
		float f = (float)n/(float)100.0;
		ftnw(f, nw);
		float b;
		nwtf(nw,&b);
		if(fabs(f-b) > 0.019) {
			printf("Error converting float %f: diff: %f (b: %f)\n", f, fabs(f-b),b);
			++errors;
		}
	}

	printf("Testing converting negative floats back and forth:\n");
	srand(time(NULL));
	for(int i = 0; i<1000;++i) {
		int n = rand() % UINT16_MAX;
		float f = (float)n/(float)100.0;
		f *=-1;
		ftnw(f, nw);
		float b;
		nwtf(nw, &b);
		if(fabs(f-b) > 0.019) {
			printf("Error converting float %f: diff: %f (b: %f)\n", f, fabs(f-b),b);
			++errors;
		}
	}
	float test[]={13.37};
	int count = 1;
	for(int i=0; i < count; ++i) {
		float f=test[i];
		ftnw(f, nw);
		float b;
		nwtf(nw, &b);
		if(fabs(f-b) > 0.019) {
			printf("Error converting float %f: diff: %f (b: %f)\n", f, fabs(f-b),b);
			++errors;
		}
	}
	printf("Number of errors: %d/%d\n",errors,2000+count);
	free(nw);

	printf("Setting up broadcast test on port 4711\n");
	int sock1 = create_udp_socket(4711, true);
	int sock2 = create_udp_socket(4711, true);
	nw_var_t vars[5];
	nw_var_t vars_r[5];
	vars[0].i = 17;
	vars[1].f = 13.371;
	vars[2].c = 'z';
	vars[3].i = (uint32_t)4000000000;

	vars[4].set_str("hailol");
	send_frame(sock1, broadcast_addr(4711), NW_CMD_TEST, vars);
	printf("Waiting\n");
	count=0;
	while(!data_available(sock2, 0,0 )) {
		++count;
		if(count > 1000) {
			printf("Broadcast recive est failed, no data recieved\n");
			exit(-1);
		}
		usleep(100);
	}
	printf("\n");
	addr_t addr;
	frame_t f = read_frame(sock2, vars_r, &addr);
	if(f.cmd == NW_CMD_INVALID) {
		printf("Invalid package recived\n");
	} else {
		if(f.cmd == NW_CMD_TEST)
			printf("Recived correct package:\n");
		else
			printf("Recived wrong package:\n");
		f.print(vars);
	}
	printf("%s\n", vars[4].str);
	close_socket(sock1);
	close_socket(sock2);
}
示例#12
0
int main(int argc, char* argv[]){
	int verbose_flag;
	int network_port = PORT;
  static struct option long_options[] =
  {
		{"port",    required_argument, 0, 'p' },
		{"help",    no_argument,       0, 'h'},
		{"fullscreen",    no_argument,       0, 'f'},
		{"verbose",    no_argument,      &verbose_flag, 'v'},
		{0, 0, 0, 0}
  };

  int option_index = 0;
  int c;


  while( (c=getopt_long(argc, argv, "p:hfv", long_options, &option_index)) != -1 ) {
	switch(c) {
		case 0:
			break;
		case 'p':
			network_port = atoi(optarg);
			printf("Set port to %i\n", network_port);
			break;
		case 'f':
			fullscreen= true;
			break;
		case 'h':
			show_usage();
			exit(0);
		default:
			break;
	}
  }

  if ( ! (argc - optind == 2 || argc - optind == 3) ){
	show_usage();
    exit(1);
  }


	const char * nick = argv[optind++];
	int team = atoi(argv[optind++])-1;
	std::string hostname="";


	setup();
  bool run = true;

  render_splash();

  nw_var_t vars[PAYLOAD_SIZE-1];

  if(optind < argc) {
	  hostname = std::string(argv[optind++]);
  } else {
	msg = "Searching for server...";
	//Try to find with broadcast
	int sockfd = create_udp_socket(BROADCAST_PORT, true);
	for(int i=0;i<5 && run; ++i) {
	   render_splash();
		printf("Hello, anybody out there?\n");
		send_frame(sockfd, broadcast_addr(BROADCAST_PORT), NW_CMD_FIND_SERVER, vars);
		for(int n=0; n<5 && run; ++n) { //Accept 5 other messages
			if(data_available(sockfd,2,0)) {
				addr_t addr;
				frame_t f = read_frame(sockfd,vars, &addr);
				if(f.cmd == NW_CMD_EXISTS_SERVER) {
					hostname = addr.hostname();
					network_port = vars[0].i;

					printf("Yey, found one. Now I'm happy :D\n");
					printf("There is a server at %s:%d with %d players\n", hostname.c_str(), network_port, vars[1].i);
					goto done_searching;
				}
			} else 
				break;
		} 
		printf("Nope :( Maybe later?\n");
		poll(&run);
		usleep(2000000);
	}
	done_searching:
	signal(SIGPIPE, SIG_IGN); //Ignore sigpipe
	close_socket(sockfd);
	signal(SIGPIPE, SIG_DFL); //Restore

	if(hostname == "") {
		printf("No local server found\n");
		cleanup();
		exit(3);
	}
  }
  char buffer[256];
  sprintf(buffer, "Connecting to %s:%d...", hostname.c_str(), network_port);
  msg = std::string(buffer);
  render_splash();

  /* verbose dst */
  if ( verbose_flag ){
	  verbose = stdout;
  } else {
	  verbose = fopen("/dev/null","w");
  }

	srand(time(NULL));



  client = new Client(hostname.c_str(), network_port);
  client->create_me(nick, team);

	msg = "Connected, loading game...";

	while(!ready && run) {
		render_splash();
		usleep(1000);
		poll(&run);
		client->incoming_network();
	}
  
  struct timeval ref;
  gettimeofday(&ref, NULL);

  while ( run ){
    struct timeval ts;
	gettimeofday(&ts, NULL);

    /* calculate dt */
    double dt = (ts.tv_sec - ref.tv_sec) * 1000000.0;
    dt += ts.tv_usec - ref.tv_usec;
    dt /= 1000000;

	if(dt > 1) {
	 	printf("dt too large, setting to 1: %f\n", dt);
	 	dt = 1;
	}

	bool last_shield_status = client->me->full_shield;
	float last_shield_angle = client->me->shield_angle;

    /* do stuff */
    poll(&run);
	 client->incoming_network();
	 logic(dt);
	 client->run(dt);

	if(client->me->full_shield != last_shield_status || client->me->shield_angle != last_shield_angle) {
		client->send_shield();
	}

	 render(dt);
		 
    /* framelimiter */
    const int delay = (REF_DT - dt) * 1000000;
    if ( delay > 0 ){
      usleep(delay);
    }

    /* store reference time */
    ref = ts;
  }

  if(ready) 
	  client->send_quit();

  cleanup();
}
示例#13
0
文件: main.c 项目: ITCTYear3/Elevator
void callbox(byte my_floor) {
    byte rxmessage[PAYLOAD_SIZE];   // Received data payload
    static byte floor, direction;
    word distance;
    static byte flag_dist_init = 0;
    CANframe txframe;   // Transmitted CAN frame
    
    floor = 0xFF;   // Start at false floor
    direction = DIRECTION_STATIONARY;   // Assume starting car direction is stationary
    
    if (!flag_dist_init){
        dist_init();
        flag_dist_init = 1;
    }
    
    if(SW1 && !sw1_pressed) {  
        sw1_pressed = 1;
        button_up(my_floor);
    }
    if(SW2 && !sw2_pressed) { 
        sw2_pressed = 1;
        button_down(my_floor);
    }
    if(!SW1) sw1_pressed = 0;
    if(!SW2) sw2_pressed = 0;
    
    runSerialCAN(MSCAN_NODE_ID);
    
    if(data_available()) {
        
        CANget(rxmessage);
        
        switch(rxmessage[0]) {
            case CMD_LOCATION:
                floor = rxmessage[1];
                direction = rxmessage[2];
                
#ifdef USE_LCD
                LCDclear();
                LCDprintf("Floor: %d\nDir: %d", floor, direction);
#endif
#ifdef USE_LED7
                led7_write(led7_table[floor]);
#endif
                break;
            case CMD_BUTTON_CALL:
                rxmessage[1] == DIRECTION_UP ? button_up(my_floor) : button_down(my_floor);
                break;
            case CMD_ERROR:
#ifdef USE_LCD
                LCDclear();
                LCDprintf("Error condition\nreceived!");
#endif
                break;
            default:
#ifdef USE_LCD
                LCDclear();
                LCDputs("Unknown command");
#endif
                break;
        }
        
        // Turn off indicator LED once car has reached local floor
        if(floor == my_floor) {
            LED1 = 0; LED2 = 0;
        }
        
    }
    
    // Sonar sensor currently attached to callbox 1
    // Send off distance message to controller node
    if (my_floor == FLOOR1) {
        distance = dist_read();
        
        txframe.id = MSCAN_CTL_ID;
        txframe.priority = 0x01;
        txframe.length = 3;
        txframe.payload[0] = CMD_DISTANCE;
        txframe.payload[1] = (distance & 0xFF00) >> 8;         
        txframe.payload[2] = (distance & 0x00FF);
        CANsend(&txframe);
    }
示例#14
0
文件: main.c 项目: ITCTYear3/Elevator
/*
 * Elevator car functionality
 * - 
 */
void car(void) {
    byte ret;
    byte sw1_pressed = 0, sw2_pressed = 0;
    char *command, *floor, *direction;
    byte cur_floor;
    byte elevatorDirection = 0xFF;
    
    CANframe txframe;               // Transmitted CAN frames
    byte rxmessage[PAYLOAD_SIZE];   // Received data payload
    
    cur_floor = 0;
    
    // Message to controller; floor 1
    txframe.id = MSCAN_CTL_ID;
    txframe.priority = 0x01;
    txframe.length = 2;
    txframe.payload[0] = CMD_BUTTON_CAR;
    txframe.payload[2] = DIRECTION_STATIONARY; 
    
    
    if(BTN_FLOOR1) {
        if (BUTTON_FL1 > cur_floor) {
            elevatorDirection = DIRECTION_DOWN;
        } else if (BUTTON_FL1 < cur_floor) {
            elevatorDirection = DIRECTION_UP;
        } else {
            elevatorDirection = DIRECTION_STATIONARY;
        }
        
        txframe.payload[1] = BUTTON_FL1;
        txframe.payload[2] = elevatorDirection;
        
        ret = CANsend(&txframe);
        if(ret) {
            // Message could not be sent!
        }
    }
    if(BTN_FLOOR2) {
        if (BUTTON_FL2 > cur_floor) {
            elevatorDirection = DIRECTION_DOWN;
        } else if (BUTTON_FL2 < cur_floor) {
            elevatorDirection = DIRECTION_UP;
        } else {
            elevatorDirection = DIRECTION_STATIONARY;
        }
        
        txframe.payload[1] = BUTTON_FL2;
        txframe.payload[2] = elevatorDirection;
        
        ret = CANsend(&txframe);
        if(ret) {
            // Message could not be sent!
        }
    }
    if(BTN_FLOOR3) {
        if (BUTTON_FL3 > cur_floor) {
            elevatorDirection = DIRECTION_DOWN;
        } else if (BUTTON_FL3 < cur_floor) {
            elevatorDirection = DIRECTION_UP;
        } else {
            elevatorDirection = DIRECTION_STATIONARY;
        }
        
        txframe.payload[1] = BUTTON_FL3;
        txframe.payload[2] = elevatorDirection;
        
        ret = CANsend(&txframe);
        if(ret) {
            // Message could not be sent!
        }
    }
    if(BTN_DROPEN) {
        txframe.payload[1] = BUTTON_DOOR_OPEN;
        txframe.payload[2] = elevatorDirection;
        
        ret = CANsend(&txframe);
        if(ret) {
            // Message could not be sent!
        }
    }
    if(BTN_DRCLOSE) {
        
        txframe.payload[1] = BUTTON_DOOR_CLOSE;
        txframe.payload[2] = elevatorDirection;
        
        ret = CANsend(&txframe);
        if(ret) {
            // Message could not be sent!
        }
    }
    if(BTN_ESTOP) {
        
        txframe.payload[1] = BUTTON_STOP;
        txframe.payload[2] = elevatorDirection;
        
        ret = CANsend(&txframe);
        if(ret) {
            // Message could not be sent!
        }
    }
    
    // CAN bus <-> serial link
    // Check for new incoming messages and send out received messages via serial
    runSerialCAN(MSCAN_NODE_ID);
    
    if(data_available()) {
        
        CANget(rxmessage);
        
        switch(rxmessage[0]) {
            case CMD_LOCATION:
                command = "Loc";
                break;
            case CMD_BUTTON_CALL:
                command = "Call";
                break;
            case CMD_BUTTON_CAR:
                command = "Car";
                break;
            case CMD_DISP_APPEND:
                command = "Disp";
                break;
            case CMD_ERROR:
                command = "Err";
                break;
            default:
                // Command didn't match known commands!
                goto car_cmd_error;
        }
        
        switch(rxmessage[1]) {
            case FLOOR1:
                floor = "1";
                cur_floor = 1;
                break;
            case FLOOR2:
                floor = "2";  
                cur_floor = 2;
                break;
            case FLOOR3:
                floor = "3"; 
                cur_floor = 3;
                break;
            default:
                // Command didn't match known commands!
                goto car_cmd_error;
        }
        
        
        // Turn off LED when car arrives at requested floor
        if ( rxmessage[0] == CMD_LOCATION ) {           
            if( rxmessage[1] == FLOOR1 ) {
                LED1 = 0;
            }
            if( rxmessage[1] == FLOOR2 ) {
                LED2 = 0;
            }
            /*if( rxmessage[1] == FLOOR3 ) {
                LED3 = 0;
            }*/
        }
        
#ifdef USE_LED7
        led7_write(led7_table[cur_floor]); 
#endif
#ifdef USE_LCD
        LCDhome();
        LCDclear();
        LCDprintf("Command: %s\nFloor%s", command, floor);
#endif
        
        return;
        
car_cmd_error:
        //LCDhome();
        //LCDclear();
        //LCDprintf("Error in\ncommand");
        return;
        
    }
}
示例#15
0
文件: main.c 项目: ITCTYear3/Elevator
/*
 * Controller functionality
 * - Send elevator location messages to callboxes
 * - Listen for button press messages
 */
void controller() {
    byte sw1_pressed = 0, sw2_pressed = 0;
    byte rxmessage[PAYLOAD_SIZE];   // Received data payload 
    byte button_pressed;
	byte next_floor;
    char *button_floor_str, *button_direction_str;
    
    byte update_lcd = 1;
    byte cycle_count = 0;
    word distance;  // car height in cm, distance measurement in mm
    byte cur_floor;
    byte last_floor = 0;
    //byte b;   // used for debug manual frame sending testing
    
    dist_init();
    mctrl_init();   // Initialize servo motor controller
    
    for(;;) {
        
        mctrl_update();
        
        cycle_count++;
        
        if ( cycle_count == 10 ) {
            update_lcd = 1;
            cycle_count = 0;
        }
        
        if ( update_lcd ) {
            update_lcd = 0;
/*            
#ifdef USE_LCD
            if ( cur_floor == 0 ) { 
                LCDclear();
                LCDputs("No car");
            } else {
                LCDclear();
                LCDprintf("%dmm/F%d", car_height, cur_floor);
            }
#endif
*/
        }
        
        // CAN bus <-> serial link
        // Check for new incoming messages and send out received messages via serial
        runSerialCAN(MSCAN_NODE_ID);
        /*
        while ( sci_bytesAvailable() ) {
            sci_readByte(&b);
            lcd_putc(b);  
        } */
        
        if(data_available()) {
            
            CANget(rxmessage);
            
            switch(rxmessage[0]) {
            case CMD_BUTTON_CALL:
                button_pressed = rxmessage[1];
                
                addToQueue(button_pressed);
                next_floor = peekNextFloor();
                pid_setpoint(FLOOR2SETPOINT(next_floor));
                
                switch(cur_floor) {
                case FLOOR1:
                    button_floor_str = "1";
                    break;
                case FLOOR2:
                    button_floor_str = "2";
                    break;
                case FLOOR3:
                    button_floor_str = "3";
                    break;
                default:
                    break;
                }
                
                if(next_floor == cur_floor) {
                    button_direction_str = "stat";
                } else if(next_floor > cur_floor) {
                    button_direction_str = "up  ";
                } else {
                    button_direction_str = "down";
                }
                
#ifdef USE_LCD
                LCDhome();
                LCDprintf("\nFloor%s Dir %s", button_floor_str, button_direction_str);
#else
#ifdef USE_LCD2
                lcd_goto(0x10); // Start at second line
                lcd_puts("Floor");
                lcd_puts(button_floor_str);
                lcd_puts(" Dir ");
                lcd_puts(button_direction_str);
#endif
#endif
                break;
            case CMD_BUTTON_CAR:
                button_pressed = rxmessage[1];
                if(button_pressed == BUTTON_STOP) {
                    // call emergency stop function
                } else if(button_pressed < BUTTON_DOOR_CLOSE) {
                    addToQueue(button_pressed);
                }
                    
                next_floor = peekNextFloor();
                pid_setpoint(FLOOR2SETPOINT(next_floor));
                
                switch(cur_floor) {
                case FLOOR1:
                    button_floor_str = "1";
                    break;
                case FLOOR2:
                    button_floor_str = "2";
                    break;
                case FLOOR3:
                    button_floor_str = "3";
                    break;
                default:
                    break;
                }
                
                if(next_floor == cur_floor){
                    button_direction_str = "stat";
                }else if(next_floor > cur_floor){
                    button_direction_str = "up  ";
                }else {
                    button_direction_str = "down";
                }
                
#ifdef USE_LCD
                LCDhome();
                LCDprintf("\nFloor%s Dir %s", button_floor_str, button_direction_str);
#endif
#ifdef USE_LCD2
                lcd_goto(0x10); // Start at second line
                lcd_puts("Floor");
                lcd_puts(button_floor_str);
                lcd_puts(" Dir ");
                lcd_puts(button_direction_str);
#endif
                break;
            case CMD_DISTANCE:
                distance = (rxmessage[1] << 8) | rxmessage[2];
                pid_feedback(distance);
                
                if (distance < SETPOINT_F1 + FLOOR_MARGIN) cur_floor = FLOOR1;
                if (distance > SETPOINT_F2 - FLOOR_MARGIN && distance < SETPOINT_F2 + FLOOR_MARGIN) cur_floor = FLOOR2;
                if (distance > SETPOINT_F3 - FLOOR_MARGIN && distance < SETPOINT_F3 + FLOOR_MARGIN) cur_floor = FLOOR3;
                
                if ( distance > 1500 ) {
                    cur_floor = 0;
#ifdef USE_LED7
                    led7_write(led7_bars[1]);
#endif
                } else {
#ifdef USE_LED7
                    led7_write(led7_table[cur_floor]);
#endif
                    if ( cur_floor != last_floor ) {
                        update_floor(cur_floor);
                        last_floor = cur_floor;
                        
                        // if we have reached the target floor, pop off the top of the queue
                        // TODO: change name of getNextFloor() to be more descriptive
                        if(cur_floor == next_floor){
                            getNextFloor();
                            next_floor = peekNextFloor();
                            pid_setpoint(FLOOR2SETPOINT(next_floor));
                        }
                    }
                }
#ifdef USE_LCD
                LCDhome();
                LCDprintf("Dist: %4d", distance);
#endif
                break;
            case CMD_ERROR:
                
                break;
            default:
#ifdef USE_LCD
                LCDclear();
                LCDputs("\nUnknown command");
#endif
#ifdef USE_LCD2
                lcd_goto(0x10); // Start at second line
                lcd_puts("Unknown command");
#endif
                break;
            }
            
        }
        
        delay_ms(100);
    }
}
示例#16
0
文件: main.c 项目: ITCTYear3/Elevator
/*
 * Callbox functionality
 * - Listen for button presses, and accept elevator location messages
 */
void callbox(byte my_floor) {
    byte ret;
    byte sw1_pressed = 0, sw2_pressed = 0;
    CANframe txframe;               // Transmitted CAN frame
    byte rxmessage[PAYLOAD_SIZE];   // Received data payload
    static byte floor, direction;
    
    floor = my_floor;                   // Assume starting floor is at this callbox floor
    direction = DIRECTION_STATIONARY;   // Assume starting car direction is stationary
    
    if(SW1 && !sw1_pressed) {
        sw1_pressed = 1;
        LED1 = 1;
        
        // Message to controller; up button pressed
        txframe.id = MSCAN_CTL_ID;
        txframe.priority = 0x01;
        txframe.length = 3;
        txframe.payload[0] = CMD_BUTTON_CALL;
        txframe.payload[1] = my_floor;
        txframe.payload[2] = BUTTON_UP;
        
        ret = CANsend(&txframe);
        if(ret) {
            // Message could not be sent!
        }
    }
    if(SW2 && !sw2_pressed) {
        sw2_pressed = 1;
        LED2 = 1;
        
        // Message to controller; down button pressed
        txframe.id = MSCAN_CTL_ID;
        txframe.priority = 0x01;
        txframe.length = 3;
        txframe.payload[0] = CMD_BUTTON_CALL;
        txframe.payload[1] = my_floor;
        txframe.payload[2] = BUTTON_DOWN;
        
        ret = CANsend(&txframe);
        if(ret) {
            // Message could not be sent!
        }
    }
    
    if(!SW1 && (floor == my_floor)) sw1_pressed = 0;
    if(!SW2 && (floor == my_floor)) sw2_pressed = 0;
    
    if(data_available()) {
        
        CANget(rxmessage);
        
        switch(rxmessage[0]) {
            case CMD_LOCATION:
                floor = rxmessage[1];
                direction = rxmessage[2];
                break;
            case CMD_ERROR:
                // Error condition received!
                break;
            default:
                // Command didn't match known commands!
                break;
        }
        
        LCDclear();
        LCDprintf("Floor: %s\nDir: %s", floor, direction);
    }
}
示例#17
0
文件: hardware.cpp 项目: edga/lc3
 operator int16_t() const {
   return data_available(fd) ? 0x8000 : 0;
 }
示例#18
0
文件: main.c 项目: ITCTYear3/Elevator
/*
 * Controller functionality
 * - Send elevator location messages to callboxes
 * - Listen for button press messages
 */
void controller(void) {
    byte ret;
    byte sw1_pressed = 0, sw2_pressed = 0;
    char *command, *floor, *direction;
    
    CANframe txframe;               // Transmitted CAN frame
    byte rxmessage[PAYLOAD_SIZE];   // Received data payload
    
    
    if(SW1 && !sw1_pressed) {
        sw1_pressed = 1;
        LED1 = 1;
        
        // Message to floor 1 callbox; direction up
        txframe.id = MSCAN_FL1_ID;
        txframe.priority = 0x01;
        txframe.length = 3;
        txframe.payload[0] = CMD_LOCATION;
        txframe.payload[1] = FLOOR1;
        txframe.payload[2] = DIRECTION_UP;
        
        ret = CANsend(&txframe);
        if(ret) {
            // Message could not be sent!
        }
    }
    if(SW2 && !sw2_pressed) {
        sw2_pressed = 1;
        LED2 = 1;
        
        // Message to floor 1 callbox; direction down
        txframe.id = MSCAN_FL1_ID;
        txframe.priority = 0x01;
        txframe.length = 3;
        txframe.payload[0] = CMD_LOCATION;
        txframe.payload[1] = FLOOR1;
        txframe.payload[2] = DIRECTION_DOWN;
        
        ret = CANsend(&txframe);
        if(ret) {
            // Message could not be sent!
        }
    }
    
    if(!SW1) {
        sw1_pressed = 0;
        LED1 = 0;
    }
    if(!SW2) {
        sw2_pressed = 0;
        LED2 = 0;
    }
    
    if(data_available()) {
        
        CANget(rxmessage);
        
        switch(rxmessage[0]) {
            case CMD_BUTTON_CALL:
                command = "Call";
                break;
            case CMD_BUTTON_CAR:
                command = "Car";
                break;
            case CMD_DISP_APPEND:
                command = "Disp";
                break;
            case CMD_ERROR:
                command = "Err";
                break;
            default:
                // Command didn't match known commands!
                goto cmd_error;
        }
        
        switch(rxmessage[1]) {
            case FLOOR1:
                floor = "1";
                break;
            case FLOOR2:
                floor = "2";
                break;
            case FLOOR3:
                floor = "3";
                break;
            default:
                // Command didn't match known commands!
                goto cmd_error;
        }
        
        switch(rxmessage[2]) {
            case BUTTON_UP:
                direction = "Up";
                break;
            case BUTTON_DOWN:
                direction = "Down";
                break;
            default:
                // Command didn't match known commands!
                goto cmd_error;
        }
        
        LCDclear();
        LCDprintf("Command: %s\nFloor%s Dir: %s", command, floor, direction);
        
        return;
        
cmd_error:
        LCDclear();
        LCDprintf("Error in\ncommand");
    }
}
示例#19
0
void Client::incoming_network() {
	if(data_available(_sockfd,0,0)) {
		addr_t addr;
		frame_t f = read_frame(_sockfd,_vars, &addr);
		if(ready) {
			Player * p;
			switch(f.cmd) {
				case NW_CMD_INVALID:
					fprintf(stderr, "Recived invalid package\n");
					break;
				case NW_CMD_QUIT:
					printf("Start erase\n");
					printf("Delete user %i:%s\n", _vars[0].i, players[_vars[0].i]->nick.c_str());
					players.erase(_vars[0].i);
					break;
				case NW_CMD_MOVE:
					p = players[_vars[0].i];
					//Save data to player:
					p->pos.x = _vars[1].f;
					p->pos.y = _vars[2].f;
					p->angle = _vars[3].f;
					p->current_base_texture = (texture_t)_vars[4].i;
					p->velocity.x = _vars[5].f;
					p->velocity.y = _vars[6].f;
					break;
				case NW_CMD_ROTATE:
					p = players[_vars[0].i];
					p->angle = _vars[1].f;
					break;
				case NW_CMD_FIRE:
					p = players[_vars[0].i];
					p->fire = (bool)_vars[1].c;
					break;
				case NW_CMD_SHIELD:
					p = players[_vars[0].i];
					p->shield_angle = _vars[1].f;
					p->full_shield = _vars[2].c;
					break;
				case NW_CMD_JOIN:
					p = new Player(_vars[1].str, _vars[2].i);
					p->id = _vars[0].i;
					players[p->id] = p;
					p->dead = _vars[3].c;
					p->pos.x = _vars[4].f;
					p->pos.y = _vars[5].f;
					p->angle = _vars[6].f;
					printf("Added player %i:%s\n", p->id, p->nick.c_str());
					break;
				case NW_CMD_SPAWN:
					p = players[_vars[0].i];
					printf("Spawn player %s\n", p->nick.c_str());
					p->spawn_remote(vector_t(_vars[1].f, _vars[2].f));
					break;
				case NW_CMD_POWER:
					me->power = _vars[0].f;	
					printf("Server changed our power: %f\n", me->power);
					break;
				case NW_CMD_KILL:
					{
						Player * killer = players[_vars[0].i];
						Player * killed = players[_vars[1].i];

						killed->dead = 1;
						log_message(killer->nick+" killed "+killed->nick);
						char buffer[64];
						if(killed->id == me->id) {
							queue_announcement(std::string("KILLED BY ")+killer->nick);
						} else if(killer->id == me->id && killed->team != me->team) {
							sprintf(buffer, "KILLED %s +%ip", killed->nick.c_str(), KILL_SCORE);
							queue_announcement(std::string(buffer));
						} else if(killer->id == me->id)  {
							sprintf(buffer, "TEAMKILL! %ip", TEAM_KILL_SCORE);
							queue_announcement(std::string(buffer));
						}
					}
					break;
				case NW_CMD_SCORE:
					{
						me->score = _vars[0].i;
						char buffer[128];
						sprintf(buffer,"Your score: %i", me->score);
						log_message(std::string(buffer));
					}
				case NW_CMD_ERROR:
					fprintf(stderr, "Server reported error: %s\n", _vars[0].str);
					break;
			}	
		} else {
			switch(f.cmd) {
				case NW_CMD_INVALID:
					fprintf(stderr, "Recived invalid package\n");
					break;
				case NW_CMD_ACCEPT:
					me->id=_vars[0].i;
					printf("Accepted, player id: %i\n", me->id);
					players[me->id] = me;
					ready = true;
					break;
				default:
					printf("Ignore message (cmd: %i)\n", f.cmd);
					break;
		}
	}
}
}
DiLambda::DiLambda(QObject *parent) :
    QObject(parent)
{
    connect(&current_port, SIGNAL(readyRead()), this, SLOT(data_available()));
    connect(&current_port, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(serial_port_error(QSerialPort::SerialPortError)));
}