Skip to content

Instantly share code, notes, and snippets.

Created March 3, 2017 14:52
Show Gist options
  • Save anonymous/9a8c6f2be1a552bc2b1ca178e126ad95 to your computer and use it in GitHub Desktop.
Save anonymous/9a8c6f2be1a552bc2b1ca178e126ad95 to your computer and use it in GitHub Desktop.
I2C example using classic Wii controllers.
// code is from adafruit origianlly https://github.com/adafruit/Adafruit_SSD1306/
// Many improvements are needed. This is just a "get me started" code
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
namespace SSD1306_OLED
{
public class Program
{
static int SSD1306_LCDHEIGHT = 64;
static int SSD1306_LCDWIDTH = 128;
static ushort SSD1306_I2C_ADDRESS = (0x3C);
static I2CDevice.Configuration DisplayConfig =
new I2CDevice.Configuration(SSD1306_I2C_ADDRESS, 400);
static I2CDevice I2C = new I2CDevice(DisplayConfig);
static void ssd1306_command(int cmd)
{
I2CDevice.I2CTransaction[] xActions =
new I2CDevice.I2CTransaction[1];
// create write buffer (we need one byte)
byte[] buff = new byte[2];
buff[1] = (byte) cmd;
xActions[0] = I2CDevice.CreateWriteTransaction(buff);
if (I2C.Execute(xActions, 1000) == 0)
{
Debug.Print("Failed to perform I2C transaction");
}
}
static void InitDisplay()
{
// Init sequence
ssd1306_command(0xae);// SSD1306_DISPLAYOFF); // 0xAE
ssd1306_command(0xd5);// SSD1306_SETDISPLAYCLOCKDIV); // 0xD5
ssd1306_command(0x80); // the suggested ratio 0x80
ssd1306_command(0xa8);// SSD1306_SETMULTIPLEX); // 0xA8
ssd1306_command(SSD1306_LCDHEIGHT - 1);
ssd1306_command(0xd3);// SSD1306_SETDISPLAYOFFSET); // 0xD3
ssd1306_command(0x0); // no offset
ssd1306_command(0x40);// SSD1306_SETSTARTLINE | 0x0); // line #0
ssd1306_command(0x8d);// SSD1306_CHARGEPUMP); // 0x8D
if (false)//vccstate == SSD1306_EXTERNALVCC)
{ ssd1306_command(0x10); }
else
{ ssd1306_command(0x14); }
ssd1306_command(0x20);// SSD1306_MEMORYMODE); // 0x20
ssd1306_command(0x00); // 0x0 act like ks0108
ssd1306_command(0xa1);// SSD1306_SEGREMAP | 0x1);
ssd1306_command(0xc8);// SSD1306_COMSCANDEC);
ssd1306_command(0xda);// SSD1306_SETCOMPINS); // 0xDA
ssd1306_command(0x12);
ssd1306_command(0x81);// SSD1306_SETCONTRAST); // 0x81
if (false)//vccstate == SSD1306_EXTERNALVCC)
{ ssd1306_command(0x9F); }
else
{ ssd1306_command(0xCF); }
ssd1306_command(0xd9);// SSD1306_SETPRECHARGE); // 0xd9
if (false)//vccstate == SSD1306_EXTERNALVCC)
{ ssd1306_command(0x22); }
else
{ ssd1306_command(0xF1); }
ssd1306_command(0xd8);// SSD1306_SETVCOMDETECT); // 0xDB
ssd1306_command(0x40);
ssd1306_command(0xa4);//SSD1306_DISPLAYALLON_RESUME); // 0xA4
ssd1306_command(0xa6);// SSD1306_NORMALDISPLAY); // 0xA6
ssd1306_command(0x2e);// SSD1306_DEACTIVATE_SCROLL);
ssd1306_command(0xaf);// SSD1306_DISPLAYON);//--turn on oled panel
///////////////////
///////////////////
}
static I2CDevice.I2CTransaction[] xActions =
new I2CDevice.I2CTransaction[1];
static void Render(byte[] data)
{
ssd1306_command(0x21);// SSD1306_COLUMNADDR);
ssd1306_command(0); // Column start address (0 = reset)
ssd1306_command(SSD1306_LCDWIDTH - 1); // Column end address (127 = reset)
ssd1306_command(0x22);// SSD1306_PAGEADDR);
ssd1306_command(0); // Page start address (0 = reset)
ssd1306_command(7); // Page end address
byte[] img = new byte[(128 * 64 / 8)+1];
img[0] = 0x40;
Array.Copy(data, 0, img, 1, 128 * 64 / 8);
//img[50]=0xAA;
xActions[0] = I2CDevice.CreateWriteTransaction(img);
if (I2C.Execute(xActions, 1000) == 0)
{
Debug.Print("Failed to perform I2C transaction");
}
}
static void ReadController()
{
I2CDevice.Configuration ControllerConfig =
new I2CDevice.Configuration(0xA4 >> 1, 400);
I2C.Config = ControllerConfig;
//init controller
byte[] data2 = new byte[2] { 0x40, 0x00 };
xActions[0] = I2CDevice.CreateWriteTransaction(data2);
if (I2C.Execute(xActions, 1000) == 0)
{
Debug.Print("Failed to perform I2C transaction");
}
while (true)
{
//read the data
byte[] RawData = new byte[6];
xActions[0] = I2CDevice.CreateReadTransaction(RawData);
if (I2C.Execute(xActions, 1000) == 0)
{
Debug.Print("Failed to perform I2C transaction");
}
//for (int x = 0; x < 6; x++) { RawData[x] = (byte)((RawData[x] ^ 0x17) + 0x17); }
//_AnalogLeftX = ((RawData[0] & 0x3F)) - 32;
//_AnalogLeftY = ((RawData[1] & 0x3F)) - 30;
Debug.Print("X " + (((RawData[0] & 0x3F)) ) + " - Y " + (((RawData[1] & 0x3F)) ));
// start conversion
byte[] data1 = new byte[1] { 0x00 };
xActions[0] = I2CDevice.CreateWriteTransaction(data1);
if (I2C.Execute(xActions, 1000) == 0)
{
Debug.Print("Failed to perform I2C transaction");
}
Thread.Sleep(40);
}
I2C.Config = DisplayConfig;
}
public static void Main()
{
InitDisplay();
Bitmap LCD = new Bitmap(128, 64);
Font fnt = Resources.GetFont(Resources.FontResources.NinaB);
int counter = 0;
int x = 50;
int dx = 5;
//init Wii controller
I2CDevice.Configuration ControllerConfig =
new I2CDevice.Configuration(0xA4 >> 1, 400);
I2C.Config = ControllerConfig;
//init controller
byte[] data2 = new byte[2] { 0x40, 0x00 };
xActions[0] = I2CDevice.CreateWriteTransaction(data2);
if (I2C.Execute(xActions, 1000) == 0)
{
Debug.Print("Failed to perform I2C transaction");
}
while (true)
{
I2C.Config = ControllerConfig;
// read the data
byte[] RawData = new byte[6];
xActions[0] = I2CDevice.CreateReadTransaction(RawData);
if (I2C.Execute(xActions, 1000) == 0)
{
Debug.Print("Failed to perform I2C transaction");
}
//for (int x = 0; x < 6; x++) { RawData[x] = (byte)((RawData[x] ^ 0x17) + 0x17); }
//Debug.Print("X " + (((RawData[0] & 0x3F))) + " - Y " + (((RawData[1] & 0x3F))));
// start conversion
byte[] data1 = new byte[1] { 0x00 };
xActions[0] = I2CDevice.CreateWriteTransaction(data1);
if (I2C.Execute(xActions, 1000) == 0)
{
Debug.Print("Failed to perform I2C transaction");
}
// revert I2C to the display
I2C.Config = DisplayConfig;
//LCD.DrawText(">" + counter++, fnt, Microsoft.SPOT.Presentation.Media.Color.White, 10, 0);
//LCD.DrawEllipse(Microsoft.SPOT.Presentation.Media.Color.White, x, 60, 5, 5);
LCD.DrawEllipse(Microsoft.SPOT.Presentation.Media.Color.White, (RawData[0] & 0x3F)*2, 64- (RawData[1] & 0x3F), 5, 5);
byte[] img = GHI.Utilities.Bitmaps.Convert(LCD, GHI.Utilities.Bitmaps.Format.Bpp1x128);
Render(img);
LCD.Clear();
x += dx;
if (x < 0 || x > 128)
dx *= -1;
//ReadController();
System.Threading.Thread.Sleep(30);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment