Example #1
0
/* reads byte from controller
 * Input:	rs	1: read data from DDRAM/CGRAM
 * 			0: read busy flag and address counter 
 */
static uint8_t lcd_read(uint8_t rs)
{
	uint8_t data;

	if (rs)
		set_rs_high();	/* RS=0 -> read data from DD/CGRAM */
	else
		set_rs_low();	/* RS=1 -> read busy flag and AC */
	set_rw_high(); 		/* RW=1 for all read operations */
	data_pins_in(); 

	set_en_high();
	e_delay();		/* enable 1us high (could be set much less) */
	
	data = read_pins(); 
	set_en_low();

	return data; 
}
Example #2
0
void httpd_appcall(void) {
  if(uip_conn->lport != HTONS(80)) {
    uip_abort();
    return;
  }

  struct httpd_state *hs = (struct httpd_state *)&(uip_conn->appstate);
  bool send_new_data = false;

  if(uip_connected()) {
    printf("Connected\n");
    hs->data_count = 0;
    hs->idle_count = 0;
    hs->state = 0;
    hs->done = false;
  } else if(uip_newdata()) {
    printf("New data\n");
    if(strncmp(DATA_BUF, "GET ", 4) != 0) {
      uip_abort();
      return;
    }
    printf("Got get request\n");

#define PATH_START 4
    /* Get path */
    int i;
    for(i = 4; i<uip_datalen() && DATA_BUF[i] != ' '; i++);

    char path[21];
    if( i-PATH_START > 20 ) {
      i = 20;
    }

    memcpy(path, DATA_BUF + PATH_START, i-PATH_START);
    path[i-PATH_START] = '\0';
    printf("Path: '%s'\n", path);

    if(strcmp(path, "/") == 0) {
      printf("root\n");
      hs->request_type = REQUEST_FILE;
      hs->state = FILE_ROOT;
      hs->offset = 0;
    } else if(strncmp(path, "/read", 5) == 0) {
      hs->request_type = REQUEST_READ;
    } else if(strncmp(path, "/write/", 7) == 0) {
      hs->request_type = REQUEST_WRITE;
      hs->state = REQUEST_WRITE_ERR;
      struct header_pin *connector;
      uint8_t pin;
      char value[10];

      if( !parse_path(path+7, &connector, &pin, value) ) {
	goto config_done;
      }

      if( connector[pin].config != CONFIG_OUTPUT ) {
	goto config_done;
      }

      if(value[0] == '1') {
	MAP_GPIOPinWrite(connector[pin].base, connector[pin].pin, connector[pin].pin);
      } else {
	MAP_GPIOPinWrite(connector[pin].base, connector[pin].pin, 0);
      }
      hs->state = REQUEST_WRITE_OK;
    } else if(strncmp(path, "/config/",8) == 0) {
      hs->request_type = REQUEST_CONFIG;
      struct header_pin *connector;
      uint8_t pin;
      char dir[10];

      if( !parse_path(path+8, &connector, &pin, dir) ) {
	printf("Nope\n");
	goto config_done;
      }

      if( connector[pin].config == CONFIG_NOT_USED ) {
	printf("N/A\n");
	goto config_done;
      }

      printf("Dir: '%c'\n", dir[0]);

      if( dir[0] == 'i' ) {
	printf("Input\n");
	connector[pin].config = CONFIG_INPUT;
	configure_pin(&connector[pin]);
      } else if( dir[0] == 'o' ) {
	printf("Output\n");
	connector[pin].config = CONFIG_OUTPUT;
	configure_pin(&connector[pin]);
      }
    } else {
      hs->request_type = 0;
    }
  config_done:
    send_new_data = true;
  } else if( uip_acked() ) {
    hs->data_count++;
    if( hs->done ) {
      uip_close();
    } else {
      send_new_data = true;
    }
  } else if( uip_poll() ) {
    printf("Poll\n");
    hs->idle_count++;
    if( hs->idle_count > 10 ) {
      uip_close();
    }
  }

  if( uip_rexmit() || send_new_data ) {
    printf("%p: Request type: %d\n", hs, hs->request_type);
    printf("%p: Sending data (%d)\n", hs, hs->data_count);
    switch(hs->request_type) {
    case REQUEST_READ:
      if(hs->data_count == 0) {
	hs->xmit_buf = http_json_header;
	hs->xmit_buf_size = sizeof(http_json_header)-1;
      } else if(hs->data_count == 1) {
	hs->xmit_buf = NULL;

	uint8_t buf[500];
	uint16_t i = 0;

	static char b1[] = "{\n\t\"J1\": [";
	static char b2[] = "\t\"J2\": [";
	static char b3[] = "\t\"J3\": [";
	static char b4[] = "\t\"J4\": [";
	static char bnone[] = "\"x\"";
	static char bx[] = "\n}";

	memcpy(buf+i, b1, sizeof(b1)-1);
	i+= sizeof(b1)-1;
	i += read_pins(j1, HEADER_SIZE, buf+i);
	buf[i++] = ']';
	buf[i++] = ',';
	buf[i++] = '\n';

	memcpy(buf+i, b2, sizeof(b2)-1);
	i+= sizeof(b2)-1;
	i += read_pins(j2, HEADER_SIZE, buf+i);
	buf[i++] = ']';
	buf[i++] = ',';
	buf[i++] = '\n';

	memcpy(buf+i, b3, sizeof(b3)-1);
	i+= sizeof(b3)-1;
	i += read_pins(j3, HEADER_SIZE, buf+i);
	buf[i++] = ']';
	buf[i++] = ',';
	buf[i++] = '\n';

	memcpy(buf+i, b4, sizeof(b4)-1);
	i+= sizeof(b4)-1;
	i += read_pins(j4, HEADER_SIZE, buf+i);
	buf[i++] = ']';

	memcpy(buf+i, bx, sizeof(bx)-1);
	i+= sizeof(bx)-1;

	uip_send(buf, i);
	hs->done = true;
      } else {
	hs->xmit_buf = NULL;
	uip_close();
      }
      break;
    case REQUEST_CONFIG:
      if(hs->data_count == 0) {
	hs->xmit_buf = http_json_header;
	hs->xmit_buf_size = sizeof(http_json_header)-1;
	hs->done = true;
      } /*else if(hs->data_count == 1) {
	}*/ else {
	hs->xmit_buf = NULL;
	uip_close();
      }
      break;
    case REQUEST_WRITE:
      if(hs->data_count == 0) {
	hs->xmit_buf = http_json_header;
	hs->xmit_buf_size = sizeof(http_json_header)-1;
      } else if(hs->data_count == 1) {
	if( hs->state == REQUEST_WRITE_OK ) {
	  char buf[] = "ok";
	  uip_send(buf, sizeof(buf)-1);
	} else {
	  char buf[] = "error";
	  uip_send(buf, sizeof(buf)-1);
	}
	hs->done = true;
	hs->xmit_buf = NULL;
      } else {
	hs->xmit_buf = NULL;
	uip_close();
      }
      break;
    case REQUEST_FILE:
      if(hs->data_count == 0) {
	hs->xmit_buf = http_response_header;
	hs->xmit_buf_size = sizeof(http_response_header)-1;
      } else {
	uint32_t offset = (hs->data_count-1) * TRANSFER_SIZE;
	uint32_t remain;

	remain = index_html_len - offset;

	uint16_t count = TRANSFER_SIZE;
	if( remain < TRANSFER_SIZE) {
	  count = remain;
	  hs->done = true;
	}

	printf("remain: %d\n", remain);
	printf("count: %d\n", count);
	if( index_html_len <= offset ) {
	  hs->xmit_buf = NULL;
	  uip_close();
	} else {
	  hs->xmit_buf = index_html + offset;
		hs->xmit_buf_size = count;
		//hs->xmit_buf = "test";
	  //hs->xmit_buf_size = 4;
	}
      }
      break;
    default:
      if(hs->data_count == 0) {
	hs->xmit_buf = http_404_header;
	hs->xmit_buf_size = sizeof(http_404_header)-1;
      } else if(hs->data_count == 1) {
	hs->xmit_buf = unknown_request;
	hs->xmit_buf_size = sizeof(unknown_request)-1;
	hs->done = true;
      } else {
	hs->xmit_buf = NULL;
	uip_close();
      }
      break;
    }

    if (hs->xmit_buf != NULL ) {
      uip_send(hs->xmit_buf, hs->xmit_buf_size);
    }
  }
}