Example #1
0
int umain (void) {
	
	state = SETUP;
	
	while(1) {
		 switch (state)
		 {
			case (SETUP):
				setup_state();
				break;
		
			case (MOVING):
				moving_state();
				break;
		
			case (TURNING):
				turning_state();
				break;
				
			case (STOP):
				stop_state();
				break;
		 }
	
	}

	

	return 0;
}
Example #2
0
void run_iter(int n)
{
   int i;

   for (i = 0; i < n; i++) {
      setup_state();
      tk();
   }
}
Example #3
0
File: chat.c Project: rahpaere/tcpr
int main(int argc, char **argv)
{
    handle_options(argc, argv);
    setup_tcpr();
    setup_connection();
    setup_state();
    handle_events();
    teardown();
    return EXIT_SUCCESS;
}
Example #4
0
//----------------------"MAIN"-----------------
pthread_t setup_state_and_poll_thread(State * state, int argc, char ** argv) {
	if (!state) return 0;

	unsigned long id = ID;
	if (id) {
		setup_state(state, argc, argv, NULL);
	} else {
		struct json_object * request_config = create_json_request(argc, argv);

		struct String * message = perform_curl(NULL, POLL_CALL, PORT, 0, request_config);
		json_object_put(request_config);

		setup_state(state, argc, argv, message);

		if (!message) {
			printf("Could not obtain message, exiting\n");
			return 0;
		}

		id = parse_setup_response(message->data);

		pthread_mutex_init(&mID, NULL);
		setID(id);
	}

	pthread_t thread = 0;

	if (!id) {
		printf("No ID was retrieved from the response\n");
	} else {
		if (pthread_create(&thread, NULL, poll_t, state)) {
			thread = 0;
		}
	}
	return thread;
}
Example #5
0
int main(int argc, char **argv)
{
	// Setup
	setup_config(argc,argv);
	setup_state();
	setup_sigs();

	// Objects	
	logger.setup();
	server.setup();
	sensor.setup();
	heater.setup();

    // Thread
	if(pthread_create( &serverThread, NULL, serverPoll, NULL) != 0)
		logger.fail("Server thread creation failure");

	float chosenSetPoint;
	int sensorResult = 0;
	clock_t nextLoop;
	
	conf.update = 1000/conf.update;
	
	timeTick();
	nextLoop = state.time + conf.update;
    
 	logger.info("Starting up Coffeed");
 	
	while(state.run)
	{
		// Time update
		timeTick();

		if(state.time >= nextLoop)
		{
			// Get sensor data
			sensorResult = sensor.update();
		
			// If active choose set point
			if(state.active)
			{
				if(state.brewmode == TRUE)
					chosenSetPoint = conf.brewPoint;
				else
					chosenSetPoint = conf.steamPoint;
								
				// Sensor fault
				if(!sensorResult)
				{
					logger.fail("Sensor read failure");
					state.active = FALSE;
					heater.off();
				}
				// Tuning mode
				else if(state.tuning != FALSE)
				{
					// Begin
					if(state.tuning == 2)
					{
						state.tuning = TRUE;
						
						heater.setPower(50);
						aTune.cancel();
						//aTune.setNoiseBand(1);
						//aTune.setOutputStep(50);
						//aTune.setLookbackSec(20);
						//aTune.setControlType(1);
						
						logger.info("Autotuning BEGIN %f %f %f",conf.pgain,conf.igain,conf.dgain);
					}
					// While (1) we want to tune + (2) update is not done
					else if(state.tuning == TRUE && aTune.update() == FALSE)
					{
						logger.info("tuning");
					}
					// Tuning complete because state.tuning = TRUE but aTune == TRUE
					else
					{
						logger.info("tuning successful am I right?");
						// Done
						conf.pgain = aTune.getKp();
						conf.igain = aTune.getKi();
						conf.dgain = aTune.getKd();
						
						logger.info("Autotuning FINISH %f %f %f",conf.pgain,conf.igain,conf.dgain);
						
						state.tuning = FALSE;
					}					
				}
				// Regular control
				else
				{					
					// PID result
					state.pidResult = pid.result(chosenSetPoint, state.tempPoint);
					
					// Set Heater Duty
					heater.setPower( state.pidResult );
					
					// Log
					logger.debug("PID Result %d temperature %f setP %f brewP %f steamP %f", state.pidResult, state.tempPoint, chosenSetPoint, conf.brewPoint, conf.steamPoint);
				}
			}
			// Inactive system
			else
			{
				// Tuning can't happen in a inactive system
				if(state.tuning != FALSE)
				{
					state.tuning = FALSE;
					aTune.cancel();
				}
				
				if(state.brewmode)
					state.brewmode = TRUE;
				
				chosenSetPoint = 0;
				heater.off();
			}

			// Get Heater Duty
			// This should be centralized to the heater
			state.power = heater.getPower();
			
			// Schedule the next loop after the whole operation	
			nextLoop = state.time + conf.update;
		}
		else
		{
			// Sleep until we need you
			usleep(1000 * (nextLoop - state.time));
		}
	}
	
    logger.info("Shutting down Coffeed");
}
Example #6
0
int main(int argc, char **argv)
{
	int opt;

	while ((opt = getopt(argc, argv, "b:c:a:f:s:?")) != -1)
		switch (opt) {
		case 'b':
			split_address(optarg, &bind_host, &bind_port);
			break;
		case 'c':
			split_address(optarg, &connect_host, &connect_port);
			break;
		case 'a':
			split_address(optarg, &application_host,
						&application_port);
			break;
		case 'f':
			split_address(optarg, &filter_host, &filter_port);
			break;
		case 's':
			state_file = optarg;
			break;
		default:
			fprintf(stderr, "Usage: %s [OPTIONS]\n", argv[0]);
			fprintf(stderr, "  -b HOST:[PORT]  "
				"Bind to the specified address.\n");
			fprintf(stderr, "  -c HOST:[PORT]  "
				"Connect to the specified address.\n");
			fprintf(stderr, "  -a HOST:[PORT]  "
				"Receive updates at the specified address.\n");
			fprintf(stderr, "  -f HOST:[PORT]  "
				"Send updates to the specified address.\n");
			fprintf(stderr, "  -s FILE         "
				"Keep persistent state in FILE.\n");
			fprintf(stderr, "  -?              "
				"Print this help message and exit.\n");
			exit(EXIT_FAILURE);
		}

	if (!connect_port && connect_host)
		connect_port = "8888";
	if (!bind_port && (bind_host || !connect_port))
		bind_port = "8888";

	if (filter_host || filter_port) {
		filtering = 1;
		if (!filter_port)
			filter_port = application_port
					? application_port : "7777";
		if (!application_port)
			application_port = filter_port;
		if (!application_host)
			application_host = bind_host;
		if (!state_file)
			state_file = "tcpr-application.dat";
		setup_state();
		setup_update_connection();
	}

	if (recovering)
		recover_connection();
	else
		setup_connection();

	finish();
	return EXIT_SUCCESS;
}
Example #7
0
int main(int argc, char **argv)
{
	int opt;

	while ((opt = getopt(argc, argv, "b:c:a:f:s:p?")) != -1)
		switch (opt) {
		case 'b':
			split_address(optarg, &bind_host, &bind_port);
			break;
		case 'c':
			split_address(optarg, &connect_host, &connect_port);
			break;
		case 'a':
			application_path = optarg;
			break;
		case 'f':
			filter_path = optarg;
			break;
		case 's':
			state_file = optarg;
			break;
		case 'p':
			filtering = 0;
			break;
		default:
			fprintf(stderr, "Usage: %s [OPTIONS]\n", argv[0]);
			fprintf(stderr, "  -b HOST:[PORT]  "
				"Bind to HOST at PORT.\n");
			fprintf(stderr, "  -c HOST:[PORT]  "
				"Connect to HOST at PORT.\n");
			fprintf(stderr, "  -a PATH         "
				"Receive updates at the UNIX socket PATH.\n");
			fprintf(stderr, "  -f PATH         "
				"Send updates to the UNIX socket PATH.\n");
			fprintf(stderr, "  -s FILE         "
				"Keep persistent state in FILE.\n");
			fprintf(stderr, "  -p              "
				"Act as the peer; i.e. ignore TCPR.\n");
			fprintf(stderr, "  -?              "
				"Print this help message and exit.\n");
			exit(EXIT_FAILURE);
		}

	if (!connect_port && connect_host)
		connect_port = "8888";
	if (!bind_port && (bind_host || !connect_port))
		bind_port = "8888";

	if (filtering) {
		setup_state();
		setup_update_connection();
	}

	if (recovering)
		recover_connection();
	else
		setup_connection();

	finish();
	return EXIT_SUCCESS;
}
Example #8
0
int _al_draw_prim_indexed_opengl(ALLEGRO_BITMAP *target, ALLEGRO_BITMAP* texture, const void* vtxs, const ALLEGRO_VERTEX_DECL* decl, const int* indices, int num_vtx, int type)
{   
#ifdef ALLEGRO_CFG_OPENGL

   int num_primitives = 0;
   ALLEGRO_DISPLAY *ogl_disp = target->display;
   ALLEGRO_BITMAP_OGL *ogl_target = (ALLEGRO_BITMAP_OGL *)target;
   const void* vtx;
   const void* idx = indices;
   GLenum idx_size;

#if defined ALLEGRO_GP2XWIZ || defined ALLEGRO_IPHONE
   GLushort ind[num_vtx];
   int ii;
#endif

   if (target->parent) {
       ogl_target = (ALLEGRO_BITMAP_OGL *)target->parent;
   }

   if ((!ogl_target->is_backbuffer && ogl_disp->ogl_extras->opengl_target != ogl_target) || al_is_bitmap_locked(target)) {
      return _al_draw_prim_indexed_soft(texture, decl, vtxs, indices, num_vtx, type);
   }
   
   vtx = vtxs;

   _al_opengl_set_blender(ogl_disp);
   setup_state(vtx, decl, texture);
   
   if(texture) {
      glEnable(GL_TEXTURE_2D);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   }
  
#if defined ALLEGRO_GP2XWIZ || defined ALLEGRO_IPHONE
   for (ii = 0; ii < num_vtx; ii++) {
      ind[ii] = (GLushort)indices[ii];
   }
   idx = ind;
   idx_size = GL_UNSIGNED_SHORT;
#else
   idx_size = GL_UNSIGNED_INT;
#endif

   switch (type) {
      case ALLEGRO_PRIM_LINE_LIST: {
         glDrawElements(GL_LINES, num_vtx, idx_size, idx);
         num_primitives = num_vtx / 2;
         break;
      };
      case ALLEGRO_PRIM_LINE_STRIP: {
         glDrawElements(GL_LINE_STRIP, num_vtx, idx_size, idx);
         num_primitives = num_vtx - 1;
         break;
      };
      case ALLEGRO_PRIM_LINE_LOOP: {
         glDrawElements(GL_LINE_LOOP, num_vtx, idx_size, idx);
         num_primitives = num_vtx;
         break;
      };
      case ALLEGRO_PRIM_TRIANGLE_LIST: {
         glDrawElements(GL_TRIANGLES, num_vtx, idx_size, idx);
         num_primitives = num_vtx / 3;
         break;
      };
      case ALLEGRO_PRIM_TRIANGLE_STRIP: {
         glDrawElements(GL_TRIANGLE_STRIP, num_vtx, idx_size, idx);
         num_primitives = num_vtx - 2;
         break;
      };
      case ALLEGRO_PRIM_TRIANGLE_FAN: {
         glDrawElements(GL_TRIANGLE_FAN, num_vtx, idx_size, idx);
         num_primitives = num_vtx - 2;
         break;
      };
      case ALLEGRO_PRIM_POINT_LIST: {
         glDrawElements(GL_POINTS, num_vtx, idx_size, idx);
         num_primitives = num_vtx;
         break;
      };
   }

   if(texture) {
      glDisable(GL_TEXTURE_2D);
      glMatrixMode(GL_TEXTURE);
      glLoadIdentity();
      glMatrixMode(GL_MODELVIEW);
   }
   
   glDisableClientState(GL_COLOR_ARRAY);
   glDisableClientState(GL_VERTEX_ARRAY);
   glDisableClientState(GL_TEXTURE_COORD_ARRAY);

   return num_primitives;
#else
   (void)target;
   (void)texture;
   (void)vtxs;
   (void)decl;
   (void)indices;
   (void)num_vtx;
   (void)type;

   return 0;
#endif
}
Example #9
0
int _al_draw_prim_opengl(ALLEGRO_BITMAP* target, ALLEGRO_BITMAP* texture, const void* vtxs, const ALLEGRO_VERTEX_DECL* decl, int start, int end, int type)
{   
#ifdef ALLEGRO_CFG_OPENGL

   int num_primitives = 0;
   ALLEGRO_DISPLAY *ogl_disp = target->display;
   ALLEGRO_BITMAP_OGL *ogl_target = (ALLEGRO_BITMAP_OGL *)target;
   const void* vtx;
   int stride = decl ? decl->stride : (int)sizeof(ALLEGRO_VERTEX);
   int num_vtx;
   
   if (target->parent) {
       ogl_target = (ALLEGRO_BITMAP_OGL *)target->parent;
   }
  
   if ((!ogl_target->is_backbuffer && ogl_disp->ogl_extras->opengl_target != ogl_target) || al_is_bitmap_locked(target)) {
      return _al_draw_prim_soft(texture, vtxs, decl, start, end, type);
   }
   
   vtx = (const char*)vtxs + start * stride;
   num_vtx = end - start;

   _al_opengl_set_blender(ogl_disp);
   setup_state(vtx, decl, texture);
   
   if(texture) {
      glEnable(GL_TEXTURE_2D);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   }

   switch (type) {
      case ALLEGRO_PRIM_LINE_LIST: {
         glDrawArrays(GL_LINES, 0, num_vtx);
         num_primitives = num_vtx / 2;
         break;
      };
      case ALLEGRO_PRIM_LINE_STRIP: {
         glDrawArrays(GL_LINE_STRIP, 0, num_vtx);
         num_primitives = num_vtx - 1;
         break;
      };
      case ALLEGRO_PRIM_LINE_LOOP: {
         glDrawArrays(GL_LINE_LOOP, 0, num_vtx);
         num_primitives = num_vtx;
         break;
      };
      case ALLEGRO_PRIM_TRIANGLE_LIST: {
         glDrawArrays(GL_TRIANGLES, 0, num_vtx);
         num_primitives = num_vtx / 3;
         break;
      };
      case ALLEGRO_PRIM_TRIANGLE_STRIP: {
         glDrawArrays(GL_TRIANGLE_STRIP, 0, num_vtx);
         num_primitives = num_vtx - 2;
         break;
      };
      case ALLEGRO_PRIM_TRIANGLE_FAN: {
         glDrawArrays(GL_TRIANGLE_FAN, 0, num_vtx);
         num_primitives = num_vtx - 2;
         break;
      };
      case ALLEGRO_PRIM_POINT_LIST: {
         glDrawArrays(GL_POINTS, 0, num_vtx);
         num_primitives = num_vtx;
         break;
      };
   }

   if(texture) {
      glDisable(GL_TEXTURE_2D);
      glMatrixMode(GL_TEXTURE);
      glLoadIdentity();
      glMatrixMode(GL_MODELVIEW);
   }
   
   glDisableClientState(GL_COLOR_ARRAY);
   glDisableClientState(GL_VERTEX_ARRAY);
   glDisableClientState(GL_TEXTURE_COORD_ARRAY);

   return num_primitives;
#else
   (void)target;
   (void)texture;
   (void)vtxs;
   (void)decl;
   (void)start;
   (void)end;
   (void)type;

   return 0;
#endif
}