void StepMotor::singleStep(bool fwd) {
  if (fwd) {
    phase++;
    if (phase==8) {
      phase=0;
    }
  }
  else {
    phase--;
    if (phase==-1) {
      phase=7;
    }
  }
  energize();
}
Beispiel #2
0
int
main (int argc, char *argv[])
{
  /* Process command line arguments */
  if (argc != 2)
    {
      fprintf (stderr, "Usage: %s <image path>\n", argv[0]);
      return (EXIT_FAILURE);
    }
  char *image_path = argv[1];

  /* Initialize subsystems */
  atexit (quit);
  if (SDL_Init (SDL_INIT_VIDEO))
    {
      fprintf (stderr, "Could not initialize SDL: %s\n", SDL_GetError ());
      return (EXIT_FAILURE);
    }
  char title[FILENAME_MAX];
  sprintf (title, "seamless - %s", image_path);
  SDL_WM_SetCaption (title, title);
  if (IMG_Init (0))
    {
      fprintf (stderr, "Could not initialize SDL_image: %s\n",
	       IMG_GetError ());
      return (EXIT_FAILURE);
    }

  /* Prepare drawing */
  SDL_Surface *image = IMG_Load (image_path);
  if (!image)
    {
      fprintf (stderr, "Could not load image %s: %s\n", image_path,
	       IMG_GetError ());
      return (EXIT_FAILURE);
    }
  SDL_Surface *screen =
    SDL_SetVideoMode (image->w, image->h, image->format->BitsPerPixel,
		      SDL_SWSURFACE);
  if (!screen)
    {
      fprintf (stderr, "Could not set video mode: %s\n", SDL_GetError ());
      return (EXIT_FAILURE);
    }


  /* Main loop */
  bool running = true;
  Uint32 black = SDL_MapRGB (screen->format, 0, 0, 0);
  SDL_Event event;
  float *array;
  char *pred;
  SDL_Surface *original;

  while (running)
    {
      while (SDL_PollEvent (&event))
	{
	  switch (event.type)
	    {
	    case SDL_QUIT:
	      running = false;
	      break;
            case SDL_KEYDOWN:
              switch (event.key.keysym.sym)
                {
                  case SDLK_q:
                  case SDLK_ESCAPE:
                    running = 0;
                    break;
                  case SDLK_RETURN:
                    for (int i = 0; i < 50; ++i)
                      {
                        array = energize (image, steepest_neighbor);
                        pred = dynamic_program (array, image->w, image->h);

                        original = image;
                        image = remove_seam (original,
                          min_pixel (array, original->w, original->h), pred);

                        SDL_FreeSurface (original);
                        free (array);
                        free (pred);
                      }
                    break;
                  default:
                    break;
                }
            }
	}

      SDL_FillRect (screen, NULL, black);

      SDL_BlitSurface (image, NULL, screen, NULL);

      SDL_Flip (screen);
    }

  return (EXIT_SUCCESS);
}