Esempio n. 1
0
void show_message(void)
{	
	int loop = 5;
	int new_get = 0;
	int des_get = 0;
	int next_floor = 0;
	
	printf("#%d : stop\n", floor);

	while(1)
	{
		if(state == UP)
		{
			next_floor = get_up_next_floor();
			if(next_floor == floor)
			{ open_door(UP, next_floor); }
			else if(next_floor > floor)
			{ move_up(); }
			else if(next_floor < 1)
			{ move_stop(); }
		}
		else if(state == DOWN)
		{
			next_floor = get_down_next_floor();
			if(next_floor == floor)
			{ open_door(DOWN, next_floor); }
			else if(next_floor < floor)
			{ move_down(); }
			else if(next_floor > NUM)
			{ move_stop(); }
		}
		else if(state == STOP)
		{
			if(open_flag)
			{
				open_door(NONE, 0);
				open_flag = 0;
			}
			else
			{
				next_floor = get_up_next_floor();
				if(next_floor == floor)
				{ open_door(UP, next_floor); }
				else if(next_floor > floor)
				{ move_up(); }
				else if(next_floor < 1)
				{
					next_floor = get_down_next_floor();
					if(next_floor == floor)
					{ open_door(DOWN, next_floor); }
					else if(next_floor < floor)
					{ move_down(); }				
				}
			}
		}
	}	
}
Esempio n. 2
0
/* attach to given JVM */
jvm_t* jvm_attach(pid_t pid) {
    jvm_t* jvm;
    int door_fd, attach_fd, i;

    jvm = (jvm_t*) calloc(1, sizeof(jvm_t));
    if (jvm == NULL) {
        set_jvm_error(JVM_ERR_OUT_OF_MEMORY);
        print_debug("calloc failed in %s at %d\n", __FILE__, __LINE__);
        return NULL;
    }
    jvm->pid = pid;
    attach_fd = -1;

    door_fd = open_door(pid);
    if (door_fd < 0) {
        print_debug("trying to create attach file\n");
        if ((attach_fd = create_attach_file(pid)) < 0) {
            goto quit;
        }

        /* send QUIT signal to the target so that it will
         * check for the attach file.
         */
        if (send_sigquit(pid) != 0) {
            set_jvm_error(JVM_ERR_CANT_SIGNAL);
            print_debug("sending SIGQUIT failed\n");
            goto quit;
        }

        /* give the target VM time to start the attach mechanism */
        do {
            int res;
            RESTARTABLE(poll(0, 0, 200), res);
            door_fd = open_door(pid);
            i++;
        } while (i <= 50 && door_fd == -1);
        if (door_fd < 0) {
            print_debug("Unable to open door to process %d\n", pid);
            goto quit;
        }
    }

quit:
    if (attach_fd >= 0) {
        file_close(attach_fd);
        delete_attach_file(jvm->pid);
    }
    if (door_fd >= 0) {
        jvm->door_fd = door_fd;
        clear_jvm_error();
    } else {
        free(jvm);
        jvm = NULL;
    }
    return jvm;
}
Esempio n. 3
0
//Each elevator is a while loop. 
//Check the global list and if it’s empty, block on the condition variable for blocking elevators. 
//When the elevator gets a person to service, it moves to the appropriate flo0or and opens its door. 
//It puts itself into the person’s e field, then signals the person and blocks until the person wakes it up. 
//When it wakes up, it goes to the person’s destination floor, opens its door, signals the person and blocks. 
//When the person wakes it up, it closes its door and re-executes its while loop.
//• Your elevator’s should call open door(), close door() from move to floor() appropriately. 
//All sleeping and printing should be done in elevator skeleton.c. 
//Thus, your final program that you hand in should not do any sleeping or any printing.
void *elevator(void *arg)
{
    Person *person_in_transit;
    for(;;)
    {

        pthread_mutex_lock(((Elevator*)arg)->es->lock);
        while (!((Queue*)((Elevator*)arg)->es->v)->count)
            pthread_cond_wait(((Queue*)((Elevator*)arg)->es->v)->cond, ((Elevator*)arg)->es->lock);
            if(!dll_empty(((Queue*)((Elevator*)arg)->es->v)->passengers)){
             person_in_transit = (Person*)jval_v(dll_val(dll_first(((Queue*)((Elevator*)arg)->es->v)->passengers)));
             dll_delete_node(dll_first(((Queue*)((Elevator*)arg)->es->v)->passengers));
            }
        // unlock the critial section.
        pthread_mutex_unlock(((Elevator*)arg)->es->lock);
       //move the elevator.
        --((Queue*)((Elevator*)arg)->es->v)->count;
        person_in_transit->from == ((Elevator*)arg)->onfloor?:move_to_floor(((Elevator*)arg), person_in_transit->from);
        //open the door
        open_door(((Elevator*)arg));
        //add the people to the elevator
        person_in_transit->e = ((Elevator*)arg); 
        //signal the person
        pthread_mutex_lock(person_in_transit->lock);
        pthread_cond_signal(person_in_transit->cond);
        pthread_mutex_unlock(person_in_transit->lock);
        // have the elevator wait
        pthread_mutex_lock(((Elevator*)arg)->lock);
        pthread_cond_wait(((Elevator*)arg)->cond, ((Elevator*)arg)->lock);
        pthread_mutex_unlock(((Elevator*)arg)->lock);
        // close door 
        close_door(((Elevator*)arg));
        // elevator moves
        move_to_floor(person_in_transit->e,person_in_transit->to);
        // open the door once move has completed.
        open_door(((Elevator*)arg));
        // signal the person
        pthread_mutex_lock(person_in_transit->lock);
        pthread_cond_signal(person_in_transit->cond);
        pthread_mutex_unlock(person_in_transit->lock);
        // block the elevator
        pthread_mutex_lock(((Elevator*)arg)->lock);
        pthread_cond_wait(((Elevator*)arg)->cond, ((Elevator*)arg)->lock);
        pthread_mutex_unlock(((Elevator*)arg)->lock);
        // close the door
        close_door(((Elevator*)arg));
        // restart. 
    }
}
Esempio n. 4
0
int do_push(string arg)
{
	object room;

	if (is_open())
	return notify_fail("门已经是开着了。\n");

	if (!arg || (arg != "door" && arg != "men"))
	return notify_fail("你要推什么?\n");

	room = find_object(query("restroom"));
	if(! objectp(room) )
	room = load_object(query("restroom"));	
	
	if(objectp(room))
	{
		if( room->query_inside_person()<=0 )
		{
			open_door();
			message_vision("$N轻轻地把门推开。\n", this_player());
			room->open_door(__FILE__);
			remove_call_out("close_men");
			call_out("close_men", 10);
		}
		else 
		{
			message_vision("$N想把门推开,却发觉门被人从里面闩上了。\n",this_player());
		}
	}

	return 1;
}
Esempio n. 5
0
void
compute_opener_floors (void)
{
    size_t i;

    for (i = 0; i < opener_floor_nmemb;) {
        struct opener_floor *o = &opener_floor[i];
        if (fg (&o->p) == OPENER_FLOOR) {
            i++;
            continue;
        }
        remove_opener_floor (o);
    }

    for (i = 0; i < opener_floor_nmemb; i++) {
        struct opener_floor *o = &opener_floor[i];
        if (o->pressed && ! o->broken) {
            if (! o->noise) {
                alert_guards (&o->p);
                play_audio (&opener_floor_audio, &o->p, -1);
                o->noise = true;
            }
            open_door (o->p.l, o->event, o->priority, false);
        } else o->noise = false;
    }
}
Esempio n. 6
0
// Attempts to open a door if present, return 1 on succesful open
int attempt_open_door(player_t* p, room_t* r)
{
    object_t* door = NULL;
    
    if(is_of_type(object_at(p->pos.x - 1, p->pos.y, r), DOOR_ID))
        door = object_at(p->pos.x - 1, p->pos.y, r);

    else if(is_of_type(object_at(p->pos.x + 1, p->pos.y, r), DOOR_ID))
        door = object_at(p->pos.x + 1, p->pos.y, r);

    else if(is_of_type(object_at(p->pos.x, p->pos.y - 1, r), DOOR_ID))
        door = object_at(p->pos.x, p->pos.y - 1, r);

    else if(is_of_type(object_at(p->pos.x, p->pos.y + 1, r), DOOR_ID))
        door = object_at(p->pos.x, p->pos.y + 1, r);

    if(door != NULL)
	{
        open_door(door);
		return 1;
	}
	else
	{
		return 0;
	}
}
Esempio n. 7
0
static void goto_level(int position)
{
    current_position = position;
    levels[position].state = 0;
    open_door(position);
    close_door(position);
}
Esempio n. 8
0
void do_open( char_data* ch, char* argument )
{
  thing_data*  thing;
  obj_data*      obj;
  exit_data*    door;

  if( is_confused_pet( ch ) )
    return;

  if( ( thing = one_thing( ch, argument, "open",
    (thing_array*) &ch->in_room->exits, ch->array,
    &ch->contents ) ) == NULL )
    return;

  if( ( obj = object( thing ) ) != NULL ) {
    include_closed = FALSE;
    open_object( ch, obj );
    include_closed = TRUE;
    return;
    }

  if( ( door = exit( thing ) ) != NULL ) {
    open_door( ch, door );   
    return;
    }

  send( ch, "%s isn't something you can open.\n\r", thing );
}
Esempio n. 9
0
int do_open(string arg)
{
	object room;
	object me = this_player();

	if (is_open())
		return notify_fail("大门已经是开着了。\n");

	if (!arg || (arg != "men" && arg != "door" && arg != query("door_dir")))
		return notify_fail("你要开什么?\n");

	if(!objectp( room = find_object(query("outroom"))) )
		room = load_object(query("outroom"));
	if(objectp(room))
	{
		open_door();
		message_vision("$N轻手轻脚地把门打开。\n", this_player());
		room->open_door(__FILE__);
		
		if (me->query_temp("sleeped"))
		message("vision", "吱地一声,"+me->query("name")+"精神焕发地从里面把门打开了。\n",room);
		else
		message("vision", "梆地一声,"+me->query("name")+"从里面把门打开,一脸的不耐烦。\n",room);
		
		remove_call_out("close_men");
		call_out("close_men", 10);
	}

	return 1;
}
Esempio n. 10
0
int handle_event_going_down(int event, int order_table[N_FLOORS][N_BUTTONS]) {
	int next_state = GOING_DOWN;
	switch (event) {

			case EMERGENCY_BUTTON:
				change_speed(S_DOWN, S_STOP);
				activate_emergency(order_table);
				next_state = EMERGENCY;
				break;

			case GO_UP:
				change_speed(S_DOWN, S_UP);
				next_state = GOING_UP;
				break;

			case GO_DOWN:
				next_state = GOING_DOWN;
				break;
			
			case STOP:
				change_speed(S_DOWN, S_STOP);
				next_state = STOP_GOING_DOWN;
				delete_orders_at_current_floor(order_table);
				open_door();
				timer_set();
				break;
			
			case OBSTRUCTION:
				change_speed(S_DOWN, S_STOP);
				activate_emergency(order_table);
				next_state = EMERGENCY;
				break;
		}
	return next_state;
}
Esempio n. 11
0
int main(int argc, char **argv) {
	(void) argc;
	(void) argv;

	check_config();

	setbuf(stdout, NULL);
	setbuf(stderr, NULL);
	log("tuerd (rev " GIT_REV  ") starting up");

	if(getenv("TUERD_DEBUG"))
		debug = 1;

	debug("Debugging enabled. This allows people to be tracked!");

	// initialize git
	git_init();

	// initialize nfc
	nfc_device *nfc_dev = rfid_init();
	if(!nfc_dev)
		die("rfid_init failed");

	// initialize watchdog handler
	struct sigaction sa;
	sa.sa_handler = sigalarm;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = 0;

	sigaction(SIGALRM, &sa, NULL);

	while(1) {
		debug("-----------------");
		debug("waiting for event");

		if(!rfid_poll(nfc_dev))
			die("rfid_poll failed");

		debug("successfully got target");

		alarm(10);

		if(rfid_authenticate_any(nfc_dev, get_key_git)) {
			debug("auth succeeded, opening door");
			open_door();
			alarm(0);

			sleep(10);
		} else {
			debug("auth failed");
			alarm(0);

			sleep(1);
		}
	}

	return EXIT_SUCCESS;
}
Esempio n. 12
0
void TRIGGER::tick()
{
	if (server_tick() % 10 == 0)
	{
		if (hit_character())
			open_door(server_tick());
	}

	return;
}
Esempio n. 13
0
void
break_opener_floor (struct pos *p)
{
    struct opener_floor *o = opener_floor_at_pos (p);
    if (! o) return;
    open_door (o->p.l, o->event, anim_cycle, true);
    register_con_undo
    (&undo, p,
     MIGNORE, MIGNORE, o->event + EVENTS, MIGNORE,
     NULL, false, "LOOSE FLOOR BREAKING");
    o->broken = true;
}
Esempio n. 14
0
int force_open()
{
	object room;
	if(!objectp( room = find_object(query("outroom"))) )
		room = load_object(query("outroom"));
	if( !objectp(room) )	return 0;
	if( query_inside_person() <= 0 )	return 0;
	
	open_door();
	room->open_door(__FILE__);
	message("vision","外面突然响起粗重的脚步声,由远而近,到门前停了下来...\n"+query("force_name")+"啪地把门打开,伸个头进来,一脸狐疑:呆大半天了还不出去,搞什么鬼?\n", room);
	remove_call_out("close_men");
	call_out("close_men", 10);
}
Esempio n. 15
0
void signal_handler(int sig)
{
		if(sig==SIGUSR1) {
			open_door(1);
		}
		else if(sig==SIGHUP) {
			clear_config();
			read_config();
		}
		else if(sig==SIGINT) {
			printf("Got SIGINT! Exiting...");
			clear_config();
			exit(0);
		}
}
Esempio n. 16
0
static QCC_BOOL parse(alljoyn_busattachment bus, alljoyn_observer observer, char* input)
{
    char* cmd;
    char* arg;
    char* argend;

    /* ignore leading whitespace */
    for (cmd = input; *cmd && isspace(*cmd); ++cmd) {
    }
    if (!*cmd) {
        return QCC_TRUE;
    }

    /* look for an argument */
    for (arg = cmd + 1; *arg && isspace(*arg); ++arg) {
    }
    for (argend = arg; *argend && !isspace(*argend); ++argend) {
    }
    *argend = '\0';

    switch (*cmd) {
    case 'q':
        return QCC_FALSE;

    case 'l':
        list_doors(bus, observer);
        break;

    case 'o':
        open_door(bus, observer, arg);
        break;

    case 'c':
        close_door(bus, observer, arg);
        break;

    case 'k':
        knock_and_run(bus, observer, arg);
        break;

    case 'h':
    default:
        help();
        break;
    }

    return QCC_TRUE;
}
Esempio n. 17
0
void	choose_draw(t_env *e, int start, int end)
{
	if (e->text == 0)
		draw_tlwall(e, start, end);
	else
	{
		if (e->xpm == 10 && e->open == 1)
			open_door(e, start, end);
		else if (e->xpm >= 11 && e->xpm < 15)
			draw_spwall(e, start, end, (e->xpm - 4));
		else if (e->xpm >= 15)
			draw_sp_multiwall(e, start, end, (e->xpm - 4));
		else
			draw_wall(e, start, end);
	}
}
Esempio n. 18
0
File: game.c Progetto: phoboz/edom
static void game_keydown(int key)
{
  int input = get_input_keydown(key);

  if (input & PRESS_ENTER)
    open_door();

  if (input & PRESS_FIRE)
    attack();

  if (input & PRESS_ADVANCE)
    descend_level();

  if (input & PRESS_REVERT)
    ascend_level();
}
Esempio n. 19
0
File: hooks.c Progetto: Es-so/Wolf3D
int		loop_hook(t_mega *all)
{
	open_door(all);
	if (all->e.refresh == 1)
	{
		ft_bzero(all->img.data, WWIN * HWIN * 4);
		raycasting_calcul(all);
		mlx_put_image_to_window(all->e.mlx, all->e.wmlx, all->img.img,
			(WWIN_HUD / 2) - (WWIN / 2), 20);
		loot_ammo(all->sprites, &all->player, &all->cam);
		display_weapon(all);
		display_hud(all);
		all->e.refresh = 0;
	}
	key_hook(all);
	return (1);
}
Esempio n. 20
0
void creature_t::try_move(int move_x, int move_y)
{
  tile_t* tile = get_tile_at(current_dungeon, move_x, move_y);
  if (tile && (tile_is_walkable_by(*tile, this)))
  {
    creature_t* thing = get_creature_at(current_dungeon, move_x, move_y);
    if (thing)
    {
      if (get_active_effects_for(this) & EF_CONFUSE)
      {
        creature_attacks(this, thing);
      }
      else if ((is_ally() && !thing->is_ally()) || (!is_ally() && thing->is_ally()))
      {
        // The two creatures are enemies.
        creature_attacks(this, thing);
      }
    }
    else if (player.pos.x == move_x && player.pos.y == move_y)
    {
      if (!is_friendly() || (get_active_effects_for(this) & EF_CONFUSE))
      {
        // Not ally with player, or confused.
        creature_attacks(this, &player);
      }
    }
    else
    {
      pos.x = move_x;
      pos.y = move_y;
    }
  }
  else if (tile && tile->id == TILE_DOOR_CLOSED)
  {
    if (flag & CF_INTELLIGENT)
    {
      open_door(current_dungeon, this, move_x, move_y);
      //creature->pos.x = move_x;
      //creature->pos.y = move_y;
    }
  }
}
Esempio n. 21
0
void
compute_opener_floors (void)
{
  size_t i;

  for (i = 0; i < opener_floor_nmemb; i++) {
    struct opener_floor *o = &opener_floor[i];
    if (o->p.room == -1) {
      /* remove_opener_floor (o); i--; */
      continue;
    }
    if (o->pressed || o->broken) {
      if (! o->noise) {
        alert_guards (&o->p);
        play_sample (opener_floor_sample, o->p.room);
        o->noise = true;
      }
      open_door (o->event);
    } else o->noise = false;
  }
}
Esempio n. 22
0
int main(int argc, char *argv[]) {
	/* variables for serial communication */
	int fd, c;
	struct termios current_io, new_io;
	char buf[1024];
	char devfile[1024];
	int baudrate = DEFAULT_BAUDRATE;

	bzero(buf, sizeof(buf));
	strncpy(devfile, DEFAULT_DEVICE, sizeof(devfile));
	for (c = 1; c < argc; c++) {
		if (1 == sscanf(argv[c], "--devfile=%s", devfile)) {
			continue;
		}
	}

	fd = open(devfile, O_RDWR | O_NOCTTY);
	if (fd < 0) {
		perror(devfile);
		exit(-1);
	}

	tcgetattr(fd, &current_io);
	bzero(&new_io, sizeof(new_io));
	new_io.c_cflag = baudrate | CRTSCTS | CS8 | CLOCAL | CREAD;
	new_io.c_iflag = IGNPAR | ICRNL;
	new_io.c_oflag = 0;
	new_io.c_lflag = ICANON;
	new_io.c_cc[VEOF] = 4;
	new_io.c_cc[VMIN] = 1;

	tcflush(fd, TCIFLUSH);
	tcsetattr(fd, TCSANOW, &new_io);

	for (c = 1; c < argc; c++) {
		if (strcmp(argv[c], "--close") == 0)
		{
			close_door(fd);
			break;
		}

		if (strcmp(argv[c], "--open") == 0)
		{
			open_door(fd);
			break;
		}
	}

	{
		int state = get_door_state(fd);
		if(state == IS_OPENED)
		{
			puts("opened");
		}

		if(state == IS_CLOSED)
		{
			puts("closed");
		}
	}

	/* restore current io settings */
	tcsetattr(fd, TCSANOW, &current_io);

	/* close serial communication */
	close(fd);

	fflush(stdout);
	return 0;
}
Esempio n. 23
0
int main(void)
{
    if (pfio_init() < 0) perror("Cannot start PFIO");

    long int code = 0, last = 0;
    int count = 0;
    bool last_d0 = 0, last_d1 = 0;
    
    read_config();
    
    if(signal(SIGHUP, signal_handler) == SIG_ERR) perror("Cannot handle signal SIGHUP.");
    if(signal(SIGUSR1, signal_handler) == SIG_ERR) perror("Cannot handle signal SIGUSR1.");
    if(signal(SIGINT, signal_handler) == SIG_ERR) perror("Cannot handle signal SIGINT.");

		beep(1000);

#if DEBUG
    printf("Ready!\n");
#endif

    while (1)
    {
      int i = pfio_read_input();
			if(i == 0xFF) usleep(1);

			struct timeval tv;
      gettimeofday(&tv, NULL);
      long int now = tv.tv_usec + tv.tv_sec*1000;

      if((now - last > TIMEOUT)||(now<last)) {
#if DEBUG
        printf(" - ");
#endif
        count = 0;
        code = 0;
      }

      bool d0 = (i & MASK_D0)==0;
      bool d1 = (i & MASK_D1)==0;
      bool button = (i & MASK_EXIT)==0;
      bool lockcmd = (i & MASK_LOCK)==0;
      
      if(lockcmd || locked) {
#if DEBUG
	        printf("*** LOCK ***\n");
#endif
      		while(((pfio_read_input() & MASK_LOCK)==0)||locked) {
						pfio_digital_write(PIN_GLED,1);
		        usleep(200000);
						pfio_digital_write(PIN_GLED,0);
		        usleep(200000);
          }
#if DEBUG
	        printf("*** UNLOCK ***\n");
#endif
      }
      
      if(button) {
					#if DEBUG
							printf("Exit button pressed!!!\n");
					#endif
		      open_door_start();
          beep(100);
          while((pfio_read_input() & MASK_EXIT)==0) sleep(1);
					#if DEBUG
							printf("Exit button released!!!\n");
					#endif
          sleep(DOOR_DELAY);
          open_door_stop();
      }
      
			if(d0 && !last_d0) {
#if DEBUG
        printf("0");
#endif
        code = code << 1;
        count++;
      }
			if(d1 && !last_d1) {
#if DEBUG
        printf("1");
#endif
        code = (code << 1) + 1;
        count++;
      }

      if(count==BITSCOUNT) {
#if DEBUG
        printf("[%li]\n",code);
#endif

				struct card * c = cards;
				bool allowed = 0;
				while(c) {
					if(c->code == code) {
						allowed = 1;
						break;
					}
					c = c->next;
				};

        if(allowed) {
            // Accepte
#if DEBUG
          printf("Access authorized!\n");
#endif
          open_door(0);
#if DEBUG
          printf("OK\n");
#endif

        } else {
            // Refuse
#if DEBUG
          printf("Access denied!\n");
#endif
          int n;
          for(n=0; n<3; n++) {
            usleep(200000);
            beep(100);
          }
#if DEBUG
          printf("OK\n");
#endif
        }

        code = 0;
        count = 0;
      }

#if DEBUG
			fflush(stdout);
#endif

			last_d0 = d0;
			last_d1 = d1;
			last = now;
		}

    pfio_deinit();
    return 0;
}
Esempio n. 24
0
int main(void) {
    WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer

    chusma1=BCSCTL1;
    chusma2=CALBC1_1MHZ;

    chusma3=DCOCTL;
    chusma4=CALDCO_1MHZ;


    /// P1SEL &=~(BUTTON |GREEN | RED );

//****************Measurement of SMCLK********************************************************
// if SHOW_SMCLK is enabled (=1) smclk signal is visible on P1.4 useful to measure system clk
// (WARNING !! if this test is enabled only scope should be connected to  P1.4  )
#define SHOW_SMCLK 0		// =1 Show System bus clk / =0 Normal I/O operation
//*****************************************************************************************



#if SHOW_SMCLK     // show SMCLK on P1.4  (WARNING !! only scope should be connected to  P1.4  )

    P1SEL=IO_FUNCTION | P1_4 ;  // select SMCLK as primary function
    P1SEL2=IO_FUNCTION;
    P1DIR |= P1_4;  // P1.4 as output "required"
    for(;;){}  // Endless loop

#else
    P1SEL=IO_FUNCTION;
    P1SEL2=IO_FUNCTION;
    P1DIR |= (GREEN | RED);
#endif



    P1IES &= ~ (WIEGAND_0 | WIEGAND_1); //Rising edge


    P1REN |= PULLUPS;  // Enable Pull up/down resistors (THIS DISABLES SMCLK WHY ???)

    P1IFG &= ~(BUTTON | P1_4 | P1_5); // Clear pending flags
    P1IE |= (P1_4 | P1_5);   // Enable P1 Interrupts

    lamp=GREEN;

    P1OUT = lamp | PULLUPS;



    init_wiegand();
   init_timer();


    _BIS_SR(GIE);



	for(;;) {
		volatile unsigned int i;	// volatile to prevent optimization

		// P1OUT ^= lamp;				// Toggle P1.0 using exclusive-OR

		i = 10000;				// SW Delay
		do i--;
		while(i != 0);

		if(wiegand_get_status())  // if a card was read
		{

			facility_code=wiegand_get_card_fc(); // get fc
			itoa(facility_code,card_fc,10); //Convert to string
			card_nr= wiegand_get_card_number(); // then get its data
			itoa(card_nr,card_number,10); //Convert to string

			if(search_card()==FOUND)			//search card on table
			open_door();

		}



	}




	
	return 0;
}
Esempio n. 25
0
/* =====================================================================
 * Periodically recalculate some submarine parameters, with a larger delay
 * =====================================================================*/
static gboolean update_timeout_slow() {
  gdouble delta_assiette;

  if(!boardRootItem)
    return FALSE;

  if(board_paused)
    return TRUE;

  /* speed : don't reach instantly the ordered speed */
  if (speed_ordered != submarine_horizontal_speed) {
    submarine_horizontal_speed += (speed_ordered-submarine_horizontal_speed)/10.0;
    if (fabs(speed_ordered - submarine_horizontal_speed) < 0.1)
      submarine_horizontal_speed = speed_ordered;
  }

  /* assiette */
  delta_assiette = (ballast_ar_air - ballast_av_air)/200.0 +
    (barre_av_angle - barre_ar_angle)/5.0*submarine_horizontal_speed;
  assiette -= delta_assiette*UPDATE_DELAY/10000.0;
  if (assiette < -30.0)
    assiette = -30.0;
  if (assiette > 30.0)
    assiette = 30.0;

  /* If surfacing, diminish the 'assiette' */
  if ( depth <= 5.0 + SURFACE_DEPTH) {
    assiette *= depth/(depth+1.0);
  }

  /* update some dynamic parameters */
  /* resulting_weight > 0 ==> the sub goes deeper
     regleur : this is the qty of water */
  resulting_weight = weight - ballast_av_air - ballast_ar_air + regleur;
  submarine_vertical_speed = resulting_weight/300.0
    + submarine_horizontal_speed*sin(DEG_TO_RAD(-assiette));

  /* if depth rudders are in the same direction */
  if (barre_ar_angle != 0.0 && barre_av_angle != 0.0) {
    if (fabs(barre_ar_angle)/barre_ar_angle == fabs(barre_av_angle)/barre_av_angle) {
      gdouble a = (fabs(barre_ar_angle) > fabs(barre_av_angle)) ? barre_av_angle : barre_ar_angle;
      submarine_vertical_speed += a * submarine_horizontal_speed/30.0;
    }
  }

  /* position & depth */
  submarine_x += submarine_horizontal_speed
    * cos(DEG_TO_RAD(assiette))
    * UPDATE_DELAY_SLOW/1000.0;

  depth += submarine_vertical_speed * UPDATE_DELAY_SLOW/1000.0;

  if (depth < SURFACE_DEPTH)
    depth = SURFACE_DEPTH;
  if (depth > MAX_DEPTH)
    depth = MAX_DEPTH;

  // show an alert if some parameters reach the limit
  if (depth >= MAX_DEPTH-20.0 || assiette == -30.0
      || assiette == 30.0 || air == 0.0 || battery == 0.0)
    g_object_set (alert_submarine,
		  "visibility", GOO_CANVAS_ITEM_VISIBLE,
		  NULL);
  else
    g_object_set (alert_submarine,
		  "visibility", GOO_CANVAS_ITEM_INVISIBLE,
		  NULL);

  /* if the submarine dives, stop charging air tanks and batteries */
  if ( depth >= SURFACE_DEPTH+10.0 ) {
    if (air_charging) {
      air_charging = FALSE;
      gc_item_rotate_with_center(air_compressor_item, 0,
				 TRIGGER_CENTER_X, TRIGGER_CENTER_Y );
    }
    if (battery_charging) {
      battery_charging = FALSE;
      gc_item_rotate_with_center(battery_charger_item, 0,
				 TRIGGER_CENTER_X, TRIGGER_CENTER_Y );
    }
  }

  /* if the submarine is too close from right, put it on the left */
  if ( submarine_x > WRAP_X )
    {
      /* Check its within the gate range */
      GooCanvasBounds bounds;

      goo_canvas_item_get_bounds (submarine_item, &bounds);

      if(bounds.y1 < gate_top_current_y ||
	 bounds.y2 > gate_bottom_y)
	{
	  /* It's a crash */
	  submarine_explosion();
	}
      else
	{
	  gamewon = TRUE;
	  /* Let the user play indefinitly at level 3 */
	  if(gcomprisBoard->level<3)
	    gc_bonus_display(gamewon, GC_BONUS_SMILEY);
	  else
	    {
	      submarine_x = SUBMARINE_INITIAL_X;
	      depth = SUBMARINE_INITIAL_DEPTH;
	    }
	}
    }

  /* Open the door */
  if(treasure_captured && gate_top_current_y > gate_top_y)
    open_door();

  /* display the submarine */
  goo_canvas_item_set_simple_transform(submarine_item,
				       submarine_x - submarine_width/2.0,
				       depth + SURFACE_DEPTH - submarine_height/2.0,
				       1, -assiette);

  /* the frigate */
  {
    GooCanvasBounds bounds;
    goo_canvas_item_get_bounds(frigate_item, &bounds);
    //goo_canvas_item_translate(frigate_item, - FRIGATE_SPEED * UPDATE_DELAY_SLOW/1000.0, 0.0);
    /* detects a collision between the frigate and the submarine */
    if (depth <= 30.0 && !submarine_destroyed)
      if ( (submarine_x - submarine_width <= bounds.x1 && submarine_x >= bounds.x2) ||
	   (submarine_x - submarine_width >= bounds.x1 && submarine_x - submarine_width <= bounds.x2) ||
	   (submarine_x >= bounds.x1 && submarine_x <= bounds.x2) ) {
        submarine_explosion();
      }
    /* wraps the destroyer if it reached the left side (and disappeared for a long time)*/
    //if (bounds.x2 < -300.0)
    //gc_item_absolute_move( frigate_item, BOARDWIDTH, bounds.y1 );
  }

  /* whale detection */
  {
    gdouble dist1, dist2, dist3;
    dist1 = hypot( submarine_x -submarine_width/2 -whale_x,
		   depth+SURFACE_IN_BACKGROUND-whale_y);
    dist2 = hypot(submarine_x - submarine_width - whale_x,
		  depth+SURFACE_IN_BACKGROUND-whale_y);
    dist3 = hypot(submarine_x - whale_x,
		  depth+SURFACE_IN_BACKGROUND-whale_y);
    /* magnetic detection (dist1) or collision with the whale (dist2 & dist3) */
    if ( (dist1 < WHALE_DETECTION_RADIUS
	  || dist2 < WHALE_DETECTION_RADIUS
	  || dist3 < WHALE_DETECTION_RADIUS)
	 && !submarine_destroyed ) {
      g_object_set (whale, "visibility", GOO_CANVAS_ITEM_INVISIBLE, NULL);
      g_object_set (big_explosion, "visibility", GOO_CANVAS_ITEM_VISIBLE, NULL);
      submarine_explosion();
    }
  }

  /* treasure detection */
  {
    gdouble dist1, dist2, dist3;
    dist1 = hypot( submarine_x -submarine_width/2 -treasure_x,
		   depth+SURFACE_IN_BACKGROUND-treasure_y);
    dist2 = hypot(submarine_x - submarine_width - treasure_x,
		  depth+SURFACE_IN_BACKGROUND-treasure_y);
    dist3 = hypot(submarine_x - treasure_x,
		  depth+SURFACE_IN_BACKGROUND-treasure_y);
    /* magnetic detection (dist1) or collision with the treasure (dist2 & dist3) */
    if ( (dist1 < TREASURE_DETECTION_RADIUS
	  || dist2 < TREASURE_DETECTION_RADIUS
	  || dist3 < TREASURE_DETECTION_RADIUS)
	 && !treasure_captured ) {
      gc_sound_play_ogg("sounds/tuxok.wav", NULL);
      g_object_set (treasure, "visibility", GOO_CANVAS_ITEM_INVISIBLE, NULL);
      treasure_captured = TRUE;
      open_door();
    }
  }

  return TRUE;
}
Esempio n. 26
0
/* =====================================================================
 * Periodically recalculate some submarine parameters, with a larger delay
 * =====================================================================*/
static gboolean update_timeout_slow() {
  gdouble delta_assiette;

  if(!boardRootItem)
    return FALSE;

  if(board_paused)
    return TRUE;

  /* speed : don't reach instantly the ordered speed */
  if (speed_ordered != submarine_horizontal_speed) {
    submarine_horizontal_speed += (speed_ordered-submarine_horizontal_speed)/10.0;
    if (fabs(speed_ordered - submarine_horizontal_speed) < 0.1)
      submarine_horizontal_speed = speed_ordered;
  }

  /* assiette */
  delta_assiette = (ballast_ar_air - ballast_av_air)/200.0 +
    (barre_av_angle - barre_ar_angle)/5.0*submarine_horizontal_speed;
  assiette -= delta_assiette*UPDATE_DELAY/10000.0;
  if (assiette < -30.0)
    assiette = -30.0;
  if (assiette > 30.0)
    assiette = 30.0;

  /* If surfacing, diminish the 'assiette' */
  if ( depth <= 5.0 + SURFACE_DEPTH) {
    assiette *= depth/(depth+1.0);
  }

  /* update some dynamic parameters */
  /* resulting_weight > 0 ==> the sub goes deeper
     regleur : this is the qty of water */
  resulting_weight = weight - ballast_av_air - ballast_ar_air + regleur;
  submarine_vertical_speed = resulting_weight/300.0
    + submarine_horizontal_speed*sin(DEG_TO_RAD(-assiette));

  /* if depth rudders are in the same direction */
  if (barre_ar_angle != 0.0 && barre_av_angle != 0.0) {
    if (fabs(barre_ar_angle)/barre_ar_angle == fabs(barre_av_angle)/barre_av_angle) {
      gdouble a = (fabs(barre_ar_angle) > fabs(barre_av_angle)) ? barre_av_angle : barre_ar_angle;
      submarine_vertical_speed += a * submarine_horizontal_speed/30.0;
    }
  }

  /* position & depth */
  submarine_x += submarine_horizontal_speed
    * cos(DEG_TO_RAD(assiette))
    * UPDATE_DELAY_SLOW/1000.0;

  depth += submarine_vertical_speed * UPDATE_DELAY_SLOW/1000.0;

  if (depth < SURFACE_DEPTH)
    depth = SURFACE_DEPTH;
  if (depth > MAX_DEPTH)
    depth = MAX_DEPTH;

  // show an alert if some parameters reach the limit
  if (depth >= MAX_DEPTH-20.0 || assiette == -30.0
      || assiette == 30.0 || air == 0.0 || battery == 0.0)
    g_object_set (alert_submarine,
		  "visibility", GOO_CANVAS_ITEM_VISIBLE,
		  NULL);
  else
    g_object_set (alert_submarine,
		  "visibility", GOO_CANVAS_ITEM_INVISIBLE,
		  NULL);

  /* if the submarine dives, stop charging air tanks and batteries */
  if ( depth >= SURFACE_DEPTH+10.0 ) {
    if (air_charging) {
      air_charging = FALSE;
      gc_item_rotate_with_center(air_compressor_item, 0,
				 TRIGGER_CENTER_X, TRIGGER_CENTER_Y );
    }
    if (battery_charging) {
      battery_charging = FALSE;
      gc_item_rotate_with_center(battery_charger_item, 0,
				 TRIGGER_CENTER_X, TRIGGER_CENTER_Y );
    }
  }

  /* Check the submarine passed the right door */
  if ( submarine_x > WRAP_X && !gamewon )
    {
      /* Check its within the gate range */
      GooCanvasBounds bounds;

      goo_canvas_item_get_bounds (submarine_item, &bounds);

      guint antena_height = 30;
      if(bounds.y1 + antena_height < gate_top_current_y ||
	 bounds.y2 > gate_bottom_y)
	{
	  /* It's a crash */
	  submarine_explosion();
	}
      else
	{
	  gamewon = TRUE;
	  ok();
	}
    }

  /* Open the door */
  if(treasure_captured && gate_top_current_y > gate_top_y)
    open_door();

  /* display the submarine */
  goo_canvas_item_set_simple_transform(submarine_item,
				       submarine_x - submarine_width/2.0,
				       depth + SURFACE_DEPTH - submarine_height/2.0,
				       1, -assiette);

  /* the frigate */
  if ( frigate_item ) {
    /* detects a collision between the frigate and the submarine */
    if (depth <= 30.0 && !submarine_destroyed) {
      GooCanvasBounds bounds;
      goo_canvas_item_get_bounds(frigate_item, &bounds);
      gdouble frigate_x = bounds.x1 + (bounds.x2 - bounds.x1) / 2;
      if ( abs(submarine_x - frigate_x) < 100 ) {
        submarine_explosion();
	return TRUE;
      }
    }
  }

  /* whale detection */
  {
    gdouble dist1 = hypot( submarine_x - whale_x,  depth - whale_y);
    if ( ( dist1 < WHALE_DETECTION_RADIUS ) && !submarine_destroyed ) {
      g_object_set (whale, "visibility", GOO_CANVAS_ITEM_INVISIBLE, NULL);
      g_object_set (big_explosion, "visibility", GOO_CANVAS_ITEM_VISIBLE, NULL);
      submarine_explosion();
      return TRUE;
    }
  }

  /* treasure detection */
  {
    gdouble dist1 = hypot( submarine_x - treasure_x, depth - treasure_y);
    if ( (dist1 < TREASURE_DETECTION_RADIUS)
	 && !treasure_captured ) {
      gc_sound_play_ogg("sounds/tuxok.wav", NULL);
      g_object_set (treasure, "visibility", GOO_CANVAS_ITEM_INVISIBLE, NULL);
      treasure_captured = TRUE;
      open_door();
    }
  }

  return TRUE;
}
Esempio n. 27
0
void *elevator(void *arg){
    Elevator *e = (Elevator *) arg; 
    Dllist item, next, pickup;
    Person *p;
    int direction = 1;
    pickup = new_dllist();

    while(1){

        if(e -> onfloor >= top) direction = -1;
        else if(e -> onfloor <= 1) direction = 1;
        //printf("\tElevator[%i] on floor %i going %s:\n",
        //        e -> id, e -> onfloor, direction == 1 ? "up": "down"); 

        /* pick people up */
        pthread_mutex_lock(lock);

        item = dll_first(people);
        while(!dll_empty(people) && item != dll_nil(people)){
            next = dll_next(item);
            p = (Person *) item -> val.v;

            //printf("\t\tShould I get %s %s going from %i to %i? ",
            //        p -> fname, p -> lname, p -> from, p -> to);

            if(e -> onfloor == p -> from){
                if(p -> to > e -> onfloor && direction == 1 || 
                   p -> to < e -> onfloor && direction == -1){
                    dll_append(pickup, item -> val);
                    dll_delete_node(item);
                    //printf("yes!\n");

                }
                //else printf("no!\n");
            }
            //else printf("no!\n");

            item = next;
        }

        pthread_mutex_unlock(lock);

        item = dll_first(pickup);
        while(!dll_empty(pickup) && item != dll_nil(pickup)){
            next = dll_next(item);
            p = (Person *) item -> val.v;

            if(!e -> door_open) open_door(e);
            pthread_mutex_lock(p -> lock);

            p -> e = e;

            pthread_cond_signal(p -> cond);
            pthread_mutex_lock(e -> lock);
            pthread_mutex_unlock(p -> lock);

            pthread_cond_wait(e -> cond, e -> lock);
            pthread_mutex_unlock(e -> lock);

            dll_delete_node(item);
            item = next;
        }
        if(e -> door_open) close_door(e);

        move_to_floor(e, e -> onfloor + direction);

        /* drop people off */
        item = dll_first(e -> people);
        while(!dll_empty(e -> people) && item != dll_nil(e -> people)){
            next = dll_next(item);
            p = (Person *) item -> val.v;

            if(p -> to == e -> onfloor){
               if(!e -> door_open) open_door(e);
                
               pthread_mutex_lock(p -> lock);
               pthread_cond_signal(p -> cond);
               pthread_mutex_lock(e -> lock);
               pthread_mutex_unlock(p -> lock);

               pthread_cond_wait(e -> cond, e -> lock);
               pthread_mutex_unlock(e -> lock);
            }

            item = next;
        }
        //if(e -> door_open) close_door(e);

    }

    return NULL;
}
Esempio n. 28
0
int do_open (string str)
{
  unlock_door ("east");
  open_door   ("east");
  return 1;
}
Esempio n. 29
0
void* goto_floor_with_network(int targetFloor){
  // Baserer meg her på at heis befinner seg i etasje
  //Try to reconnect
  //If orders in orderlist
  int currentFloor;
  int directionIndex;
  int flag = 0;


  // Funksjoner som sjekker bestillinger kan implementeres som tråder og fjernes fra tilstandsmaskiner



  printf("Searching for orders\n");

  //While løkke kjører så lenge bestillingslister har elementer i seg
    printf("Element in ButtonVector, internalButtonVector: %i externalButtonVector: %i\n", !check_if_internalButtonVector_is_empty(), !check_if_externalButtonVector_is_empty() );
    print_button_vectors();

    currentFloor = elev_get_floor_sensor_signal();
    lastFloor = currentFloor;

    printf("TargetFloor etter end_order_in_direction %i \n", targetFloor);
    direction = get_direction(currentFloor, targetFloor);
    directionIndex = direction_to_index(direction); // Til bruk når vi aksesser externalButtonVector

    printf("Direction er %i directionIndex er %i\n", direction, directionIndex );
    set_motor_direction(lastFloor, targetFloor); 



    time_t start, end;
  	double elapsed = 0;
  	time(&start);

    while(currentFloor != targetFloor){ //Heis beveger seg mot targetFloor i gitt retning
    	
    	 if (elapsed  > 5 ){
    		connectStatus = 0;
    		printf("Motor Disconnected\n");

    		 while(!((currentFloor != -1)  && (currentFloor != lastFloor) )){
    		 	
    		 	elapsed = 0;
    		 	 time(&start);
    			 currentFloor = elev_get_floor_sensor_signal();

    		 }    	
    		 connectStatus = 1;
    		 printf("Motor connected\n");
    		}
    	else{
    		connectStatus = 1;
    		time(&end);
           
    	} 
    	 elapsed = difftime(end, start);


      if((currentFloor != -1)  && (currentFloor != lastFloor) ){ //Elevator have reached new floor
      	time(&start);


        lastFloor = currentFloor;
        elev_set_floor_indicator(lastFloor);

        if (internalButtonVector[currentFloor]){ 
              pthread_mutex_lock(&elevLock);
          internalButtonVector[currentFloor] = 0;
          	    pthread_mutex_unlock(&elevLock);

          elev_set_motor_direction(DIRN_STOP);
          printf("Open door 1\n");
          
          elev_set_button_lamp(BUTTON_COMMAND, currentFloor, 0);
          open_door();   
          flag = 1;
      		}
        if(externalButtonVector[currentFloor][directionIndex] ){
           pthread_mutex_lock(&elevLock);
          externalButtonVector[currentFloor][directionIndex] = 0;

          send_order_handeled_confirmation(currentFloor, directionIndex);
            pthread_mutex_unlock(&elevLock);

		        if (flag != 1){
		        elev_set_motor_direction(DIRN_STOP);
		        printf("Open door 1\n");
		        elev_set_button_lamp(BUTTON_COMMAND, currentFloor, 0);
		        open_door();
		        flag = 0;
		    	}
        }

	      }



	      set_motor_direction(lastFloor, targetFloor);



	          pthread_mutex_lock(&elevLock);

	     	 targetFloor = end_order_in_direction(direction, targetFloor); // Oppdaterer targetfloor under reise
	     
	          pthread_mutex_unlock(&elevLock);

	     // printf("LastFloor er %i\n", lastFloor);
	      currentFloor = elev_get_floor_sensor_signal();
	   //   sleep(0.2); //Forsinke prints
    }

    //Exit while -> framme ved targetFloor
    if ( (currentFloor == targetFloor) ){ 

    	lastFloor = currentFloor;
        elev_set_floor_indicator(lastFloor);


      elev_set_motor_direction(DIRN_STOP);

      printf("Open door 2, framme ved targetFloor %i\n", targetFloor);


         



      

      open_door();
		elev_set_button_lamp(BUTTON_COMMAND, targetFloor, 0);
       pthread_mutex_lock(&elevLock);
        internalButtonVector[targetFloor] = 0;
      externalButtonVector[targetFloor][0] = 0;  // I denne bestillingsslettingen kan det fort ligge bug
      externalButtonVector[targetFloor][1] = 0;

		pthread_mutex_unlock(&elevLock);

	      send_order_handeled_confirmation(targetFloor, 0);
	      send_order_handeled_confirmation(targetFloor, 1);

    	direction = 0;
    }


  //  sleep(0.2);
  
  return NULL;
}
Esempio n. 30
0
void door_routine(void)
//This function opens the door and exits when the door is locked
{
	//variables for this function
	char pass[PASS_LENGTH],pass_2[PASS_LENGTH];
	int x,y,exit=0,loop; //x,y,temp are used to check return values from passcode_func. exit is used to hold infinite loop till door closed
	
	//initialisation
	for(loop=0;loop<PASS_LENGTH;++loop)
	{
		pass[loop]=PASS_NULL;
		pass_2[loop]=PASS_NULL; 
	}
	
	//first, open the door
	open_door();
	Write("OPEN");

	while(exit!=1) //function will only exit when door closed
	{
		if(key_flag==1)
		{
			if(key_pressed==KEY_CHANGE)
			{
				while(scan()==1); //debounce
				key_flag=0;
				Write("Chan"); 		//indicate that they've entered pass change mode
				delay_10ms(150); 	//delay before blanking screen
				Write("    ");
				
				//passcode change mode
				x=passcode_entering(pass,30);
				if(x==KEY_ENTER) //only continue if passcode_func exited due to enter being pressed
				{
					Write("    ");
					y=passcode_entering(pass_2,30);
					if(y==KEY_ENTER)
					{
						if(check_arrays(pass,pass_2))
						{
							pass_write(pass);
							Write("done"); 		//say 'done to indicate password changed.
						}
						else
						{
							Write("Err ");
						}
						delay_10ms(150); //1.5 second delay   >>Could change this to "wait for enter pressed."
					}
				}
				
				//clear values stored
				for(loop=0;loop<PASS_LENGTH;++loop)
				{
					pass[loop]=PASS_NULL;
					pass_2[loop]=PASS_NULL;
				}
				Write("OPEN");
			
			}
			
			if(key_pressed==KEY_ENTER)
			{
				while(scan()==1); //debounce
				key_flag=0;
				
				//now close the door
				close_door();
				Write("Shut");
				exit=1; //door has been closed, function can now exit.
			}
			
			if(key_pressed!=KEY_ENTER&&key_pressed!=KEY_CHANGE) //just to handle/clear random numeric key presses whilst door is open
			{
				while(scan()==1); //debounce
				key_flag=0;
			}
		}
	}//end of while loop
}