Took out old emulator and put in a new c# one
This commit is contained in:
parent
f9f2d2c836
commit
fe80c09668
12 changed files with 265 additions and 335 deletions
|
|
@ -1,15 +1,15 @@
|
|||
// Copyright (c) 2015, Lars Brubaker
|
||||
// All rights reserved.
|
||||
//
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this
|
||||
// list of conditions and the following disclaimer.
|
||||
// list of conditions and the following disclaimer.
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
|
|
@ -23,58 +23,70 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO.Ports;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PrinterEmulator
|
||||
namespace MatterHackers.PrinterEmulator
|
||||
{
|
||||
public class Emulator
|
||||
{
|
||||
double extruderGoalTemperature = 210;
|
||||
double bedGoalTemperature = -1; // no bed present
|
||||
Random random = new Random();
|
||||
private double bedGoalTemperature = -1;
|
||||
private double extruderGoalTemperature = 210;
|
||||
|
||||
// no bed present
|
||||
private Random random = new Random();
|
||||
|
||||
// Dictionary of command and response callback
|
||||
Dictionary<string, Func<string, string>> responses = new Dictionary<string, Func<string, string>>();
|
||||
private Dictionary<string, Func<string, string>> responses = new Dictionary<string, Func<string, string>>();
|
||||
|
||||
private SerialPort serialPort = null;
|
||||
|
||||
public Emulator()
|
||||
{
|
||||
responses.Add("M105", randomTemp);
|
||||
responses.Add("A", echo);
|
||||
responses.Add("M114", getPosition);
|
||||
responses.Add("N", parseChecksumLine);
|
||||
responses.Add("M105", RandomTemp);
|
||||
responses.Add("A", Echo);
|
||||
responses.Add("M114", GetPosition);
|
||||
responses.Add("N", ParseChecksumLine);
|
||||
responses.Add("M115", reportMarlinFirmware);
|
||||
responses.Add("M104", setExtruderTemperature);
|
||||
responses.Add("M109", setExtruderTemperature);
|
||||
responses.Add("M140", setBedTemperature);
|
||||
responses.Add("M190", setBedTemperature);
|
||||
responses.Add("M104", SetExtruderTemperature);
|
||||
responses.Add("M109", SetExtruderTemperature);
|
||||
responses.Add("M140", SetBedTemperature);
|
||||
responses.Add("M190", SetBedTemperature);
|
||||
}
|
||||
|
||||
// Add response callbacks here
|
||||
public string randomTemp(string command)
|
||||
public string PortName { get; set; }
|
||||
public bool RunSlow { get; set; }
|
||||
|
||||
public string Echo(string command)
|
||||
{
|
||||
// temp commands look like this: ok T:19.4 /0.0 B:0.0 /0.0 @:0 B@:0
|
||||
if (bedGoalTemperature == -1)
|
||||
{
|
||||
return $"ok T:{(extruderGoalTemperature + random.Next(-2, 2))}\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
return $"ok T:{extruderGoalTemperature + random.Next(-2, 2)} B:{bedGoalTemperature + random.Next(-2, 2) }\n";
|
||||
}
|
||||
return command;
|
||||
}
|
||||
|
||||
public string getCorrectResponse(string command)
|
||||
public string getCommandKey(string command)
|
||||
{
|
||||
if (command.IndexOf(' ') != -1)
|
||||
{
|
||||
return command.Substring(0, command.IndexOf(' '));
|
||||
}
|
||||
return command;
|
||||
}
|
||||
|
||||
public string GetCorrectResponse(string command)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Remove line returns
|
||||
command = command.Split('\n')[0]; // strip of the trailing cr (\n)
|
||||
command = parseChecksumLine(command);
|
||||
command = ParseChecksumLine(command);
|
||||
var commandKey = getCommandKey(command);
|
||||
if (responses.ContainsKey(commandKey))
|
||||
{
|
||||
if (RunSlow)
|
||||
{
|
||||
// do the right amount of time for the given command
|
||||
Thread.Sleep(20);
|
||||
}
|
||||
return responses[commandKey](command);
|
||||
}
|
||||
else
|
||||
|
|
@ -90,23 +102,66 @@ namespace PrinterEmulator
|
|||
return "ok\n";
|
||||
}
|
||||
|
||||
public string getPosition(string command)
|
||||
public string GetPosition(string command)
|
||||
{
|
||||
// position commands look like this: X:0.00 Y:0.00 Z0.00 E:0.00 Count X: 0.00 Y:0.00 Z:0.00 then an ok on the next line
|
||||
return "X:0.00 Y:0.00 Z0.00 E:0.00 Count X: 0.00 Y:0.00 Z:0.00\nok\n";
|
||||
}
|
||||
|
||||
// Add response callbacks here
|
||||
public string RandomTemp(string command)
|
||||
{
|
||||
// temp commands look like this: ok T:19.4 /0.0 B:0.0 /0.0 @:0 B@:0
|
||||
if (bedGoalTemperature == -1)
|
||||
{
|
||||
return $"ok T:{(extruderGoalTemperature + random.Next(-2, 2))}\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
return $"ok T:{extruderGoalTemperature + random.Next(-2, 2)} B:{bedGoalTemperature + random.Next(-2, 2) }\n";
|
||||
}
|
||||
}
|
||||
|
||||
public string reportMarlinFirmware(string command)
|
||||
{
|
||||
return "FIRMWARE_NAME:Marlin V1; Sprinter/grbl mashup for gen6 FIRMWARE_URL:https://github.com/MarlinFirmware/Marlin PROTOCOL_VERSION:1.0 MACHINE_TYPE:Framelis v1 EXTRUDER_COUNT:1 UUID:155f84b5-d4d7-46f4-9432-667e6876f37a\nok\n";
|
||||
}
|
||||
|
||||
public string echo(string command)
|
||||
public void Startup()
|
||||
{
|
||||
return command;
|
||||
serialPort = new SerialPort(PortName);
|
||||
|
||||
serialPort.Open();
|
||||
string speed = RunSlow ? "slow" : "fast";
|
||||
Console.WriteLine($"\n Initializing emulator on port {serialPort.PortName} (Speed: {speed})");
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
string line = "";
|
||||
try
|
||||
{
|
||||
line = serialPort.ReadLine(); // read a '\n' terminated line
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
if (line.Length > 0)
|
||||
{
|
||||
Console.WriteLine(line);
|
||||
var response = GetCorrectResponse(line);
|
||||
|
||||
Console.WriteLine(response);
|
||||
serialPort.Write(response);
|
||||
}
|
||||
}
|
||||
|
||||
serialPort.Close();
|
||||
});
|
||||
}
|
||||
|
||||
string parseChecksumLine(string command)
|
||||
private string ParseChecksumLine(string command)
|
||||
{
|
||||
if (command[0] == 'N')
|
||||
{
|
||||
|
|
@ -120,32 +175,7 @@ namespace PrinterEmulator
|
|||
}
|
||||
}
|
||||
|
||||
public string getCommandKey(string command)
|
||||
{
|
||||
if (command.IndexOf(' ') != -1)
|
||||
{
|
||||
return command.Substring(0, command.IndexOf(' '));
|
||||
}
|
||||
return command;
|
||||
}
|
||||
|
||||
string setExtruderTemperature(string command)
|
||||
{
|
||||
try
|
||||
{
|
||||
// M104 S210 or M109 S[temp]
|
||||
var sIndex = command.IndexOf('S') + 1;
|
||||
extruderGoalTemperature = int.Parse(command.Substring(sIndex));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
|
||||
return "ok\n";
|
||||
}
|
||||
|
||||
string setBedTemperature(string command)
|
||||
private string SetBedTemperature(string command)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
@ -160,5 +190,21 @@ namespace PrinterEmulator
|
|||
|
||||
return "ok\n";
|
||||
}
|
||||
|
||||
private string SetExtruderTemperature(string command)
|
||||
{
|
||||
try
|
||||
{
|
||||
// M104 S210 or M109 S[temp]
|
||||
var sIndex = command.IndexOf('S') + 1;
|
||||
extruderGoalTemperature = int.Parse(command.Substring(sIndex));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
|
||||
return "ok\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue