Beispiel #1
0
  /**
   * Display a single word statically on the display. In order to display the string correctly it
   * should be about 6 characters or less, otherwise the string will not display completely before
   * you start moving in the opposite direction. In these cases, some characters may display
   * backwards.
   *
   * @param string The string to display
   * @param count how many swings to display it
   */
  public void swingThis(String string, int count) {
    int i = 0;
    double threshold = 1.0;
    try {
      while (i < count) {
        // wait for motion
        double acceleration = accelerometer.getAccelY();
        // System.out.println("accelerometer =" + acceleration +
        // " range = " + range );
        if (acceleration > threshold) {
          // we have acceleration right motion
          while (acceleration > threshold) {
            Thread.sleep(10);
            acceleration = accelerometer.getAccelY();
          }
          displayStringForward(string);
          i++;
          // System.out.println("time = " + (now-start) +
          // "fwadjust = " + forwardAdjust + ", Target = " + period);
        } else if (acceleration < -threshold) {
          // we have motion to the left
          while (acceleration < -threshold) {
            Thread.sleep(10);
            acceleration = accelerometer.getAccelY();
          }
          displayStringBackward(string);
          i++;
        }

        Thread.yield(); // so the SpotMonitor thread won't starve
      }
    } catch (IOException ex) {
      ex.printStackTrace();
    } catch (InterruptedException ex) {
      ex.printStackTrace();
    }
  }
Beispiel #2
0
  /**
   * Scroll a string across the swinging display
   *
   * @param string The string to display
   * @param scrollrate The speed to scroll at. To be readable, this should be a value from 1 to 10.
   */
  public void scroll(String string, int scrollrate) {
    long start = System.currentTimeMillis();
    long left = start;
    long right = start;
    int overhead = 140; // The magic values of overhead and gap
    int gap = 170; // were set empirically
    int i, j, offset = overhead;
    int len = string.length();
    int mapSize = 0;
    int[] bitmap;
    boolean lefting = false; // debug

    System.out.println("start scrolling " + string);

    // find the length of the bitmap
    for (i = 0; i < len; i++) {
      mapSize += (font.getCharWidth(string.charAt(i))) + 1;
    }

    // create a custom length array to hold the bitmap
    bitmap = new int[mapSize + (2 * overhead)];

    for (i = 0; i < bitmap.length; i++) bitmap[i] = 0;

    // preload the bitmap for the whole string
    // this is probably a bad idea memory-wse, but I'm in a hurry
    for (i = 0; i < len; i++) {
      int[] character = font.getChar(string.charAt(i));
      for (j = 0; j < character.length; j++) {
        bitmap[offset++] = character[j];
      }
      bitmap[offset++] = 0;
    }

    System.out.println("bitmap inited");
    offset = overhead - (gap - overhead);

    double threshold = 1.0;
    try {
      while (offset < bitmap.length - gap) {
        // wait for motion
        double acceleration = accelerometer.getAccelY();
        if (acceleration > threshold) {
          while (acceleration > threshold) {
            Thread.sleep(10);
            acceleration = accelerometer.getAccelY();
          }

          bltBitmapForward(bitmap, offset, gap - overhead);

          offset += scrollrate;

        } else if (acceleration < -threshold) {
          while (acceleration < -threshold) {
            Thread.sleep(10);
            acceleration = accelerometer.getAccelY();
          }
          bltBitmapBackward(bitmap, offset, gap - overhead);
          offset += scrollrate;
        }
        try {
          Thread.sleep(100);
        } catch (InterruptedException ex) {
          ex.printStackTrace();
        }
      }
      System.out.println(" finished scrolling " + gap);

    } catch (IOException ex) {
      ex.printStackTrace();
    } catch (InterruptedException ex) {
      ex.printStackTrace();
    }
  }