Conway's game of life applied to the mbed and an RA8875 LCD.

Dependencies:   LifeRules mbed RA8875

Inspired by a forum discussion on the mbed site, this version was scaled to support up to a 480x272 display - in a monochrome mode, or at a lower resolution in color (the color shows simple animation for birthing and dying cells).

Leveraging the LifeRules class, the game can be easily adapted to other displays - whether monochrome or color.

By default, this version allocates memory from the Ethernet ram banks, so avoids the memory limitations of some designs.

It should be simple to adapt it to any display - color or b&w, high or low resolution.

Revision:
8:d0e048eef5db
Parent:
7:f6d73bba9c4c
Child:
9:159df2630ad0
--- a/main.cpp	Sat Jan 23 17:26:49 2016 +0000
+++ b/main.cpp	Sat Aug 06 15:12:41 2016 +0000
@@ -1,10 +1,31 @@
 // game of life implementation inspired by this forum thread
 // http://mbed.org/forum/helloworld/topic/4822/
 // 
-#include "mbed.h"       // v112
-#include "RA8875.h"     // v102
+#include "mbed.h"       // v122
+#include "RA8875.h"     // v126
 #include "LifeRules.h"  // v3
 
+// These two defines can be enabled, or commented out
+//#define BIG_SCREEN
+//#define CAP_TOUCH
+#define LCD_C 16         // color - bits per pixel
+
+#ifdef CAP_TOUCH
+RA8875 lcd(p5, p6, p7, p12, NC, p9,p10,p13, "tft"); // MOSI,MISO,SCK,/ChipSelect,/reset, SDA,SCL,/IRQ, name
+#else
+RA8875 lcd(p5, p6, p7, p12, NC, "tft");             //MOSI, MISO, SCK, /ChipSelect, /reset, name
+LocalFileSystem local("local");                     // access to calibration file for resistive touch.
+#endif
+
+#ifdef BIG_SCREEN
+    #define LCD_W 800
+    #define LCD_H 480
+#else
+    #define LCD_W 480
+    #define LCD_H 272
+#endif
+
+
 // Define the life-map size
 #define LIFE_W 150
 #define LIFE_H 100
@@ -41,35 +62,25 @@
 
 Life life(LIFE_W, LIFE_H, LIFE_CLR);
 
-// NOT USING p21, but the mbed lib v82 will not work if NC is 
-// in the constructor. 
-// See thread http://mbed.org/forum/bugs-suggestions/topic/4859/
-RA8875 lcd(p5, p6, p7, p12, p21, "tft");
-
-// Define the screen size (may be fixed in the display driver)
-#define SCREEN_W 480
-#define SCREEN_H 272
-
 // Where on screen do we locate it?
-#if LIFE_W * LIFE_Z < SCREEN_W
+#if LIFE_W * LIFE_Z < LCD_W
 #define H_OFFSET -1
 #else
 #define H_OFFSET 0
 #endif
 
-#if LIFE_H * LIFE_Z < SCREEN_H
+#if LIFE_H * LIFE_Z < LCD_H
 #define V_OFFSET -1
 #else
 #define V_OFFSET 0
 #endif
 
-#define LIFE_OFFSET_X (SCREEN_W - (LIFE_W * LIFE_Z) + H_OFFSET)
-#define LIFE_OFFSET_Y (SCREEN_H - (LIFE_H * LIFE_Z) + V_OFFSET)
+#define LIFE_OFFSET_X (LCD_W - (LIFE_W * LIFE_Z) + H_OFFSET)
+#define LIFE_OFFSET_Y (LCD_H - (LIFE_H * LIFE_Z) + V_OFFSET)
 
 unsigned char imgbuffer[3*LIFE_W]; // from forum thread...
 FILE *img;
 
-LocalFileSystem local("local"); //file system
 Serial pc(USBTX,USBRX); // for debugging
 
 int msDelay = 1000;     //delay between frames
@@ -86,6 +97,8 @@
 void ScreenUpdate()
 {
     lcd.window(LIFE_OFFSET_X, LIFE_OFFSET_Y, LIFE_W * LIFE_Z, LIFE_H * LIFE_Z);
+    lcd.SetGraphicsCursor(LIFE_OFFSET_X+1, LIFE_OFFSET_Y+1);
+    pc.printf("window(%d,%d, %d,%d)\r\n", LIFE_OFFSET_X, LIFE_OFFSET_Y, LIFE_W * LIFE_Z, LIFE_H * LIFE_Z);
     lcd._StartGraphicsStream();
     for (int j = 0; j < LIFE_H; j++) {
         for (int Zx = 0; Zx < LIFE_Z; Zx++) {
@@ -130,7 +143,7 @@
         snprintf(fqfn, sizeof(fqfn), "/local/Screen%02d.bmp", i);
         FILE * fh = fopen(fqfn, "rb");
         if (!fh) {
-            lcd.PrintScreen(0,0,480,272,fqfn);
+            lcd.PrintScreen(0,0,LCD_W,LCD_H,fqfn);
             pc.printf(" as /local/Screen%02d.bmp\r\n", i);
             return i;
         } else {
@@ -147,7 +160,9 @@
     pc.printf("\r\nConway's Game of Life - Build " __DATE__ " " __TIME__ "\r\n");
 
     lcd.init();
-    lcd.frequency(5000000);
+    lcd.Backlight(0.5f);
+
+    //lcd.frequency(5000000);
     lcd.puts("Welcome to Conway's Game of Life\r\n\r\n");
     INFO("Destroy all life");
     life.DestroyAllLife();