first commit

This commit is contained in:
BlubbFish 2026-08-03 13:07:34 +02:00
commit be5bebcac0
12 changed files with 453 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.vs
HotEndUltiDecoder/bin
HotEndUltiDecoder/obj

31
HotEndUltiDecoder.sln Normal file
View File

@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29806.167
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotEndUltiDecoder", "HotEndUltiDecoder\HotEndUltiDecoder.csproj", "{D7896124-7283-4833-9972-C9A3A9474327}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "litjson", "..\Librarys\litjson\litjson\litjson.csproj", "{DD9F38C3-A47A-4600-AFD9-C7C1415618FD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D7896124-7283-4833-9972-C9A3A9474327}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D7896124-7283-4833-9972-C9A3A9474327}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D7896124-7283-4833-9972-C9A3A9474327}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D7896124-7283-4833-9972-C9A3A9474327}.Release|Any CPU.Build.0 = Release|Any CPU
{DD9F38C3-A47A-4600-AFD9-C7C1415618FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DD9F38C3-A47A-4600-AFD9-C7C1415618FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DD9F38C3-A47A-4600-AFD9-C7C1415618FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DD9F38C3-A47A-4600-AFD9-C7C1415618FD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9F98ECF0-A88A-4BBF-9BCF-20B88FFBB2D4}
EndGlobalSection
EndGlobal

35
HotEndUltiDecoder/Crc8.cs Normal file
View File

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BlubbFish.Helper.HotEndUltiDecoder {
public static class Crc8 {
static readonly Byte[] table = new Byte[256];
// x8 + x7 + x6 + x4 + x2 + 1
const Byte poly = 0x07;
public static Byte ComputeChecksum(Byte[] bytes) {
Byte crc = 0;
if(bytes != null && bytes.Length > 0) {
foreach(Byte b in bytes) {
crc = table[crc ^ b];
}
}
return crc;
}
static Crc8() {
for(Int32 i = 0; i < 256; ++i) {
Int32 temp = i;
for(Int32 j = 0; j < 8; ++j) {
if((temp & 0x80) != 0) {
temp = (temp << 1) ^ poly;
} else {
temp <<= 1;
}
}
table[i] = (Byte)temp;
}
}
}
}

View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>BlubbFish.Helper.HotEndUltiDecoder</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Librarys\litjson\litjson\litjson.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BlubbFish.Helper.HotEndUltiDecoder.Items {
internal class CrcItem : RegisterItem<Boolean>, IItems {
private readonly Int32 _addr;
private readonly Tuple<Int32, Int32> _range;
public CrcItem(Int32 address, Tuple<Int32, Int32> over_address_range, String description = "8 bit cyclic redundancy checksum") : base(description, "boolean") {
this._addr = address;
this._range = over_address_range;
}
private Byte CalculateCrc(Byte[] data) => Crc8.ComputeChecksum(data[this._range.Item1..this._range.Item2]);
public override Boolean GetValueFromData(Byte[] data) => this.CalculateCrc(data) == data[this._addr];
public override Byte[] SetValueInData(Byte[] data, Boolean value) {
Byte[] new_data = data.ToArray();
new_data[this._addr] = this.CalculateCrc(data);
return new_data;
}
public Double GetDouble(Byte[] data) => throw new NotImplementedException();
public Byte[] SetDouble(Byte[] data, Double value) => throw new NotImplementedException();
public String GetString(Byte[] data) => throw new NotImplementedException();
public Byte[] SetString(Byte[] data, String value) => throw new NotImplementedException();
public Boolean GetBoolean(Byte[] data) => this.GetValueFromData(data);
public Byte[] SetBoolean(Byte[] data, Boolean value) => this.SetValueInData(data, value);
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BlubbFish.Helper.HotEndUltiDecoder.Items {
internal class EnumItem : NumericItem, IItems {
private readonly String[] _enum;
public EnumItem(Int32 address, String mask, String description, String[] @enum, String unit = null) : base(address, mask, description, null, null, unit) => this._enum = @enum;
public new String GetString(Byte[] data) {
Int32 value = (Int32)this.GetValueFromData(data);
if(value > this._enum.Length) {
return null;
}
try {
return this._enum[value];
} catch {
Console.WriteLine("at index " + value + " no valid value was found");
return null;
}
}
public new Byte[] SetString(Byte[] data, String value) {
for(Int32 i = 0; i < this._enum.Length; i++) {
if(this._enum[i] == value) {
return this.SetValueInData(data, i);
}
}
throw new ArgumentException("key ('"+value+"') does not exist in EnumItem");
}
public new Byte[] SetDouble(Byte[] data, Double value) => this.SetString(data, value.ToString().Replace(',','.'));
}
}

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BlubbFish.Helper.HotEndUltiDecoder.Items {
class GUIDItem : RegisterItem<Guid>, IItems {
private Int32 _addr;
public GUIDItem(Int32 address, String description) : base(description, "guid") => this._addr = address;
public override Guid GetValueFromData(Byte[] data) => new Guid(data[this._addr..(this._addr + 16)]);
public override Byte[] SetValueInData(Byte[] data, Guid value) {
Byte[] new_data = data.ToArray();
Byte[] guid_bytes = value.ToByteArray();
foreach(Int32 i in Enumerable.Range(this._addr, 16)) {
new_data[i] = guid_bytes[i - this._addr];
}
return new_data;
}
public String GetString(Byte[] data) => this.GetValueFromData(data).ToString();
public Byte[] SetString(Byte[] data, String value) => this.SetValueInData(data, new Guid(value));
public Double GetDouble(Byte[] data) => throw new NotImplementedException();
public Byte[] SetDouble(Byte[] data, Double value) => throw new NotImplementedException();
public Boolean GetBoolean(Byte[] data) => throw new NotImplementedException();
public Byte[] SetBoolean(Byte[] data, Boolean value) => throw new NotImplementedException();
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BlubbFish.Helper.HotEndUltiDecoder.Items {
interface IItems {
Double GetDouble(Byte[] data);
Byte[] SetDouble(Byte[] data, Double value);
String GetString(Byte[] data);
Byte[] SetString(Byte[] data, String value);
Boolean GetBoolean(Byte[] data);
Byte[] SetBoolean(Byte[] data, Boolean value);
}
}

View File

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BlubbFish.Helper.HotEndUltiDecoder.Items {
internal class NumericItem : RegisterItem<Double>, IItems {
private readonly Int32 _addr;
private readonly Int32 _mask;
private readonly Func<Int32, Double> _read;
private readonly Func<Double, Int32> _write;
private readonly Int32 _bytes;
private readonly Int32 _shift;
public NumericItem(Int32 address, String mask, String description, Func<Int32, Double> read = null, Func<Double, Int32> write = null, String unit = null) : base(description, unit) {
this._addr = address;
this._mask = Int32.Parse(mask, System.Globalization.NumberStyles.HexNumber);
this._read = read ?? (x => x);
this._write = write ?? (x => (Int32)x);
this._bytes = (Int32)Math.Ceiling(mask.Length / 2.0);
this._shift = 0;
Int32 tmp_mask = this._mask;
while((tmp_mask & 1) == 0) {
tmp_mask >>= 1;
this._shift += 1;
}
}
public override Double GetValueFromData(Byte[] data) {
Int32 tmp_value = 0;
foreach(Byte b in data[this._addr..(this._addr+this._bytes)]) {
tmp_value = (tmp_value << 8) | b;
}
tmp_value &= this._mask;
return this._read(tmp_value >> this._shift);
}
public override Byte[] SetValueInData(Byte[] data, Double value) {
Byte[] new_data = data.ToArray();
Int32 tmp_value = 0;
foreach(Byte b in data[this._addr..(this._addr + this._bytes)]) {
tmp_value = (tmp_value << 8) | b;
}
tmp_value &= ~this._mask;
tmp_value |= (this._write(value) << this._shift) & this._mask;
foreach(Int32 i in Enumerable.Range(this._addr, this._bytes)) {
new_data[i] = (Byte)((tmp_value >> ((this._addr + this._bytes - 1 - i) * 8)) & 0xff);
}
return new_data;
}
public Byte[] SetDouble(Byte[] data, Double value) => this.SetValueInData(data, value);
public Double GetDouble(Byte[] data) => this.GetValueFromData(data);
public String GetString(Byte[] data) => throw new NotImplementedException();
public Byte[] SetString(Byte[] data, String value) => throw new NotImplementedException();
public Boolean GetBoolean(Byte[] data) => this.GetValueFromData(data) == 1;
public Byte[] SetBoolean(Byte[] data, Boolean value) => this.SetValueInData(data, value ? 1 : 0);
}
}

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BlubbFish.Helper.HotEndUltiDecoder.Items {
abstract class RegisterItem<T> {
protected String _dsc;
protected String _unit;
public RegisterItem(String dsc, String unit) {
this._dsc = dsc;
this._unit = unit;
}
abstract public T GetValueFromData(Byte[] data);
abstract public Byte[] SetValueInData(Byte[] data, T value);
public String GetDescription() => this._dsc;
public String GetUnit() => this._unit;
}
}

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BlubbFish.Helper.HotEndUltiDecoder.Items {
internal class StringItem : RegisterItem<String>, IItems {
private readonly Int32 _addr;
private readonly Int32 _to;
public StringItem(Int32 address, Int32 size, String description) : base(description, "string") {
this._addr = address;
this._to = this._addr + size;
}
public override String GetValueFromData(Byte[] data) => Encoding.UTF8.GetString(data[this._addr..this._to]).Trim('\0');
public override Byte[] SetValueInData(Byte[] data, String value) {
Byte[] new_data = data.ToArray();
Byte[] encoded = Encoding.UTF8.GetBytes(value);
if(encoded.Length > this._to - this._addr) {
encoded = encoded[0..(this._to - this._addr)];
} else if(encoded.Length < this._to - this._addr) {
encoded = encoded.Concat(data[(this._addr + encoded.Length)..this._to]).ToArray();
}
foreach(Int32 i in Enumerable.Range(this._addr, this._to - this._addr)) {
new_data[i] = encoded[i - this._addr];
}
return new_data;
}
public Double GetDouble(Byte[] data) => throw new NotImplementedException();
public Byte[] SetDouble(Byte[] data, Double value) => throw new NotImplementedException();
public String GetString(Byte[] data) => this.GetValueFromData(data);
public Byte[] SetString(Byte[] data, String value) => this.SetValueInData(data, value);
public Boolean GetBoolean(Byte[] data) => throw new NotImplementedException();
public Byte[] SetBoolean(Byte[] data, Boolean value) => throw new NotImplementedException();
}
}

View File

@ -0,0 +1,116 @@
using System;
using System.Collections.Generic;
using System.Linq;
using BlubbFish.Helper.HotEndUltiDecoder.Items;
using LitJson;
namespace BlubbFish.Helper.HotEndUltiDecoder {
class Program {
readonly Dictionary<String, IItems> hotendlayout;
public Program() {
this.hotendlayout = new Dictionary<String, IItems> {
{ "major_version_number", new NumericItem(0x00, "fc", "major EEPROM layout specification number, changed when the spec changes are non backwards compatible") },
{ "minor_version_number", new NumericItem(0x00, "03", "minor eeprom version number, changed when a change is purely additive") },
{ "manufacturer_id", new StringItem(0x01, 13, "manufacturer identification string") },
{ "hotend_cartridge_id", new StringItem(0x0e, 10, "hotend cartridge identification string assigned by manufacturer") },
{ "PID_Kp", new NumericItem(0x18, "ffff", "Proportional coefficient", x => x / 100, x => (Int32)(x * 100)) }, // type: ignore
{ "PID_Ki", new NumericItem(0x1a, "ffff", "Integral coefficient", x => x / 100, x => (Int32)(x * 100)) }, // type: ignore
{ "PID_Kd", new NumericItem(0x1c, "ffff", "Derivative coefficient", x => x / 100, x => (Int32)(x * 100)) }, // type: ignore
{ "hardware_revision", new NumericItem(0x1e, "ff", "hardware revision number") },
{ "crc8_page_0", new CrcItem(0x1f, new Tuple<Int32, Int32>(0x00, 0x1e + 1)) },
{ "nozzle_size", new NumericItem(0x20, "ff000000", "size of the 'opening' of the nozzle", x => x / 40.0, x => (Int32)(x * 40.0), "mm") },
{ "hot_zone_size", new NumericItem(0x20, "00ffc000", "length until the insert or cool zone", x => x / 10, x => (Int32)(x * 10), "mm") }, // type: ignore
{ "maximum_temperature", new NumericItem(0x20, "00003ff0", "maximum operating temperature that should not be exceeded, stuff will break", unit: "degC") },
{ "filament_size", new EnumItem(0x20, "0000000f", "filament diameter", new String[] {"2.85", "1.75" }, unit: "mm") },
{ "angle", new NumericItem(0x24, "ff000000", "internal angle of the hot end leading up to the 'opening'", unit: "degrees") },
{ "flat_size", new NumericItem(0x24, "00ff0000", "internal flat section before rising with the 'angle' at the 'opening'", x => x / 10, x => (Int32)(x * 10), "mm") }, // type: ignore
{ "insert_type", new EnumItem(0x24, "0000ff00", "Type of nozzle insert", new String[] {"teflon", "metal/none" }, unit: "string") },
{ "nominal_resistance", new NumericItem(0x26, "00fff800", "this cartridge's heater resistance", x => x / 100, x => (Int32)(x * 100), "Ohm") }, // type: ignore
{ "abrasive_resistant", new NumericItem(0x26, "00000400", "Indicates if this hotend is abrasive_resistant", unit: "bool") },
{ "crc8_page_1", new CrcItem(0x3f, new Tuple<Int32, Int32>(0x20, 0x3e + 1)) },
{ "last_material_guid", new GUIDItem(0x40, "128bit material GUID of the last material used with this nozzle") },
{ "material_extruded", new NumericItem(0x50, "ffffff00", "approximate accumulative amount of material extruded during printing", unit: "cm") },
{ "time_spend_hot", new NumericItem(0x52, "00ffffff", "approximate time spent above 65 degC", unit: "minutes") },
{ "max_exp_temperature", new NumericItem(0x56, "ffc0", "maximum temperature exposed to this nozzle", unit: "degC") },
{ "crc8_page_2", new CrcItem(0x5f, new Tuple<Int32, Int32>(0x40, 0x5e + 1)) },
{ "crc8_page_3", new CrcItem(0x7f, new Tuple<Int32, Int32>(0x60, 0x7e + 1)) }
};
this.CreateGcodeFromJson("{\r\n \"major_version_number\": 1,\r\n \"minor_version_number\": 0,\r\n \"manufacturer_id\": \"BlubbFish\\u0000\\u0000\\u0000\\u0000\",\r\n \"hotend_cartridge_id\": \"AA 0.6\\u0000\\u0000\\u0000\\u0000\",\r\n \"PID_Kp\": 0,\r\n \"PID_Ki\": 0,\r\n \"PID_Kd\": 0,\r\n \"hardware_revision\": 1,\r\n\r\n \"nozzle_size\": 0.6,\r\n \"hot_zone_size\": 21,\r\n \"maximum_temperature\": 350,\r\n \"filament_size\": 2.85,\r\n \"angle\": 70,\r\n \"flat_size\": 0.3,\r\n \"insert_type\": \"teflon\",\r\n \"nominal_resistance\": 23,\r\n \"abrasive_resistant\": false\r\n}");
//this.CreateJsonFromGCode("04556c74696d616b657200000000414120302e38000000000000000000000175", "203495e046030047e00000000000000000000000000000000000000000000019", "0000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000000");
}
private void CreateJsonFromGCode(String p1, String p2, String p3, String p4) {
Byte[] data = this.StringToByteArray(p1);
data = data.Concat(this.StringToByteArray(p2)).ToArray();
data = data.Concat(this.StringToByteArray(p3)).ToArray();
data = data.Concat(this.StringToByteArray(p4)).ToArray();
Dictionary<String, Object> jsonobj = new Dictionary<String, Object>();
foreach(KeyValuePair<String, IItems> item in this.hotendlayout) {
if(item.Value is NumericItem && !(item.Value is EnumItem)) {
jsonobj.Add(item.Key, item.Value.GetDouble(data));
} else if(item.Value is StringItem) {
jsonobj.Add(item.Key, item.Value.GetString(data));
} else if(item.Value is CrcItem) {
jsonobj.Add(item.Key, item.Value.GetBoolean(data));
} else if(item.Value is EnumItem) {
jsonobj.Add(item.Key, item.Value.GetString(data));
} else if(item.Value is GUIDItem) {
jsonobj.Add(item.Key, item.Value.GetString(data));
}
}
Console.WriteLine(JsonMapper.ToJson(jsonobj));
}
public Byte[] StringToByteArray(String hex) {
return Enumerable.Range(0, hex.Length).Where(x => x % 2 == 0).Select(x => Convert.ToByte(hex.Substring(x, 2), 16)).ToArray();
}
private void CreateGcodeFromJson(String jsonstr) {
JsonData json = JsonMapper.ToObject(jsonstr);
Byte[] data = new Byte[0x80];
foreach(KeyValuePair<String, JsonData> item in json) {
if(item.Value.IsDouble) {
data = this.hotendlayout[item.Key].SetDouble(data, (Double)item.Value);
} else if(item.Value.IsInt) {
data = this.hotendlayout[item.Key].SetDouble(data, (Int32)item.Value);
} else if(item.Value.IsString) {
data = this.hotendlayout[item.Key].SetString(data, item.Value.ToString());
} else if(item.Value.IsBoolean) {
data = this.hotendlayout[item.Key].SetBoolean(data, (Boolean)item.Value);
}
}
data = this.hotendlayout["crc8_page_0"].SetBoolean(data, false);
data = this.hotendlayout["crc8_page_1"].SetBoolean(data, false);
data = this.hotendlayout["crc8_page_2"].SetBoolean(data, false);
data = this.hotendlayout["crc8_page_3"].SetBoolean(data, false);
Console.WriteLine("sendgcode M151 T1 A00 D" + BitConverter.ToString(data[0..8]).Replace("-", ""));
Console.WriteLine("sendgcode M151 T1 A08 D" + BitConverter.ToString(data[8..16]).Replace("-", ""));
Console.WriteLine("sendgcode M151 T1 A16 D" + BitConverter.ToString(data[16..24]).Replace("-", ""));
Console.WriteLine("sendgcode M151 T1 A24 D" + BitConverter.ToString(data[24..32]).Replace("-", ""));
Console.WriteLine();
Console.WriteLine("sendgcode M151 T1 A32 D" + BitConverter.ToString(data[32..40]).Replace("-", ""));
Console.WriteLine("sendgcode M151 T1 A40 D" + BitConverter.ToString(data[40..48]).Replace("-", ""));
Console.WriteLine("sendgcode M151 T1 A48 D" + BitConverter.ToString(data[48..56]).Replace("-", ""));
Console.WriteLine("sendgcode M151 T1 A56 D" + BitConverter.ToString(data[56..64]).Replace("-", ""));
//Console.WriteLine(BitConverter.ToString(data[0..32]).Replace("-", ""));
//Console.WriteLine(BitConverter.ToString(data[32..64]).Replace("-", " "));
//Console.WriteLine(BitConverter.ToString(data[64..96]).Replace("-", " "));
//Console.WriteLine(BitConverter.ToString(data[96..128]).Replace("-", " "));
}
static void Main(String[] _) => new Program();
}
}