using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware; // 追加、参照設定にも追加
using GHI.OSHW.Hardware; // 追加、参照設定にも追加
using System.IO.Ports;
using BoeBotLib;
namespace BoeBotLib
{
class Color
{
public static int Black = 0;
public static int White = 0xffff;
public static int Red = 0xf800;
public static int Green = 0x07e0;
public static int Blue = 0x001f;
public static int Yellow = Red | Green;
public static int Magenta= Red | Blue;
public static int Cyan = Green | Blue;
}
// 基本的に以前のバージョンとは異なり、座標系がxとyは逆である(つまり、縦長)
// Iコマンドは以前の座標系である
class uOLED96G1
{
DigitalOut reset;
// Microsoft.SPOT.Hardware.
SerialPort serial;
byte LineX, LineY;
public uOLED96G1()
{
init("COM2", CPU.P22);
}
public uOLED96G1(String port, Cpu.Pin pin)
{
init(port, pin);
}
private void init(String port, Cpu.Pin resetPin)
{
reset = new DigitalOut(resetPin, false);
serial = new SerialPort(port, 250000, Parity.None, 8, StopBits.One);
serial.Open();
reset.Write(false); // reset
CPU.delay(1); // 1ms
reset.Write(true);
CPU.delay(400); // 推奨は4秒
int ch=0;
int count = 0;
do
{
serial.WriteByte((byte)'U'); // LCDイニシャライズ
if (serial.BytesToRead > 0) ch = serial.ReadByte();
CPU.delay(5);
if (count++ > 100)
{
count = 0;
reset.Write(false); // reset
CPU.delay(1); // 1ms
reset.Write(true);
CPU.delay(400); // 推奨は4秒
}
} while (ch != 0x06);
LineX = LineY=0;
}
public void EraseScreen()
{
serial.WriteByte((byte)'E');
waitAck();
}
public void VersionInfo()
{
byte[] cmd = { (byte)'V', 1 };
serial.Write(cmd, 0, cmd.Length);
int n = serial.BytesToRead;
if (n > 0)
{
for (int i = 0; i < 5; i++)
{
serial.ReadByte();
}
}
}
public void Print(byte x, byte y, String str, int color)
{
byte[] cmd = { (byte)'S', x, y, 0,(byte)(color>>8), (byte)(color&0xff), 1, 1 };
serial.Write(cmd, 0, cmd.Length);
for (int i = 0; i < str.Length; i++) serial.WriteByte((byte)str[i]);
serial.WriteByte(0);
waitAck();
}
public void Print(String str, int color)
{
if (LineY >= 96) LineY = 0;
Print(LineX, LineY, str, color);
LineY += 8;
}
public void Print(String str)
{
if (LineY >= 96)
{
LineY = 0;
EraseScreen();
}
Print(LineX, LineY, str, Color.White);
LineY += 8;
}
public void DrawLine(byte x1, byte y1, byte x2, byte y2, int color)
{
byte[] cmd = { (byte)'L', x1, y1, x2, y2, (byte)(color>>8), (byte)(color&0xff) };
serial.Write(cmd, 0, cmd.Length);
waitAck();
}
public void DrawCircle(byte x, byte y, byte rad, int color)
{
DrawCircle(x, y, rad, color, true);
}
public void DrawCircle(byte x, byte y, byte rad, int color, bool fill)
{
SetPenSize(fill ?(byte) 0 : (byte)1);
byte[] cmd = { (byte)'C', x, y, rad, (byte)(color >> 8), (byte)(color & 0xff) };
serial.Write(cmd, 0, cmd.Length);
waitAck();
}
public void SetPenSize(byte size)
{
byte[] cmd = { (byte)'p', size};
serial.Write(cmd, 0, cmd.Length);
}
public void DrawRectangle(byte x1, byte y1, byte x2, byte y2, int color)
{
DrawRectangle(x1, y1, x2, y2, color, true);
}
public void DrawRectangle(byte x1, byte y1, byte x2, byte y2, int color, bool fill)
{
SetPenSize(fill ? (byte)0 : (byte)1);
byte[] cmd = { (byte)'r', x1, y1, x2, y2, (byte)(color >> 8), (byte)(color & 0xff) };
serial.Write(cmd, 0, cmd.Length);
waitAck();
}
public void PutPixel(byte x, byte y, int color)
{
byte[] cmd = { (byte)'P', x, y, (byte)(color >> 8), (byte)(color & 0xff) };
serial.Write(cmd, 0, cmd.Length);
}
public void AddBitmap(byte Num, byte[] ch)
{
byte[] cmd = { (byte)'A', Num };
serial.Write(cmd, 0, cmd.Length);
serial.Write(ch, 0, 8);
}
public void DisplayBitmap(byte Num, byte x, byte y, int color)
{
byte[] cmd = { (byte)'D', Num, x, y, (byte)(color >> 8), (byte)(color & 0xff) };
serial.Write(cmd, 0, cmd.Length);
waitAck();
}
public void DrawPlygon(byte[] point, int color)
{
byte[] cmd = { (byte)'g', (byte)(point.Length/2)};
byte[] cmd2= { (byte)(color >> 8), (byte)(color & 0xff) };
serial.Write(cmd, 0, cmd.Length);
serial.Write(point, 0, point.Length);
serial.Write(cmd2, 0, cmd2.Length);
waitAck();
}
// このコマンドは普通の座標系のようである
public void drawImage(byte[] img, byte xorg, byte yorg, byte x, byte y)
{
byte[] I_cmd = { (byte)'I', xorg, yorg, x, y, 16 };
serial.Write(I_cmd, 0, I_cmd.Length);
int length = x * y * 2;
serial.Write(img, 0, length);
waitAck();
}
public void drawImage(byte[] img, byte x, byte y)
{
byte[] I_cmd = { (byte)'I', 0,0, x, y, 16 };
serial.Write(I_cmd, 0, I_cmd.Length);
serial.Write(img, 0, img.Length);
waitAck();
}
private void waitAck()
{
int ch=0;
int timeout = 10; // 10ms経てば処理は終わっていると仮定する
while (serial.BytesToRead > 0) serial.ReadByte(); // ゴミを取る
do
{
CPU.delay(1);
if (serial.BytesToRead > 0) ch = serial.ReadByte();
if (timeout-- == 0) break;
} while (ch != 0x06);
}
}
}
|