So here is my codes;
I’ve got a full screen background image that is loaded into the LCD buffer once. Then many small squares are drawn on top. Touching one of these squares changes it’s state and a different colour is displayed.
Interaction with the grid is slow and the screen only updates a couple of times a second. Really only very small pieces of the screen need to be updated at any time but i’ve not worked out how to do that yet.
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using GHIElectronics.NETMF.Hardware;
using System.Threading;
namespace BopperSequencer
{
public class Program : Microsoft.SPOT.Application, IEventListener
{
static Bitmap bs = Resources.GetBitmap(Resources.BitmapResources.blue_small);
static Bitmap bl = Resources.GetBitmap(Resources.BitmapResources.blue_large);
static Bitmap rl = Resources.GetBitmap(Resources.BitmapResources.red_large);
static Bitmap rs = Resources.GetBitmap(Resources.BitmapResources.red_small);
private static byte currentEditChannel = 0;
private static Channel[] channels = new Channel[16];
static int tx, ty, ticker;
public bool OnEvent(BaseEvent ev)
{
TouchEvent touchEvent = ev as TouchEvent;
if (touchEvent != null)
{
tx = touchEvent.Touches[0].X;
ty = touchEvent.Touches[0].Y;
if (tx > 18 && ty > 64 && tx < (18 + (16 * 18)) && ty < (64 + (18 * 16)))
{
tx -= 18;
ty -= 64;
tx /= 18;
ty /= 18;
//Debug.Print("FLIP CELL AT " + tx + " / " + ty);
flipCellAt(tx, ty);
}
}
return true;
}
public static void Main()
{
// The start-up logo will take effect after the first reset
Bitmap logo = Resources.GetBitmap(Resources.BitmapResources.splash_logo);
Configuration.StartUpLogo.Set(logo.GetBitmap(), (SystemMetrics.ScreenWidth - logo.Width) / 2, (SystemMetrics.ScreenHeight - logo.Height) / 2);
Configuration.LCD.EnableLCDBootupMessages(false);
Program myApplication = new Program();
LCD = myApplication.CreateLCD();
// Create the object that configures the GPIO pins to buttons.
GPIOButtonInputProvider inputProvider = new GPIOButtonInputProvider(null);
Touch.Initialize(myApplication);
const int CAL_POINTS = 5;
short[] sx = new short[CAL_POINTS] { 400, 80, 80, 720, 720 };
short[] sy = new short[CAL_POINTS] { 240, 48, 432, 432, 48 };
short[] cx = new short[CAL_POINTS] { 510, 159, 160, 846, 855 };
short[] cy = new short[CAL_POINTS] { 494, 233, 765, 754, 236 };
Microsoft.SPOT.Touch.Touch.ActiveTouchPanel.SetCalibration(CAL_POINTS, sx, sy, cx, cy);
channels[0] = new Channel(0);
while (true)
{
ticker++;
if (ticker == 16) ticker = 0;
//draw main grid
for (int y = 0; y < 16; y++)
{
for (int x = 0; x < 16; x++)
{
if (isCurrentChannelCellOn(x, y))
LCD.DrawImage(18 + x * 18, 64 + y * 18, rs, 0, 0, 16, 16);
else
LCD.DrawImage(18 + x * 18, 64 + y * 18, bs, 0, 0, 16, 16);
}
}
//draw the limiter
for (int x = 0; x < 16; x++)
{
LCD.DrawImage(18 + x * 18, 33, rs, 0, 0, 16, 16);
}
//draw the ticker
for (int x = 0; x < 16; x++)
{
if (ticker == x)
LCD.DrawImage(18 + x * 18, 64 + 288 + 13,rs,0,0, 16, 16);
else
LCD.DrawImage(18 + x * 18, 64 + 288 + 13, rs, 0, 0, 16, 16);
}
//draw pattern grid
for (int y = 0; y < 4; y++)
{
for (int x = 0; x < 8; x++)
{
LCD.DrawImage(18 + x * (36), 64 + 288 + 13 + 28 + (y * 18), rl, 0, 0, 34, 16);
}
}
LCD.Flush(0,0,400,400);
}
}
private static bool isCurrentChannelCellOn(int x, int y)
{
return channels[currentEditChannel].getCurrentFrame().getCell(x, y).state == Cell.CELL_NOTE_ON;
}
private static void flipCellAt(int x, int y)
{
Cell c = channels[currentEditChannel].getCurrentFrame().getCell(x, y);
if (c.state == Cell.CELL_NOTE_ON)
c.state = Cell.CELL_NOTE_OFF;
else
c.state = Cell.CELL_NOTE_ON;
}
private static Bitmap LCD;
private Bitmap CreateLCD()
{
Bitmap LCD = Resources.GetBitmap(Resources.BitmapResources.background);
return LCD;
}
private void OnButtonUp(object sender, RoutedEventArgs evt)
{
ButtonEventArgs e = (ButtonEventArgs)evt;
// Print the button code to the Visual Studio output window.
Debug.Print(e.Button.ToString());
}
}
}