Browse Source

Fix 'logo', 'blobs' and 'kenney' use of 'xy' type. #7

Also added 'touching' and 'xytouch' to GD.inputs
jamesbowman 8 years ago
parent
commit
59bf8a4bae
4 changed files with 96 additions and 75 deletions
  1. 66 2
      GD2.cpp
  2. 16 46
      GD2.h
  3. 9 16
      blobs.ino
  4. 5 11
      logo.ino

+ 66 - 2
GD2.cpp

@@ -57,10 +57,72 @@ byte ft8xx_model;
 #include "transports/wiring.h"
 #endif
 
+////////////////////////////////////////////////////////////////////////
+
+void xy::set(int _x, int _y)
+{
+  x = _x;
+  y = _y;
+}
+
+void xy::rmove(int distance, int angle)
+{
+  x -= GD.rsin(distance, angle);
+  y += GD.rcos(distance, angle);
+}
+
+int xy::angleto(class xy &other)
+{
+  int dx = other.x - x, dy = other.y - y;
+  return GD.atan2(dy, dx);
+}
+
+void xy::draw(byte offset)
+{
+  GD.Vertex2f(x - PIXELS(offset), y - PIXELS(offset));
+}
+
+int xy::onscreen(void)
+{
+  return (0 <= x) &&
+         (x < PIXELS(GD.w)) &&
+         (0 <= y) &&
+         (y < PIXELS(GD.h));
+}
+
+class xy xy::operator+=(class xy &other)
+{
+  x += other.x;
+  y += other.y;
+  return *this;
+}
+
+int xy::nearer_than(int distance, xy &other)
+{
+  int lx = abs(x - other.x);
+  if (lx > distance)
+    return 0;
+  int ly = abs(y - other.y);
+  if (ly > distance)
+    return 0;
+
+  // trivial accept: 5/8 is smaller than 1/sqrt(2)
+  int d2 = (5 * distance) >> 3;
+  if ((lx < d2) && (ly < d2))
+    return 1;
+
+  return ((lx * lx) + (ly * ly)) < (distance * distance);
+}
+
+
+////////////////////////////////////////////////////////////////////////
+
 static GDTransport GDTR;
 
 GDClass GD;
 
+////////////////////////////////////////////////////////////////////////
+
 // The GD3 has a tiny configuration EEPROM - AT24C01D
 // It is programmed at manufacturing time with the setup
 // commands for the connected panel. The SCL,SDA lines
@@ -210,10 +272,10 @@ void GDClass::tune(void)
 }
 
 void GDClass::begin(uint8_t options) {
-#if STORAGE && defined(ARDUINO)
+#if defined(ARDUINO)
   GDTR.begin0();
 
-  if (options & GD_STORAGE) {
+  if (STORAGE && (options & GD_STORAGE)) {
     GDTR.ios();
     SD.begin(SD_PIN);
   }
@@ -1161,6 +1223,8 @@ void GDClass::get_inputs(void) {
   GDTR.rd_n(bi, REG_TRACKER, 4);
   GDTR.rd_n(bi + 4, REG_TOUCH_RZ, 13);
   GDTR.rd_n(bi + 17, REG_TAG, 1);
+  inputs.touching = (inputs.x != -32768);
+  inputs.xytouch.set(PIXELS(inputs.x), PIXELS(inputs.y));
 #ifdef DUMP_INPUTS
   for (size_t i = 0; i < sizeof(inputs); i++) {
     Serial.print(bi[i], HEX);

+ 16 - 46
GD2.h

@@ -406,6 +406,20 @@ public:
 
 ////////////////////////////////////////////////////////////////////////
 
+class xy {
+public:
+  int x, y;
+  void set(int _x, int _y);
+  void rmove(int distance, int angle);
+  int angleto(class xy &other);
+  void draw(byte offset = 0);
+  int onscreen(void);
+  class xy operator+=(class xy &other);
+  int nearer_than(int distance, xy &other);
+};
+
+////////////////////////////////////////////////////////////////////////
+
 class GDClass {
 public:
   int w, h;
@@ -445,6 +459,8 @@ public:
     int16_t tag_x;
     uint8_t tag;
     uint8_t ptag;
+    uint8_t touching;
+    xy xytouch;
   } inputs;
 
   void AlphaFunc(byte func, byte ref);
@@ -1053,52 +1069,6 @@ class Poly {
     }
 };
 
-class xy {
-public:
-  int x, y;
-  void set(int _x, int _y) {
-    x = _x;
-    y = _y;
-  }
-  void rmove(int distance, int angle) {
-    x -= GD.rsin(distance, angle);
-    y += GD.rcos(distance, angle);
-  }
-  int angleto(class xy &other) {
-    int dx = other.x - x, dy = other.y - y;
-    return GD.atan2(dy, dx);
-  }
-  void draw(byte offset = 0) {
-    GD.Vertex2f(x - PIXELS(offset), y - PIXELS(offset));
-  }
-  int onscreen(void) {
-    return (0 <= x) &&
-           (x < PIXELS(GD.w)) &&
-           (0 <= y) &&
-           (y < PIXELS(GD.h));
-  }
-  class xy operator+=(class xy &other) {
-    x += other.x;
-    y += other.y;
-    return *this;
-  }
-  int nearer_than(int distance, xy &other) {
-    int lx = abs(x - other.x);
-    if (lx > distance)
-      return 0;
-    int ly = abs(y - other.y);
-    if (ly > distance)
-      return 0;
-
-    // trivial accept: 5/8 is smaller than 1/sqrt(2)
-    int d2 = (5 * distance) >> 3;
-    if ((lx < d2) && (ly < d2))
-      return 1;
-
-    return ((lx * lx) + (ly * ly)) < (distance * distance);
-  }
-};
-
 #if SDCARD
 class Streamer {
 public:

+ 9 - 16
blobs.ino

@@ -3,20 +3,16 @@
 #include <GD2.h>
 
 #define NBLOBS      128 //' a{
-#define OFFSCREEN   -16384
 
-struct xy {
-  int x, y;
-} blobs[NBLOBS];
+xy blobs[NBLOBS];
+const xy offscreen = {-16384, -16384};
 
 void setup()
 {
   GD.begin();
 
-  for (int i = 0; i < NBLOBS; i++) {
-    blobs[i].x = OFFSCREEN;
-    blobs[i].y = OFFSCREEN;
-  }
+  for (int i = 0; i < NBLOBS; i++)
+    blobs[i] = offscreen;
   Serial.begin(1000000);    // JCB
 } //' }a
 
@@ -24,13 +20,10 @@ void loop() //' b{
 {
   static byte blob_i;
   GD.get_inputs();
-  if (GD.inputs.x != -32768) {
-    blobs[blob_i].x = GD.inputs.x << 4; //' xy{
-    blobs[blob_i].y = GD.inputs.y << 4; //' }xy
-  } else {
-    blobs[blob_i].x = OFFSCREEN;
-    blobs[blob_i].y = OFFSCREEN;
-  }
+  if (GD.inputs.x != -32768)
+    blobs[blob_i] = GD.inputs.xytouch;
+  else
+    blobs[blob_i] = offscreen;
   blob_i = (blob_i + 1) & (NBLOBS - 1);
 
   GD.ClearColorRGB(0xd0d0c0); // JCB
@@ -59,7 +52,7 @@ void loop() //' b{
     GD.ColorRGB(r, g, b);
 
     // Draw it!
-    GD.Vertex2f(blobs[j].x, blobs[j].y);
+    blobs[j].draw();
   }
   GD.swap();
 } //' }b

+ 5 - 11
logo.ino

@@ -20,20 +20,14 @@ byte clamp255(int x)
   return x;
 }
 
-struct xy {
-  int x, y;
-};
-
 #define NSTARS 256
 
 void loop()
 {
   byte fade;
   xy stars[NSTARS];
-  for (int i = 0; i < NSTARS; i++) {
-    stars[i].x = GD.random(16 * 480);
-    stars[i].y = GD.random(16 * 272);
-  }
+  for (int i = 0; i < NSTARS; i++)
+    stars[i].set(GD.random(PIXELS(GD.w)), GD.random(PIXELS(GD.h)));
 
   for (int t = 0; t < 464; t++) {
     GD.cmd_gradient(0, 0, 0x120000, 0, 272, 0x480000);
@@ -42,12 +36,12 @@ void loop()
     for (int i = 0; i < NSTARS; i++) {
       GD.ColorA(64 + (i >> 2));
       GD.PointSize(8 + (i >> 5));
-      GD.Vertex2f(stars[i].x, stars[i].y);
+      stars[i].draw();
       // stars drift left, then wrap around
       stars[i].x -= 1 + (i >> 5);
       if (stars[i].x < -256) {
-        stars[i].x += (16 * 500);
-        stars[i].y = GD.random(16 * 272);
+        stars[i].x += PIXELS(GD.w + 20);
+        stars[i].y = GD.random(PIXELS(GD.h));
       }
     }
     GD.RestoreContext();