Ejemplo n.º 1
0
int
main(int argc, char **argv)
{
    server_args_t server_args;
    pthread_t server_thread;

    seed_random();

    burp_track = track_new(BURP_WAV);
    if (! burp_track) {
	perror(BURP_WAV);
	exit(1);
    }

    p = piface_new();

    pthread_mutex_init(&event_lock, NULL);
    server_args.port = 5555;
    server_args.command = remote_event;
    server_args.state = NULL;

    pthread_create(&server_thread, NULL, server_thread_main, &server_args);

    while(true) {
	wait_for_trigger();
	take_action();
	wait_for_trigger_release();
	if (n_consecutive_spits) {
	    wait_for_recharge();
	}
    }

    return 0;
}
void main(void) {
  
  UINT8 input_validity;
  UINT8 userInput1,userInput2,userInput3;
  UINT8 flag1, flag2;
  
  struct Stepper stepper1,stepper2;
  
	// set up serial port communication
  InitializeSerialPort();

  
  InitializePWM(); 
	
	//Intialize timer
	InitializeTimer();
	
	// allocate and fill receipe array
	InitializeRecipe();
	
	IntializeLED();
	
	set_stepper(&stepper1,PWM_CHANNEL_STEPPER1,STEPPER1_RECIPE);
	set_stepper(&stepper2,PWM_CHANNEL_STEPPER2,STEPPER2_RECIPE);
  
  if(1 == POST())
  {
    printf("POST failed\r\n");
    for(;;) 
    {
    }
    
  } 
  else 
  {
    printf("POST successful\r\n");  
  
    (void)printf(">");
    for(;;) {
      _FEED_COP();
   
      //check serial port for input
      if(1 == BufferEmpty()) 
      { 
        do
        {  
          userInput1 = GetChar();
          (void)printf("%c", userInput1);
          
          userInput2 = GetChar();
          (void)printf("%c", userInput2);
        
          //get <CR>
          userInput3 = GetChar();
          
          if( 'x' == userInput1 || 'X' == userInput1 || 'x' == userInput2 || 'X' == userInput2 ) 
          {
            input_validity = INVALID_INPUTS;  
          } 
          else
          {
            input_validity = VALID_INPUTS;
          }
          
        }while(INVALID_INPUTS == input_validity);
        
        // print <LF> and then '>'
        (void)printf("\r\n>");
        
        //set state of stepper1
        update_state(&stepper1,userInput1);
        
        //set state of stepper2
        update_state(&stepper2,userInput2);
      }
      
      // for stepper  1 take action according to state
      flag1 = take_action(&stepper1);
      
      // for stepper 2 take action acording to state  
  	  flag2 = take_action(&stepper2);
  	  
  	  glow_led(flag1,flag2);
  	  
  	  (void)wait_cycle();
    } 
  }
  
}
Ejemplo n.º 3
0
static int
my_action(void *new_ps, void **children, int n_children, int pn_offset,
	  struct D_Parser *parser, int speculative) {
  D_ParseNode *dd = D_PN(new_ps, pn_offset);
  PyObject *result = NULL;
  PyObject *children_list, *string_list = NULL;
  PNode *pn = (PNode *)new_ps;
  int action_index = pn->reduction->action_index;
  PyObject *tuple = NULL;
  PyObject *arg_types = NULL;
  D_ParserPyInterface *ppi = d_interface(parser);
  int takes_speculative = 0;
  PyObject *action = 0;

  if (PyErr_Occurred()) {
    /* just keep returning until finished parsing.  Need a way to tell dparser to quit */
    return 0;
  }

  if (action_index != -1) {
    tuple = PyList_GetItem(ppi->actions, action_index);
    PyArg_ParseTuple(tuple, "OOi", &action, &arg_types, &takes_speculative);
  }
  
  if (ppi->takes_globals) {
    inc_global_state(parser, dd);
  }

  if (ppi->print_debug_info && tuple) {
    print_debug_info(dd, tuple, speculative, ppi->print_debug_info);
  }

  if (takes_speculative == -1 && !speculative) {
    /* user.t and user.s were already set when this was called speculatively */
    return 0;
  }

  if (ppi->takes_strings) {
    string_list = pylist_children(parser, dd, 1);
    if (string_list == NULL) {
      return -1;
    }
  }
  /* this function owns string_list */

  if (takes_speculative == 0 && speculative) {
    Py_INCREF(Py_None);
    Py_XDECREF(dd->user.t);
    /*    if (dd->user.s)
	  printf("freeing2:%d\n", dd->user.s);*/
    Py_XDECREF(dd->user.s);
    dd->user.t = Py_None;
    //    printf("dd1:%d\n", dd);
    dd->user.s = NULL;
    Py_XDECREF(string_list);
    /*    printf("setting:%d\n", string_list);*/
    //dd->user.s = string_list;

    return 0;
  }

  children_list = pylist_children(parser, dd, 0);
  if (children_list == NULL) {
    /*    if (string_list)
	  printf("freeing3:%d\n", string_list);*/

      Py_XDECREF(string_list);
      return -1;
  }
  /* this function owns children_list */

  if (action_index != -1) {
    result = take_action(arg_types, children_list, speculative, dd, string_list, 
			 n_children, parser, children, pn_offset, action);
    Py_DECREF(children_list);
  } else {
    result = children_list;
  }
  /* function now owns result, string_list */

  if (result == ppi->reject || result == NULL) {
    /*    if (string_list)
	  printf("freeing4:%d\n", string_list);*/

    Py_XDECREF(result);
    Py_XDECREF(string_list);
    return -1;  /* user rejected */
  }

  Py_XDECREF(dd->user.t); /* these may have been set in a speculative pass */
  /*  if(dd->user.s)
      printf("freeing5:%d\n", dd->user.s);*/
  Py_XDECREF(dd->user.s);
  /*  if(dd->user.s)
      printf("setting2:%d\n", string_list);*/
  //  printf("dd2:%d\n", dd);
  dd->user.t = result;
  dd->user.s = string_list;  

  return 0;
}
Ejemplo n.º 4
0
int
main(int argc, char **argv)
{
	int c;

	/* Revoke setgid privileges */
	setgid(getgid());

#ifdef DEBUG
	while ((c = getopt(argc, argv, "a:b:hp:r:t:d")) != -1)
#else
	while ((c = getopt(argc, argv, "a:b:hp:r:t:")) != -1)
#endif
		switch (c) {
		case 'a':
			arrow_num = atoi(optarg);
			break;
		case 'b':
			bat_num = atoi(optarg);
			break;
#ifdef DEBUG
		case 'd':
			debug = 1;
			break;
#endif
		case 'h':
			level = HARD;
			break;
		case 'p':
			pit_num = atoi(optarg);
			break;
		case 'r':
			room_num = atoi(optarg);
			if (room_num < MIN_ROOMS_IN_CAVE) {
				(void)fprintf(stderr,
	"No self-respecting wumpus would live in such a small cave!\n");
				exit(1);
			}
			if (room_num > MAX_ROOMS_IN_CAVE) {
				(void)fprintf(stderr,
	"Even wumpii can't furnish caves that large!\n");
				exit(1);
			}
			break;
		case 't':
			link_num = atoi(optarg);
			if (link_num < 2) {
				(void)fprintf(stderr,
	"Wumpii like extra doors in their caves!\n");
				exit(1);
			}
			break;
		case '?':
		default:
			usage();
	}

	if (link_num > MAX_LINKS_IN_ROOM ||
	    link_num > room_num - (room_num / 4)) {
		(void)fprintf(stderr,
"Too many tunnels!  The cave collapsed!\n(Fortunately, the wumpus escaped!)\n");
		exit(1);
	}

	if (level == HARD) {
		bat_num += ((random() % (room_num / 2)) + 1);
		pit_num += ((random() % (room_num / 2)) + 1);
	}

	if (bat_num > room_num / 2) {
		(void)fprintf(stderr,
"The wumpus refused to enter the cave, claiming it was too crowded!\n");
		exit(1);
	}

	if (pit_num > room_num / 2) {
		(void)fprintf(stderr,
"The wumpus refused to enter the cave, claiming it was too dangerous!\n");
		exit(1);
	}

	instructions();
	cave_init();

	/* and we're OFF!  da dum, da dum, da dum, da dum... */
	(void)printf(
"\nYou're in a cave with %d rooms and %d tunnels leading from each room.\n\
There are %d bat%s and %d pit%s scattered throughout the cave, and your\n\
quiver holds %d custom super anti-evil Wumpus arrows.  Good luck.\n",
	    room_num, link_num, bat_num, plural(bat_num), pit_num,
	    plural(pit_num), arrow_num);

	for (;;) {
		initialize_things_in_cave();
		arrows_left = arrow_num;
		do {
			display_room_stats();
			(void)printf("Move or shoot? (m-s) ");
			(void)fflush(stdout);
			if (!fgets(answer, sizeof(answer), stdin))
				break;
		} while (!take_action());

		if (!getans("\nCare to play another game? (y-n) "))
			exit(0);
		if (getans("In the same cave? (y-n) "))
			clear_things_in_cave();
		else
			cave_init();
	}
	/* NOTREACHED */
	return (0);
}
Ejemplo n.º 5
0
int
main(int argc, char *argv[])
{
	int c;

#ifdef DEBUG
	while ((c = getopt(argc, argv, "a:b:hop:r:t:d")) != -1)
#else
	while ((c = getopt(argc, argv, "a:b:hop:r:t:")) != -1)
#endif
		switch (c) {
		case 'a':
			arrow_num = atoi(optarg);
			break;
		case 'b':
			bat_num = atoi(optarg);
			break;
#ifdef DEBUG
		case 'd':
			debug = 1;
			break;
#endif
		case 'h':
			level = HARD;
			break;
		case 'o':
			oldstyle = 1;
			break;
		case 'p':
			pit_num = atoi(optarg);
			break;
		case 'r':
			room_num = atoi(optarg);
			if (room_num < MIN_ROOMS_IN_CAVE)
				errx(1,
	"no self-respecting wumpus would live in such a small cave!");
			if (room_num > MAX_ROOMS_IN_CAVE)
				errx(1,
	"even wumpii can't furnish caves that large!");
			break;
		case 't':
			link_num = atoi(optarg);
			if (link_num < 2)
				errx(1,
	"wumpii like extra doors in their caves!");
			break;
		case '?':
		default:
			usage();
	}

	if (oldstyle) {
		room_num = 20;
		link_num = 3;
		/* Original game had exactly 2 bats and 2 pits */
		if (bat_num < 0)
			bat_num = 2;
		if (pit_num < 0)
			pit_num = 2;
	} else {
		if (bat_num < 0)
			bat_num = BAT_COUNT;
		if (pit_num < 0)
			pit_num = PIT_COUNT;
	}

	if (link_num > MAX_LINKS_IN_ROOM ||
	    link_num > room_num - (room_num / 4))
		errx(1,
"too many tunnels!  The cave collapsed!\n(Fortunately, the wumpus escaped!)");

	if (level == HARD) {
		if (room_num / 2 - bat_num)
			bat_num += arc4random_uniform(room_num / 2 - bat_num);
		if (room_num / 2 - pit_num)
			pit_num += arc4random_uniform(room_num / 2 - pit_num);
	}

	/* Leave at least two rooms free--one for the player to start in, and
	 * potentially one for the wumpus.
	 */
	if (bat_num > room_num / 2 - 1)
		errx(1,
"the wumpus refused to enter the cave, claiming it was too crowded!");

	if (pit_num > room_num / 2 - 1)
		errx(1,
"the wumpus refused to enter the cave, claiming it was too dangerous!");

	instructions();
	if (oldstyle)
		dodecahedral_cave_init();
	else
		cave_init();

	/* and we're OFF!  da dum, da dum, da dum, da dum... */
	(void)printf(
"\nYou're in a cave with %d rooms and %d tunnels leading from each room.\n\
There are %d bat%s and %d pit%s scattered throughout the cave, and your\n\
quiver holds %d custom super anti-evil Wumpus arrows.  Good luck.\n",
	    room_num, link_num, bat_num, plural(bat_num), pit_num,
	    plural(pit_num), arrow_num);

	for (;;) {
		initialize_things_in_cave();
		arrows_left = arrow_num;
		do {
			display_room_stats();
			(void)printf("Move or shoot? (m-s) ");
			(void)fflush(stdout);
			(void)fpurge(stdin);
			if (!fgets(answer, sizeof(answer), stdin))
				break;
		} while (!take_action());
		(void)fpurge(stdin);

		if (!getans("\nCare to play another game? (y-n) ")) {
			(void)printf("\n");
			exit(0);
		}
		clear_things_in_cave();
		if (!getans("In the same cave? (y-n) ")) {
			if (oldstyle)
				dodecahedral_cave_init();
			else
				cave_init();
		}
	}
	/* NOTREACHED */
}