Permalink
Browse files

Starting on a library for sending data to the badge. Add a function t…

…hat can pack up text, even across multiple packets.
  • Loading branch information...
1 parent c8a4dd0 commit 23f3f0e70d7ce59489b604722f5481b92321e017 @danzel committed Mar 31, 2012
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{5F8C8414-FA88-4841-8552-FCD155CF01EB}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>BadgeLib</RootNamespace>
+ <AssemblyName>BadgeLib</AssemblyName>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <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="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
@@ -0,0 +1,98 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace BadgeLib
+{
+ public class PacketBuilder
+ {
+ /// <summary>
+ ///
+ /// </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)
+ {
+ List<byte> buffer = new List<byte>();
+ buffer.Add((byte)speed);
+ buffer.Add(unknown);
+ buffer.Add((byte)scrollMode);
+
+ buffer.Add((byte)text.Length);
+ buffer.AddRange(text.Select(c => (byte)c));
+
+ return GenerateMultiplePackets(address0, messageIndex, buffer);
+ }
+
+ /// <summary>
+ /// Packs the given bytes accross however many packets are required and returns them
+ /// </summary>
+ /// <param name="address0">0x31</param>
+ /// <param name="messageIndex">0+</param>
+ /// <param name="bytes"></param>
+ /// <returns></returns>
+ private static List<byte> GenerateMultiplePackets(byte address0, int messageIndex, List<byte> bytes)
+ {
+ const int DataBytesInPacket = 64;
+
+ List<byte> result = new List<byte>();
+ int packetsToBuild = 1 + (bytes.Count / DataBytesInPacket);
+
+ for (int i = 0; i < packetsToBuild; i++)
+ {
+ List<byte> buffer = new List<byte>();
+ buffer.Add(0x02); //Start of message
+ buffer.Add(address0);
+ buffer.Add((byte)(0x06 + messageIndex));
+ buffer.Add((byte) (i * 0x40));
+ for (int b = i * DataBytesInPacket; b < bytes.Count && b < (i + 1) * DataBytesInPacket; b++)
+ {
+ buffer.Add(bytes[b]);
+ }
+ while (buffer.Count < 4 + DataBytesInPacket)
+ buffer.Add(0);
+ buffer.Add(CalculateCrc(buffer));
+
+ result.AddRange(buffer);
+ }
+ return result;
+ }
+
+ /// <summary>
+ /// Returns the crc for the last 67 bytes in the buffer
+ /// </summary>
+ private static byte CalculateCrc(List<byte> bytes)
+ {
+ return (byte)((bytes.Skip(bytes.Count - 67).Select(b => (int)b).Sum()) & 0xff);
+ }
+
+ public enum Speed : byte
+ {
+ Unknown = 0,
+
+ S1 = (byte)'1',
+ S2 = (byte)'2',
+ S3 = (byte)'3',
+ S4 = (byte)'4',
+ S5 = (byte)'5'
+ }
+
+ public enum ScrollMode : byte
+ {
+ Unknown = 0,
+
+ Hold = (byte)'A',
+ Rotate = (byte)'B',
+ Snow = (byte)'C',
+ Flash = (byte)'D',
+ HoldFrame = (byte)'E',
+ }
+ }
+}
@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("BadgeLib")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("BadgeLib")]
+[assembly: AssemblyCopyright("Copyright © 2012")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("132978c3-7c9a-4481-a1a6-b7c67f67d104")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
@@ -3,16 +3,41 @@ Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SerialHax", "SerialHax\SerialHax.csproj", "{F7969E55-79DF-41A2-BF4D-E31477DFDD45}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BadgeLib", "BadgeLib\BadgeLib.csproj", "{5F8C8414-FA88-4841-8552-FCD155CF01EB}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Debug|Mixed Platforms = Debug|Mixed Platforms
Debug|x86 = Debug|x86
+ Release|Any CPU = Release|Any CPU
+ Release|Mixed Platforms = Release|Mixed Platforms
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {F7969E55-79DF-41A2-BF4D-E31477DFDD45}.Debug|Any CPU.ActiveCfg = Debug|x86
+ {F7969E55-79DF-41A2-BF4D-E31477DFDD45}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
+ {F7969E55-79DF-41A2-BF4D-E31477DFDD45}.Debug|Mixed Platforms.Build.0 = Debug|x86
{F7969E55-79DF-41A2-BF4D-E31477DFDD45}.Debug|x86.ActiveCfg = Debug|x86
{F7969E55-79DF-41A2-BF4D-E31477DFDD45}.Debug|x86.Build.0 = Debug|x86
+ {F7969E55-79DF-41A2-BF4D-E31477DFDD45}.Release|Any CPU.ActiveCfg = Release|x86
+ {F7969E55-79DF-41A2-BF4D-E31477DFDD45}.Release|Mixed Platforms.ActiveCfg = Release|x86
+ {F7969E55-79DF-41A2-BF4D-E31477DFDD45}.Release|Mixed Platforms.Build.0 = Release|x86
{F7969E55-79DF-41A2-BF4D-E31477DFDD45}.Release|x86.ActiveCfg = Release|x86
{F7969E55-79DF-41A2-BF4D-E31477DFDD45}.Release|x86.Build.0 = Release|x86
+ {5F8C8414-FA88-4841-8552-FCD155CF01EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {5F8C8414-FA88-4841-8552-FCD155CF01EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {5F8C8414-FA88-4841-8552-FCD155CF01EB}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
+ {5F8C8414-FA88-4841-8552-FCD155CF01EB}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
+ {5F8C8414-FA88-4841-8552-FCD155CF01EB}.Debug|x86.ActiveCfg = Debug|Any CPU
+ {5F8C8414-FA88-4841-8552-FCD155CF01EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {5F8C8414-FA88-4841-8552-FCD155CF01EB}.Release|Any CPU.Build.0 = Release|Any CPU
+ {5F8C8414-FA88-4841-8552-FCD155CF01EB}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
+ {5F8C8414-FA88-4841-8552-FCD155CF01EB}.Release|Mixed Platforms.Build.0 = Release|Any CPU
+ {5F8C8414-FA88-4841-8552-FCD155CF01EB}.Release|x86.ActiveCfg = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = SerialHax\SerialHax.csproj
@@ -4,6 +4,7 @@
using System.Threading;
using System.Linq;
using System.Text;
+using BadgeLib;
namespace SerialHax
{
@@ -13,6 +14,22 @@ class MainClass
public static void Main (string[] args)
{
+
+ using (var serial = new SerialPort("COM" + 6, 38400, Parity.None, 8, StopBits.One))
+ {
+ _port = serial;
+ serial.Open();
+
+ SendBytes(new byte[] { 0 }); //init byte
+
+ SendBytes(PacketBuilder.GenerateTextPacket(0x31,0,0x00,PacketBuilder.Speed.S5, 0x31, PacketBuilder.ScrollMode.Rotate, "long message text that is longer than 40 chars i hope maybe it is i think yes...").ToArray());
+ SendBytes(new byte[] { 2, 0x33, 1 });
+ return;
+ }
+
+ MainClass.SendTextOnPort(6, "1234");
+ return;
+
Thread updateUiThread = new Thread(UiThread);
updateUiThread.Start();
@@ -194,7 +211,7 @@ public static void UiThread()
SendTextOnPort(4, buffers[0]);
SendTextOnPort(5, buffers[1]);
SendTextOnPort(6, buffers[2]);
- SendTextOnPort(7, buffers[3]);
+ SendTextOnPort(8, buffers[3]);
Console.Clear();
for (int i = 0; i < 4; i++)
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -37,5 +37,11 @@
<Compile Include="Main.cs" />
<Compile Include="AssemblyInfo.cs" />
</ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\BadgeLib\BadgeLib.csproj">
+ <Project>{5F8C8414-FA88-4841-8552-FCD155CF01EB}</Project>
+ <Name>BadgeLib</Name>
+ </ProjectReference>
+ </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

0 comments on commit 23f3f0e

Please sign in to comment.