This repository has been archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Subscriber.cs
215 lines (191 loc) · 9.1 KB
/
Subscriber.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using System;
using System.Collections.Generic;
using System.Drawing;
using rpi_ws281x;
using DuetAPI.Connection;
using DuetAPIClient;
using System.Threading;
using System.Threading.Tasks;
using DuetAPI.ObjectModel;
using ColorMine.ColorSpaces;
namespace LEDProgress
{
class Subscriber{
private string _socketPath;
private int _count;
private int _brightness;
private bool _quiet = false;
private bool _status = false;
private bool _fan0 = false;
private bool _fan1 = false;
private bool _fan2 = false;
private bool _heater0 = false;
private bool _heater1 = false;
private int Count {
get{
var count = _count;
if(_status){
if(_fan0) count--;
if(_fan1) count--;
if(_fan2) count--;
if(_heater0) count--;
if(_heater1) count--;
}
return count;
}
}
private Settings _settings;
private void Log(string str){
if(this._quiet) return;
Console.WriteLine(str);
}
public Subscriber(int pin,
string socketPath,
int count,
bool quiet,
int brightness,
bool invert,
bool status,
bool fan0,
bool fan1,
bool fan2,
bool heater0,
bool heater1){
this._socketPath = socketPath;
this._count = count;
this._quiet = quiet;
this._brightness = brightness;
//The default settings uses a frequency of 800000 Hz and the DMA channel 10.
this._settings = Settings.CreateDefaultSettings();
this._status = status;
this._fan0 = fan0;
this._fan1 = fan1;
this._fan2 = fan2;
this._heater0 = heater0;
this._heater1 = heater1;
Log("Initializing WS281x");
//Set brightness to maximum (255)
//Use Unknown as strip type. Then the type will be set in the native assembly.
this._settings.Channel_1 = new Channel(this._count, pin, (byte)this._brightness, invert, StripType.WS2812_STRIP);
}
public async Task Subscribe(CancellationToken cancellationToken = default(CancellationToken)){
// Create a new connection and connect to DuetControlServer
// https://duet3d.github.io/DuetSoftwareFramework/api/DuetAPIClient.SubscribeConnection.html
Log("Connecting to Duet");
using (var controller = new WS281x(this._settings))
using (SubscribeConnection connection = new SubscribeConnection()){
List<string> filter = null;
await connection.Connect(SubscriptionMode.Full, filter, this._socketPath, cancellationToken);
Log("Fetching Object Model");
ObjectModel model = await connection.GetObjectModel(cancellationToken);
do {
// var purple = new Hsv();
// purple.H = 300;
// purple.S = 1;
// purple.V = 0.33;
for(var i = 0; i < this._count; i++){
controller.SetLEDColor(0, i, Color.Black);
}
int duration = model.Job.Duration ?? 0;
int left = model.Job.TimesLeft.File ?? 0;
float percentage = ((float)duration / (float)(duration + left));
var scaled = this.Count * percentage;
var fullLeds = (int)Math.Floor(scaled);
var partial = scaled - fullLeds;
for(var i = 0; i < fullLeds; i++){
controller.SetLEDColor(0, i, Color.Green);
}
controller.SetLEDColor(0, fullLeds, GetPartialColor(new Hex("00FF00"), partial));
if(this._status){
Log("Showing Status");
var pos = this.Count;
if(this._fan0){
Log(string.Format("Fan0: {0}", model.Fans[0].ActualValue));
controller.SetLEDColor(0, pos++, GetPartialColor(new Hex("0000FF"), model.Fans[0].ActualValue));
}
if(this._fan1){
Log(string.Format("Fan0: {0}", model.Fans[1].ActualValue));
controller.SetLEDColor(0, pos++, GetPartialColor(new Hex("0000FF"), model.Fans[1].ActualValue));
}
if(this._fan2){
Log(string.Format("Fan0: {0}", model.Fans[2].ActualValue));
controller.SetLEDColor(0, pos++, GetPartialColor(new Hex("0000FF"), model.Fans[2].ActualValue));
}
if(this._heater0){
Log(string.Format("Heater0: {0}", model.Heat.Heaters[0].State.ToString()));
switch(model.Heat.Heaters[0].State){
case HeaterState.Active:
controller.SetLEDColor(0, pos++, Color.Green);
break;
case HeaterState.Fault:
controller.SetLEDColor(0, pos++, Color.Red);
break;
case HeaterState.Off:
controller.SetLEDColor(0, pos++, Color.Black);
break;
case HeaterState.Offline:
controller.SetLEDColor(0, pos++, Color.Black);
break;
case HeaterState.Standby:
controller.SetLEDColor(0, pos++, Color.LightGreen);
break;
case HeaterState.Tuning:
controller.SetLEDColor(0, pos++, Color.Blue);
break;
default:
controller.SetLEDColor(0, pos++, Color.Black);
break;
}
}
if(this._heater1){
Log(string.Format("Heater1: {0}", model.Heat.Heaters[1].State.ToString()));
switch(model.Heat.Heaters[1].State){
case HeaterState.Active:
controller.SetLEDColor(0, pos++, Color.Green);
break;
case HeaterState.Fault:
controller.SetLEDColor(0, pos++, Color.Red);
break;
case HeaterState.Off:
controller.SetLEDColor(0, pos++, Color.Black);
break;
case HeaterState.Offline:
controller.SetLEDColor(0, pos++, Color.Black);
break;
case HeaterState.Standby:
controller.SetLEDColor(0, pos++, Color.LightGreen);
break;
case HeaterState.Tuning:
controller.SetLEDColor(0, pos++, Color.Blue);
break;
default:
controller.SetLEDColor(0, pos++, Color.Black);
break;
}
}
}
controller.Render();
Log(string.Format("{0}%", percentage));
System.Threading.Thread.Sleep(25);
//TODO LEDS HERE
Log("Updating Model");
var json = await connection.GetObjectModelPatch(cancellationToken);
model.UpdateFromJson(json.RootElement);
}while(!cancellationToken.IsCancellationRequested);
for(var i = 0; i < this._count; i++){
controller.SetLEDColor(0, i, Color.Black);
}
controller.Render();
}
}
public Color GetColorFromHSV(Hsv colour){
return Color.FromArgb((int)colour.ToRgb().R, (int)colour.ToRgb().G, (int)colour.ToRgb().B);
}
public Color GetPartialColor(Hex colour, double partial){
var hsv = colour.To<Hsv>();
hsv.S = partial;
hsv.V = partial;
return Color.FromArgb((int)hsv.ToRgb().R, (int)hsv.ToRgb().G, (int)hsv.ToRgb().B);
}
}
}