Skip to content

Commit

Permalink
Badly document image uploading and add some code that does it. Curren…
Browse files Browse the repository at this point in the history
…tly only up to 12px wide images are figured out
  • Loading branch information
danzel committed Mar 31, 2012
1 parent 23f3f0e commit 7562fd3
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 75 deletions.
3 changes: 2 additions & 1 deletion BadgeInvaders/BadgeLib/BadgeLib.csproj
Expand Up @@ -33,14 +33,15 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="PacketBuilder.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
79 changes: 76 additions & 3 deletions BadgeInvaders/BadgeLib/PacketBuilder.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

Expand All @@ -12,13 +13,12 @@ public class PacketBuilder
/// </summary>
/// <param name="address0">0x31</param>
/// <param name="messageIndex">0+</param>
/// <param name="address1">0x00, 0x40, 0x80</param>
/// <param name="speed"></param>
/// <param name="unknown">0x31</param>
/// <param name="scrollMode"></param>
/// <param name="text"></param>
/// <returns></returns>
public static List<byte> GenerateTextPacket(byte address0, int messageIndex, byte address1, Speed speed, byte unknown, ScrollMode scrollMode, string text)
public static List<byte> GenerateTextPacket(byte address0, int messageIndex, Speed speed, byte unknown, ScrollMode scrollMode, string text)
{
List<byte> buffer = new List<byte>();
buffer.Add((byte)speed);
Expand All @@ -31,6 +31,79 @@ public static List<byte> GenerateTextPacket(byte address0, int messageIndex, byt
return GenerateMultiplePackets(address0, messageIndex, buffer);
}


public static List<byte> GenerateImagePacket(byte address0, int messageIndex, Speed speed, byte unknown, ScrollMode scrollMode, Bitmap bitmap)
{
List<byte> result = new List<byte>();


List<byte> buffer = new List<byte>();
//Scroll mode etc
buffer.Add((byte)speed);
buffer.Add(unknown);
buffer.Add((byte)scrollMode);

//Say how wide the image is in blocks of 12 pixels
int bitmap12PxCount = 1 + (bitmap.Width - 1) / 12;

buffer.Add((byte)bitmap12PxCount); //Length byte (how many image banks below will be mentioned

for (int i = 0; i < bitmap12PxCount; i++)
{
buffer.Add(0x80); //Image
buffer.Add((byte)i); //Index
}


//Pad the buffer out to be 4 packets long (Desktop programmer does this, but it doesn't appear to be required)
//while (buffer.Count < 4 * 64)
// buffer.Add(0);

result.AddRange(GenerateMultiplePackets(address0, messageIndex, buffer));


//Next packets (image) are special, messageIndex 8!
buffer.Clear();

buffer.AddRange(PackBitmapToBytes(bitmap));

result.AddRange(GenerateMultiplePackets(address0, 8, buffer));

return result;
}

private static List<byte> PackBitmapToBytes(Bitmap bitmap)
{
List<byte> buffer = new List<byte>();

//Pack 16 pixels from each row into 2 bytes
for (int row = 0; row < 12; row++)
{
for (int c = 0; c < 2; c++)
{
byte b =
(byte)(
((c * 8 + 0 < bitmap.Width && (int)bitmap.GetPixel(c * 8 + 0, row).GetBrightness() == 0) ? 0x80 : 0x00) |
((c * 8 + 1 < bitmap.Width && (int)bitmap.GetPixel(c * 8 + 1, row).GetBrightness() == 0) ? 0x40 : 0x00) |
((c * 8 + 2 < bitmap.Width && (int)bitmap.GetPixel(c * 8 + 2, row).GetBrightness() == 0) ? 0x20 : 0x00) |
((c * 8 + 3 < bitmap.Width && (int)bitmap.GetPixel(c * 8 + 3, row).GetBrightness() == 0) ? 0x10 : 0x00) |
((c * 8 + 4 < bitmap.Width && (int)bitmap.GetPixel(c * 8 + 4, row).GetBrightness() == 0) ? 0x08 : 0x00) |
((c * 8 + 5 < bitmap.Width && (int)bitmap.GetPixel(c * 8 + 5, row).GetBrightness() == 0) ? 0x04 : 0x00) |
((c * 8 + 6 < bitmap.Width && (int)bitmap.GetPixel(c * 8 + 6, row).GetBrightness() == 0) ? 0x02 : 0x00) |
((c * 8 + 7 < bitmap.Width && (int)bitmap.GetPixel(c * 8 + 7, row).GetBrightness() == 0) ? 0x01 : 0x00)
);
buffer.Add(b);
}
}

//Next lot of 12 pixels has something special
//4 bits of unknown ??? (copy of last 4 bits of last byte)
//Pack 16 pixels from each row into 2 bytes (offset by half a byte)


return buffer;
}

/// <summary>
/// Packs the given bytes accross however many packets are required and returns them
/// </summary>
Expand All @@ -43,7 +116,7 @@ private static List<byte> GenerateMultiplePackets(byte address0, int messageInde
const int DataBytesInPacket = 64;

List<byte> result = new List<byte>();
int packetsToBuild = 1 + (bytes.Count / DataBytesInPacket);
int packetsToBuild = 1 + (bytes.Count - 1) / DataBytesInPacket;

for (int i = 0; i < packetsToBuild; i++)
{
Expand Down

0 comments on commit 7562fd3

Please sign in to comment.