Ejemplo n.º 1
0
/*---------------------------------------------------------------------------*/
static int
write_chunk_pt(struct rudolph0_conn *c, int offset, int flag,
	    uint8_t *data, int datalen)
{
  PT_BEGIN(&recvnetfilept);

  PT_WAIT_UNTIL(&recvnetfilept, receiving_file);
  leds_on(LEDS_YELLOW);
  leds_on(LEDS_RED);
  PT_WAIT_UNTIL(&recvnetfilept, flag == RUDOLPH0_FLAG_NEWFILE);
  leds_off(LEDS_RED);

  do {
    if(datalen > 0) {
      shell_output(&recvnetfile_command, data, datalen, "", 0);
      /*      printf("write_chunk wrote %d bytes at %d\n", datalen, offset);*/
    }
    PT_YIELD(&recvnetfilept);
  } while(flag != RUDOLPH0_FLAG_LASTCHUNK);

  shell_output(&recvnetfile_command, data, datalen, "", 0);
  /*  printf("write_chunk wrote %d bytes at %d\n", datalen, offset);*/
  shell_output(&recvnetfile_command, "", 0, "", 0);
  leds_off(LEDS_YELLOW);
  receiving_file = 0;
  process_post(&shell_recvnetfile_process, PROCESS_EVENT_CONTINUE, NULL);

  PT_END(&recvnetfilept);
}
Ejemplo n.º 2
0
static PT_THREAD(handle_connection(void))
{
	PT_BEGIN(&s.pt);

	s.state = STATE_LISTENING;

	do {
		PT_WAIT_UNTIL(&s.pt, uip_newdata());

		if(uip_newdata() && parse_msg()) {
			s.state = STATE_HELLO_RECEIVED;
			uip_flags &= (~UIP_NEWDATA);
			break;
		}
	} while(s.state != STATE_HELLO_RECEIVED);

	do {
		send_request();
		PT_WAIT_UNTIL(&s.pt, uip_newdata());

		if(uip_newdata()) {
			s.state = STATE_NAME_RECEIVED;
			uip_flags &= (~UIP_NEWDATA);
			break;
		}
	} while(s.state != STATE_NAME_RECEIVED);

	send_response();

	s.state = STATE_INIT;

	PT_END(&s.pt);
}
Ejemplo n.º 3
0
bool
Human::run()
{
	PT_BEGIN();
	
	while (1)
	{
		timer.restart(3000);
		PT_WAIT_UNTIL(timer.isExpired());
		
		for (i = 0; i < 2; i++)
		{
			state = WAVE0;
			timer.restart(500);
			PT_WAIT_UNTIL(timer.isExpired());
			
			state = WAVE1;
			timer.restart(500);
			PT_WAIT_UNTIL(timer.isExpired());
		}
		state = STAND;
	}
	
	PT_END();
}
Ejemplo n.º 4
0
int DLGSM::PT_restart(struct pt *pt, char *ret) {
	static struct pt child_pt;
	static uint32_t timestamp;
	static char u;
	char iret;
	PT_BEGIN(pt);
        u = 0;                
	while (u < 3) {
		_gsmserial.flush();
        	PT_WAIT_THREAD(pt, PT_send_recv_confirm(&child_pt, &iret, "AT\r\n", "OK", 1000));
                if (iret > 0) {
        		PT_WAIT_THREAD(pt, PT_pwr_off(&child_pt, 1));
                        u = 3;
                } else {
                	PT_WAIT_UNTIL(pt, (millis() - timestamp > 1000));
                        timestamp = millis();
                        u++;
			if (_DEBUG) Serial.println(u, DEC);
        	}
	}
	timestamp = millis();
	if (iret == 1)
		PT_WAIT_THREAD(pt, PT_pwr_off(&child_pt, 0));
	PT_WAIT_UNTIL(pt, (millis() - timestamp) > 6000);
	PT_WAIT_THREAD(pt, PT_pwr_on(&child_pt));
	PT_WAIT_THREAD(pt, PT_GSM_init(&child_pt, ret));
	
	_error_cnt = 0;
	_tout_cnt = 0;
	u = 0;
	*ret = 1;
	PT_END(pt);
}
Ejemplo n.º 5
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(handle_dhcp(void))
{
  PT_BEGIN(&s.pt);
  
  /* try_again:*/
  s.state = STATE_SENDING;
  s.ticks = CLOCK_SECOND;

  do {
    send_discover();
    s.timer_init = platform_timer_op( ELUA_DHCP_TIMER_ID, PLATFORM_TIMER_OP_START, 0 );
    PT_WAIT_UNTIL(&s.pt, uip_newdata() || platform_timer_get_diff_us( ELUA_DHCP_TIMER_ID, s.timer_init, platform_timer_op( ELUA_DHCP_TIMER_ID, PLATFORM_TIMER_OP_READ, 0 ) ) >= s.ticks );
    if(uip_newdata() && parse_msg() == DHCPOFFER) {
	  uip_flags &= ~UIP_NEWDATA;
      s.state = STATE_OFFER_RECEIVED;
      break;
    }
	uip_flags &= ~UIP_NEWDATA;
    if(s.ticks < CLOCK_SECOND * 60) {
      s.ticks *= 2;
    } else {
	  s.ipaddr[0] = 0;
	  goto dhcp_failed;
	  }
  } while(s.state != STATE_OFFER_RECEIVED);
  
  s.ticks = CLOCK_SECOND;

  do {
    send_request();
    s.timer_init = platform_timer_op( ELUA_DHCP_TIMER_ID, PLATFORM_TIMER_OP_START, 0 );    
    PT_WAIT_UNTIL(&s.pt, uip_newdata() || platform_timer_get_diff_us( ELUA_DHCP_TIMER_ID, s.timer_init, platform_timer_op( ELUA_DHCP_TIMER_ID, PLATFORM_TIMER_OP_READ, 0 ) ) >= s.ticks );
    
    if(uip_newdata() && parse_msg() == DHCPACK) {
	  uip_flags &= ~UIP_NEWDATA;
      s.state = STATE_CONFIG_RECEIVED;
      break;
    }
    uip_flags &= ~UIP_NEWDATA;
    if(s.ticks <= CLOCK_SECOND * 10) {
      s.ticks += CLOCK_SECOND;
    } else {
      PT_RESTART(&s.pt);
    }
  } while(s.state != STATE_CONFIG_RECEIVED);
  
dhcp_failed:
  dhcpc_configured(&s);
  
  /*
   * PT_END restarts the thread so we do this instead. Eventually we
   * should reacquire expired leases here.
   */
  while(1) {
    PT_YIELD(&s.pt);
  }

  PT_END(&s.pt);
}
Ejemplo n.º 6
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(temperature_thread(struct pt *pt))
{
    PT_BEGIN(pt);

    init_adc(5);
    disable_digital_buffer(5);

    while(1)
    {
        cli();
        
        temperature_counter = 0;
        
        sei();

        PT_WAIT_UNTIL(pt,temperature_counter > 750);

        temp_result = 0;
        temp_sampl_cnt = 0;
     
        for(temp_sampl_cnt = 0; temp_sampl_cnt < 64; temp_sampl_cnt++)
        {    
            start_conversion();
        
            PT_WAIT_UNTIL(pt,!is_adc_busy());

            temp_result += read_adc();
        }        

        dbg(PSTR("> ADC result: %u\r\n"),temp_result >> 6);
    }

    PT_END(pt);
}
Ejemplo n.º 7
0
Archivo: minut.c Proyecto: nnayo/egere
static PT_THREAD( MNT_check_commands(pt_t* pt) )
{
	mnt_event_t ev;
	u8 swap;

	PT_BEGIN(pt);

	// as long as there are no command
	PT_WAIT_UNTIL(pt, OK == FIFO_get(&MNT.cmds_fifo, &MNT.cmd_fr));

	// silently ignore incoming response
	if ( MNT.cmd_fr.resp == 1 ) {
		DPT_unlock(&MNT.interf);
		PT_RESTART(pt);
	}

	switch (MNT.cmd_fr.cmde) {
		case FR_TAKE_OFF:
			// generate take-off event
			PT_WAIT_UNTIL(pt, (ev = MNT_EV_TAKE_OFF) && OK == FIFO_put(&MNT.ev_fifo, &ev) );
			break;

		case FR_MINUT_TIME_OUT:
			MNT_open_time(&MNT.cmd_fr);
			break;

		case FR_STATE:
			if ( (MNT.cmd_fr.argv[0] == 0x7a) || (MNT.cmd_fr.argv[0] == 0x8b) ) {
				//MNT.state = MNT.cmd_fr.argv[1];
			}

			// don't respond, response will be done by CMN
			PT_RESTART(pt);
			break;

		case FR_APPLI_START:
			MNT.started = 1;

			// don't respond
			PT_RESTART(pt);
			break;

		default:
			// shall never happen
			break;
	}

	// build the response to the current command
	swap = MNT.cmd_fr.orig;
	MNT.cmd_fr.orig = MNT.cmd_fr.dest;
	MNT.cmd_fr.dest = swap;
	MNT.cmd_fr.resp = 1;

	// enqueue it
	PT_WAIT_UNTIL(pt, OK == FIFO_put(&MNT.out_fifo, &MNT.cmd_fr));

	PT_RESTART(pt);

	PT_END(pt);
}
Ejemplo n.º 8
0
Archivo: minut.c Proyecto: nnayo/egere
static u8 init_action(pt_t* pt, void* args)
{
	frame_t fr;

	PT_BEGIN(pt);

	// signal init state
	PT_WAIT_UNTIL(pt, frame_set_2(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_STATE, 0, FR_STATE_SET, FR_STATE_INIT)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	// cmde cone stop
	PT_WAIT_UNTIL(pt, frame_set_2(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_MINUT_SERVO_CMD, 0, FR_SERVO_CONE, FR_SERVO_OFF)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	// cmde aero stop
	PT_WAIT_UNTIL(pt, frame_set_2(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_MINUT_SERVO_CMD, 0, FR_SERVO_AERO, FR_SERVO_OFF)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	// led alive 0.25s
	PT_WAIT_UNTIL(pt, frame_set_3(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_LED_CMD, 0, FR_LED_ALIVE, FR_LED_SET, 25)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	PT_YIELD_WHILE(pt, OK);

	PT_END(pt);
}
Ejemplo n.º 9
0
Archivo: minut.c Proyecto: nnayo/egere
static u8 braking_action(pt_t* pt, void* args)
{
	frame_t fr;

	PT_BEGIN(pt);

	// signal braking state
	PT_WAIT_UNTIL(pt, frame_set_2(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_STATE, 0, FR_STATE_SET, FR_STATE_BRAKING)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	// cmde cone stop
	PT_WAIT_UNTIL(pt, frame_set_2(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_MINUT_SERVO_CMD, 0, FR_SERVO_CONE, FR_SERVO_OFF)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	// cmde aero open
	PT_WAIT_UNTIL(pt, frame_set_2(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_MINUT_SERVO_CMD, 0, FR_SERVO_AERO, FR_SERVO_OPEN)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	// time-out 0.1s
	MNT.time_out = TIME_get() + 100 * TIME_1_MSEC;

	PT_YIELD_WHILE(pt, OK);

	PT_END(pt);
}
Ejemplo n.º 10
0
Archivo: minut.c Proyecto: nnayo/egere
static u8 parachute_action(pt_t* pt, void* args)
{
	frame_t fr;

	PT_BEGIN(pt);

	// signal parachute state
	PT_WAIT_UNTIL(pt, frame_set_2(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_STATE, 0, FR_STATE_SET, FR_STATE_PARACHUTE)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	// cmde cone stop
	PT_WAIT_UNTIL(pt, frame_set_2(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_MINUT_SERVO_CMD, 0, FR_SERVO_CONE, FR_SERVO_OFF)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	// cmde aero stop
	PT_WAIT_UNTIL(pt, frame_set_2(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_MINUT_SERVO_CMD, 0, FR_SERVO_AERO, FR_SERVO_OFF)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	// led open 1s
	PT_WAIT_UNTIL(pt, frame_set_3(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_LED_CMD, 0, FR_LED_OPEN, FR_LED_SET, 100)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	PT_YIELD_WHILE(pt, OK);

	PT_END(pt);
}
Ejemplo n.º 11
0
Archivo: minut.c Proyecto: nnayo/egere
static u8 flight_action(pt_t* pt, void* args)
{
	frame_t fr;

	PT_BEGIN(pt);

	// signal flight state
	PT_WAIT_UNTIL(pt, frame_set_2(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_STATE, 0, FR_STATE_SET, FR_STATE_FLIGHT)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	// cmde cone close
	PT_WAIT_UNTIL(pt, frame_set_2(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_MINUT_SERVO_CMD, 0, FR_SERVO_CONE, FR_SERVO_CLOSE)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	// cmde aero close
	PT_WAIT_UNTIL(pt, frame_set_2(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_MINUT_SERVO_CMD, 0, FR_SERVO_AERO, FR_SERVO_CLOSE)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	// time-out = flight time
	MNT.time_out = MNT.open_time * TIME_1_SEC / 10 + TIME_get();

	// led alive 0.1s
	PT_WAIT_UNTIL(pt, frame_set_3(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_LED_CMD, 0, FR_LED_ALIVE, FR_LED_SET, 10)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	PT_YIELD_WHILE(pt, OK);

	PT_END(pt);
}
Ejemplo n.º 12
0
Archivo: minut.c Proyecto: nnayo/egere
static u8 waiting_action(pt_t* pt, void* args)
{
	frame_t fr;

	PT_BEGIN(pt);

	// signal waiting state
	PT_WAIT_UNTIL(pt, frame_set_2(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_STATE, 0, FR_STATE_SET, FR_STATE_WAITING)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	// cmde cone stop
	PT_WAIT_UNTIL(pt, frame_set_2(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_MINUT_SERVO_CMD, 0, FR_SERVO_CONE, FR_SERVO_OFF)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	// led alive 1s
	PT_WAIT_UNTIL(pt, frame_set_3(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_LED_CMD, 0, FR_LED_ALIVE, FR_LED_SET, 100)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	PT_YIELD_WHILE(pt, OK);

	PT_END(pt);
}
Ejemplo n.º 13
0
Archivo: minut.c Proyecto: nnayo/egere
static u8 cone_closed_action(pt_t* pt, void* args)
{
	frame_t fr;

	PT_BEGIN(pt);

	// signal cone closed state
	PT_WAIT_UNTIL(pt, frame_set_2(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_STATE, 0, FR_STATE_SET, FR_STATE_CONE_CLOSED)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	// cmde cone close
	PT_WAIT_UNTIL(pt, frame_set_2(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_MINUT_SERVO_CMD, 0, FR_SERVO_CONE, FR_SERVO_CLOSE)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	// time-out 1s
	MNT.time_out = TIME_get() + 1 * TIME_1_SEC;

	// led alive 0.1s
	PT_WAIT_UNTIL(pt, frame_set_3(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_LED_CMD, 0, FR_LED_ALIVE, FR_LED_SET, 10)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	// led open off 0.0s
	PT_WAIT_UNTIL(pt, frame_set_3(&fr, DPT_SELF_ADDR, DPT_SELF_ADDR, FR_LED_CMD, 0, FR_LED_OPEN, FR_LED_SET, 0)
			&& OK == FIFO_put(&MNT.out_fifo, &fr)
	);

	PT_YIELD_WHILE(pt, OK);

	PT_END(pt);
}
Ejemplo n.º 14
0
int DLGSM::PT_pwr_off(struct pt *pt, uint8_t force) {
	static struct pt child_pt;
	static uint32_t ts;
	char ret;
	PT_BEGIN(pt);
	DEBUG_LOG("GSM power off");
	pinMode(GSM_PWR, OUTPUT);
        if (CONN_get_flag(CONN_PWR) || force) {
		digitalWrite(GSM_PWR, LOW);
		PT_WAIT_UNTIL(pt, (millis() - ts) > 1000);
		ts = millis();
                
                digitalWrite(GSM_PWR, HIGH);

		PT_WAIT_UNTIL(pt, (millis() - ts) > 2000);
		ts = millis();
	               
 		digitalWrite(GSM_PWR, LOW);
                PT_WAIT_UNTIL(pt, (millis() - ts) > 3000);
                
		CONN_set_flag(CONN_PWR, 0);
                CONN_set_flag(CONN_NETWORK, 0);
                CONN_set_flag(CONN_SENDING, 0);
                CONN_set_flag(CONN_CONNECTED, 0);
                PT_WAIT_THREAD(pt, PT_recv(&child_pt, &ret, "DOWN", 15000, 1));
	}
	_gsmserial.flush();
	PT_END(pt);
}
Ejemplo n.º 15
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(pingreq_pt(struct pt* pt, struct mqtt_connection* conn))
{
  PT_BEGIN(pt);

  DBG("MQTT - Sending PINGREQ\r\n");

  /* Write Fixed Header */
  PT_MQTT_WRITE_BYTE(conn, MQTT_FHDR_MSG_TYPE_PINGREQ);
  PT_MQTT_WRITE_BYTE(conn, 0);

  send_out_buffer(conn);

  PT_WAIT_UNTIL(pt, conn->out_buffer_sent);

  /* Start timeout for reply. */
  conn->waiting_for_pingresp = 1;

  /* Wait for PINGRESP
   *
   * Note that the timeout disconnect is handle in the timer callback. */
  reset_packet(&conn->in_packet);
  do {
   PT_WAIT_UNTIL(pt, conn->in_packet.packet_received);
  } while((conn->in_packet.fhdr & 0xF0) != MQTT_FHDR_MSG_TYPE_PINGRESP);
  reset_packet(&conn->in_packet);

  conn->waiting_for_pingresp = 0;

  PT_END(pt);
}
Ejemplo n.º 16
0
/*---------------------------------------------------------------------*/
static
PT_THREAD(recv_tcpthread(struct pt *pt))
{
  PT_BEGIN(pt);

  /* Read the header. */
  PT_WAIT_UNTIL(pt, uip_newdata() && uip_datalen() > 0);

  if(uip_datalen() < sizeof(struct codeprop_tcphdr)) {
    PRINTF(("codeprop: header not found in first tcp segment\n"));
    uip_abort();
    goto thread_done;
  }

  s.len = uip_htons(((struct codeprop_tcphdr *)uip_appdata)->len);
  s.addr = 0;
  uip_appdata += sizeof(struct codeprop_tcphdr);
  uip_len -= sizeof(struct codeprop_tcphdr);

  xmem_erase(XMEM_ERASE_UNIT_SIZE, EEPROMFS_ADDR_CODEPROP);

  /* Read the rest of the data. */
  do {      
    if(uip_len > 0) {
      xmem_pwrite(uip_appdata, uip_len, EEPROMFS_ADDR_CODEPROP + s.addr);
      s.addr += uip_len;
    }
    if(s.addr < s.len) {
      PT_YIELD_UNTIL(pt, uip_newdata());
    }	
  } while(s.addr < s.len);

  /* Kill old program. */
  elfloader_unload();

  /* Link, load, and start new program. */
  int s;
  static char msg[30 + 10];
  s = elfloader_load(EEPROMFS_ADDR_CODEPROP);
  if (s == ELFLOADER_OK)
    sprintf(msg, "ok\n");
  else
    sprintf(msg, "err %d %s\n", s, elfloader_unknown);
    
  /* Return "ok" message. */
  do {
    s = strlen(msg);
    uip_send(msg, s);
    PT_WAIT_UNTIL(pt, uip_acked() || uip_rexmit() || uip_closed());
  } while(uip_rexmit());

  /* Close the connection. */
  uip_close();
    
 thread_done:;
  PT_END(pt);
}
Ejemplo n.º 17
0
/**
 * @brief Button Task
 *
 * @note Called every 1 ms
 */
static
PT_THREAD(ButtonProc(struct pt *pt)) {
static uint32_t tstart;
uint32_t b;

    PT_BEGIN(pt);
    while(1) {
        PT_WAIT_UNTIL(pt,JoyStick_Read()&JOY_CENTER);
        blinking = ! blinking;
        tstart = msTick;
        PT_WAIT_UNTIL(pt,((msTick-tstart)>=DEBOUNCE_TIME));
        PT_WAIT_UNTIL(pt,!(JoyStick_Read()&JOY_CENTER));
    }
    PT_END(pt);
}
Ejemplo n.º 18
0
int DLGSM::PT_recv(struct pt *pt, char *ret, char *conf, int tout, char process) {
	static uint32_t ts, startts;
	static struct pt linerecv_pt;

	PT_BEGIN(pt);

	ts = millis();
	*ret = 0;
	PT_WAIT_UNTIL(pt, _gsmserial.available() || (millis() - ts) > tout);
	ts = millis();
	if (!_gsmserial.available()) {
		*ret = 0;
		PT_EXIT(pt);
	}
	startts = millis();
	_gsm_wline = 1;
	while (_gsmserial.available() || (millis() - startts) < tout) {
		PT_WAIT_THREAD(pt, PT_recvline(&linerecv_pt, ret, _gsm_buff, _gsm_buffsize, tout, process));
		if (conf != NULL && strstr(_gsm_buff, conf) != NULL)
			*ret = 1;
		else if (conf != NULL)
			*ret = 0;
	}
	_gsm_wline = 0;
	PT_END(pt);
}
Ejemplo n.º 19
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(pingreq_pt(struct pt *pt, struct mqtt_connection *conn))
{
  PT_BEGIN(pt);

  DBG("MQTT - Sending PINGREQ\n");

  /* Write Fixed Header */
  PT_MQTT_WRITE_BYTE(conn, MQTT_FHDR_MSG_TYPE_PINGREQ);
  PT_MQTT_WRITE_BYTE(conn, 0);

  send_out_buffer(conn);

  /* Start timeout for reply. */
  conn->waiting_for_pingresp = 1;

  /* Wait for PINGRESP or timeout */
  reset_packet(&conn->in_packet);
  timer_set(&conn->t, RESPONSE_WAIT_TIMEOUT);

  PT_WAIT_UNTIL(pt, conn->in_packet.packet_received || timer_expired(&conn->t));

  reset_packet(&conn->in_packet);

  conn->waiting_for_pingresp = 0;

  PT_END(pt);
}
Ejemplo n.º 20
0
/**
 * The first protothread function. A protothread function must always
 * return an integer, but must never explicitly return - returning is
 * performed inside the protothread statements.
 *
 * The protothread function is driven by the main loop further down in
 * the code.
 */
static int
protothread1(struct pt *pt)
{
  /* A protothread function must begin with PT_BEGIN() which takes a
     pointer to a struct pt. */
  PT_BEGIN(pt);

  /* We loop forever here. */
  while(1) {
    /* Wait until the other protothread has set its flag. */
    PT_WAIT_UNTIL(pt, protothread2_flag != 0);
    printf("Protothread 1 running\n");

    /* We then reset the other protothread's flag, and set our own
       flag so that the other protothread can run. */
    protothread2_flag = 0;
    protothread1_flag = 1;

    /* And we loop. */
  }

  /* All protothread functions must end with PT_END() which takes a
     pointer to a struct pt. */
  PT_END(pt);
}
Ejemplo n.º 21
0
// === Launching balls =================================================
static PT_THREAD (protothread_launch_balls(struct pt *pt))
{
  PT_BEGIN(pt);
  while(1){
	 //make sure we don't put in too many balls
	PT_WAIT_UNTIL(pt, current_balls < MAX_BALLS);
    while(current_balls < MAX_BALLS)    {
        PT_YIELD_TIME_msec(200); //5 balls per second
		current_balls++; //increase ball count in field				
		ball *new_ball = (ball*) malloc (sizeof(ball));	
		new_ball->xc   = int2fix16(315);
		new_ball->yc   = int2fix16(120);
        new_ball->vxc  = int2fix16(-8 + (rand()>>28)); 
        new_ball->vyc  = int2fix16(-8 + (rand()>>27));  
		new_ball->next = NULL;
        new_ball->hit_counter = 0;
        
		if (initial_ball == NULL) { //if chain is empty, this ball is first ball
			initial_ball = new_ball;
            new_ball->prev = NULL;
        }
        else {//otherwise, put it at end
			end_ball->next = new_ball;
            new_ball->prev = end_ball;
        }
        end_ball = new_ball;
    }
  }
  PT_END(pt);
} // end launch ball thread
Ejemplo n.º 22
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(handle_input(void))
{
  unsigned short i;
  char *ptr;
  char next;
  
  next = 1; 
  PT_BEGIN(&s.inpt);
  
  while(1) {
    /* Wait until data arrives. */
    next = 0;
    PT_WAIT_UNTIL(&s.inpt, next && uip_newdata());

    CURSOR_OFF();
    /* Print it out on the screen. */
    ptr = (char *)uip_appdata;
    for(i = 0; i < uip_len; ++i) {
      cbm_k_bsout(*ptr);
      ++ptr;
    }
    CURSOR_ON();
  }  
  PT_END(&s.inpt);
}
Ejemplo n.º 23
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(handle_output(void))
{
  ctk_arch_key_t c;
  char *ptr;
  
  PT_BEGIN(&s.outpt);
  
  while(1) {
    PT_WAIT_UNTIL(&s.outpt, (ctk_mode_get() == CTK_MODE_EXTERNAL) &&
		  kbhit());


    ptr = s.outputbuf;
    s.len = 0;    
    while(kbhit() && s.len < sizeof(s.outputbuf)) {
      c = cgetc();
      *ptr = c;
      ++ptr;
      ++s.len;   
    }

    PT_WAIT_THREAD(&s.outpt, send());
  }
  PT_END(&s.outpt);      
}
Ejemplo n.º 24
0
static int protothreadExplode(struct pt *pt) {
    static unsigned long ts = 0;
	PT_BEGIN(pt);
	while(1) {
		PT_WAIT_UNTIL(pt, state == STATE_EXPLODE);   
		ts = millis(); 
	
		// countdown before explode
		int tDelay = 100;
		while (millis() - ts < 3000) {
			redInd(true);
			piezo(true);
			_delay_ms(tDelay);
			redInd(false);
			piezo(false);
			_delay_ms(tDelay);
			if (tDelay > 10) tDelay -= 10;	
		}

		// explode		
		unsigned long data;
		data = 0b100000110000000011101000;
		for (int i = 1; i < 4; i++) {
			redInd(true);
			piezo(true);
			sendSony(data, 24);
			_delay_ms(200);
			redInd(false);
			piezo(false);
			_delay_ms(100);
		}
		state = STATE_FIND_ME;
	}
	PT_END(pt);
}
Ejemplo n.º 25
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(udp_server_thread(struct pt *pt))
{
    PT_BEGIN(pt);

    uint16_t payloadlen;
    static uint8_t str[64];

    while(1)
    {
        PT_WAIT_UNTIL(pt,check_flag(new_packet,udp_server_flag));

        if((buf[IP_PROTO_P]==IP_PROTO_UDP_V) && (buf[UDP_DST_PORT_H_P]==(MYUDPPORT>>8)) && (buf[UDP_DST_PORT_L_P]==(MYUDPPORT&0xff)))
        {
            /* calculate the udp message length */
            payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN;                            

            /* replace the newline with string terminator */
            buf[UDP_DATA_P+payloadlen-1] = '\0';

            /* add some explanatory header to it */
            sprintf(str,"> udp got: %s\r\n",buf+UDP_DATA_P);                    

            /* send the same message with little modification to the sender */     
            make_udp_reply_from_request(buf,str,strlen(str),MYUDPPORT);                                       
        }

        clear_flag(new_packet,udp_server_flag);
    }

    PT_END(pt);
}
Ejemplo n.º 26
0
int DLGSM::PT_send_recv(struct pt *pt, char *ret, char *cmd, int tout) {
	static uint32_t ts, startts;
	static struct pt linerecv_pt;
	static char gotsmtg = 0;
	PT_BEGIN(pt);

	*ret = 0;
	gotsmtg = 0;
	ts = millis();
	GSM_send(cmd);
	PT_WAIT_UNTIL(pt, _gsmserial.available() || (millis() - ts) > tout);
	ts = millis();

	if (!_gsmserial.available()) {
		PT_RESTART(pt);
	}
	startts = millis();
	_gsm_wline = 1;
	while (_gsmserial.available()  || (millis() - startts) < tout) {
		PT_WAIT_THREAD(pt, PT_recvline(&linerecv_pt, ret, _gsm_buff, _gsm_buffsize, tout, 1));
		if (*ret > gotsmtg)
			gotsmtg = *ret;
		else if (*ret == 0)
			_tout_cnt++;
		ts = millis();
	}
	if (*ret == 0)
		*ret = gotsmtg;
	_gsm_wline = 0;
	PT_END(pt);
}
Ejemplo n.º 27
0
int DLGSM::PT_send_recv_confirm(struct pt *pt, char *ret, char *cmd, char *conf, int tout) {
	static uint32_t ts,startts;
	static struct pt linerecv_pt;
	PT_BEGIN(pt);
	*ret = 0;
	ts = millis();
	GSM_send(cmd);
	PT_WAIT_UNTIL(pt, _gsmserial.available() || (millis() - ts) > tout);

	if (!_gsmserial.available()) {
		*ret = 0;
		PT_EXIT(pt);
	}
	ts = millis();
	startts = millis();
	_gsm_wline = 1;
	while (_gsmserial.available() || (millis() - startts) < tout) { 
		PT_WAIT_THREAD(pt, PT_recvline(&linerecv_pt, ret, _gsm_buff, _gsm_buffsize, tout, 1));
		ts = millis();
		if (*ret == 0)
			_tout_cnt++;

		if (conf != NULL && strstr(_gsm_buff, conf) != NULL) {
			*ret = 1;
			PT_EXIT(pt);
		} else if (conf != NULL && (strstr(_gsm_buff, "ERROR") != NULL || strstr(_gsm_buff, "FAIL") != NULL)) {
			*ret = 2;
			_error_cnt++;
			PT_EXIT(pt);
		} else if (conf != NULL)
			*ret = 0;
	}
	_gsm_wline = 0;
	PT_END(pt);
}
Ejemplo n.º 28
0
IROM bool LedBlink::dispatch(Msg& msg) {
	PT_BEGIN()
	PT_WAIT_UNTIL(msg.is(0, SIG_INIT));
	init();
	while (true) {
		timeout(_msecInterval);
		PT_YIELD_UNTIL(
				msg.is(_src, SIG_CONNECTED) || msg.is(_src, SIG_DISCONNECTED)
						|| timeout());
		switch (msg.signal()) {
		case SIG_TICK: {
			gpio16_output_set(_isOn);
			_isOn = !_isOn;
			break;
		}
		case SIG_CONNECTED: {
			_msecInterval = 1000;
			break;
		}
		case SIG_DISCONNECTED: {
			_msecInterval = 200;
			break;
		}
		default: {
		}
		}

	}
	PT_END();
	return false;
}
Ejemplo n.º 29
0
static PT_THREAD(zmq_sub_send_all(zmq_socket_t *self, zmq_msg_t *msg)) {
    // TODO: implement HWM
    LOCAL_PT(pt);
    PRINTF("> zmq_sub_send_all %d %p\r\n", pt.lc, msg);
    PT_BEGIN(&pt);

    self->out_conn = list_head(self->channel.connections);

    while(self->out_conn != NULL) {
        // TODO: check if queue is full (HWM)
        if((self->out_conn->validated & CONNECTION_VALIDATED) != CONNECTION_VALIDATED) {
            self->out_conn = list_item_next(self->out_conn);
            continue;
        }

        // NB. there is a bug right here.
        // When serving several peers, the msg gets added to the out queues of each of those connections.
        // The problem is: these queues are implemented with Contiki linked list,
        // Which requires to have a 'next' pointer as the first member of the struct.
        // When adding the same msg to several connections' queue, the next pointer gets overwritten every time...
        zmtp_connection_add_out_msg(self->out_conn, msg);

        zmtp_process_post(zmq_socket_output_activity, self->out_conn);
        PT_WAIT_UNTIL(&pt, self->out_conn->out_size <= 0);

        self->out_conn = list_item_next(self->out_conn);
    }

    PT_END(&pt);
}
Ejemplo n.º 30
0
static PT_THREAD(thread_uart(struct pt *pt))
{
    PT_BEGIN(pt);

    while(1)
    {
        PT_WAIT_UNTIL(pt, uart_flag);

        led_green_blink(10); /* 10 timer ticks = 100 ms */

        /* does the local node expects an id
         * or do we have to broadcast it? */
        if(timer_reached(TIMER_ID_INPUT, ID_INPUT_TIMEOUT_TICKS))
        {
            send_id_reply(uart_data);
        }
        else
        {
            set_node_id(uart_data);
        }
        uart_flag = 0;
    }

    PT_END(pt);
}