Browse Source

video playback examples #15

James Bowman 6 years ago
parent
commit
bd083107c7
3 changed files with 166 additions and 1 deletions
  1. 62 1
      GD2.h
  2. 16 0
      video1.ino
  3. 88 0
      video2.ino

+ 62 - 1
GD2.h

@@ -1282,9 +1282,70 @@ public:
   }
 };
 
+class MoviePlayer
+{
+  uint32_t mf_size, mf_base, wp;
+  Reader r;
+  void loadsector() {
+    byte buf[512];
+    GD.__end();
+    r.readsector(buf);
+    GD.resume();
+    GD.wr_n(mf_base + wp, buf, 512);
+    wp = (wp + 512) & (mf_size - 1);
+  }
+
+public:
+  int begin(const char *filename) {
+    mf_size = 0x40000UL;
+    mf_base = 0x100000UL - mf_size;
+    GD.__end();
+    if (!r.openfile(filename)) {
+      Serial.println("Open failed");
+      return 0;
+    }
+    GD.resume();
+
+    wp = 0;
+    while (wp < (mf_size - 512)) {
+      loadsector();
+    }
+
+    GD.cmd_mediafifo(mf_base, mf_size);
+    GD.cmd_regwrite(REG_MEDIAFIFO_WRITE, wp);
+    GD.finish();
+
+    return 1;
+  }
+  int service() {
+    if (r.eof()) {
+      return 0;
+    } else {
+      byte buf[512];
+
+      uint32_t fullness = (wp - GD.rd32(REG_MEDIAFIFO_READ)) & (mf_size - 1);
+      Serial.println(fullness);
+      while (fullness < (mf_size - 512)) {
+        loadsector();
+        fullness += 512;
+        GD.wr32(REG_MEDIAFIFO_WRITE, wp);
+      }
+      return 1;
+    }
+  }
+  void play() {
+    GD.cmd_playvideo(OPT_MEDIAFIFO | OPT_FULLSCREEN);
+    GD.flush();
+    while (service())
+      ;
+    GD.cmd_memcpy(0, 0, 4);
+    GD.finish();
+  }
+};
+
 /*
  * PROGMEM declarations are currently not supported by the ESP8266
- * comppiler. So redefine PROGMEM to nothing.
+ * compiler. So redefine PROGMEM to nothing.
  */
 
 #if defined(ESP8266)

+ 16 - 0
video1.ino

@@ -0,0 +1,16 @@
+#include <EEPROM.h>
+#include <SPI.h>
+#include <GD2.h>
+
+void setup()
+{
+  GD.begin();
+}
+
+void loop()
+{
+  MoviePlayer mp;
+
+  mp.begin("fun-1500.avi");
+  mp.play();
+}

+ 88 - 0
video2.ino

@@ -0,0 +1,88 @@
+#include <EEPROM.h>
+#include <SPI.h>
+#include <GD2.h>
+
+MoviePlayer mp;
+
+class Dirsearch {
+  struct dirent de;
+  int index;
+  
+public:
+  char name[13];
+  void begin() {
+    index = 0;
+  }
+  int get(const char *ext) {
+    byte i;
+
+    GD.__end();
+    char e3[3];
+
+    do {
+      GD.SD.rdn((byte*)&de, GD.SD.o_root + index++ * 32, sizeof(de));
+      for (i = 0; i < 3; i++)
+        e3[i] = tolower(de.ext[i]);
+    } while (de.name[0] &&
+             ((de.name[0] & 0x80) || (memcmp(ext, e3, 3) != 0)));
+
+    GD.resume();
+
+    char *pc = name;
+    for (i = 0; i < 8 && de.name[i] != ' '; i++)
+      *pc++ = tolower(de.name[i]);
+    *pc++ = '.';
+    for (i = 0; i < 3 && de.ext[i] != ' '; i++)
+      *pc++ = tolower(de.ext[i]);
+    *pc++ = 0;
+    Serial.println(de.name[0], HEX);
+    Serial.println(name);
+
+    return de.name[0];
+  }
+};
+  
+Dirsearch ds;
+
+char *pickfile(char *ext)
+{
+  GD.Clear();
+  ds.begin();
+  for (byte i = 0; ds.get(ext); i++) {
+    int x = (i % 3) * 160;
+    int y = (i / 3) * 68;
+    GD.Tag(i + 1);
+    GD.cmd_button(x, y, 150, 60, 27, OPT_FLAT, ds.name);
+  }
+  GD.swap();
+
+  do {
+    GD.get_inputs();
+  } while (!GD.inputs.tag);
+
+  ds.begin();
+  for (byte i = 0; i <= (GD.inputs.tag - 1); i++)
+    ds.get(ext);
+  Serial.println(GD.inputs.tag);
+  Serial.println(ds.name);
+  return ds.name;
+}
+
+void setup()
+{
+  Serial.begin(1000000); // JCB
+  GD.begin();
+}
+
+void loop()
+{
+  char *avi = pickfile("avi");
+
+  GD.Clear();
+  GD.cmd_text(GD.w / 2, GD.h / 2 - 30, 31, OPT_CENTER, "playing");
+  GD.cmd_text(GD.w / 2, GD.h / 2 + 30, 31, OPT_CENTER, avi);
+  GD.swap();
+
+  mp.begin(avi);
+  mp.play();
+}