-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainForm-DESKTOP-5NP4CDK.cs
More file actions
341 lines (325 loc) · 13.7 KB
/
MainForm-DESKTOP-5NP4CDK.cs
File metadata and controls
341 lines (325 loc) · 13.7 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Windows.Forms;
namespace ZeDMD_Updater2
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
deviceView.ColumnWidthChanging += (sender, e) =>
{
e.NewWidth = deviceView.Columns[e.ColumnIndex].Width; // Keep the original width
e.Cancel = true; // Prevent resizing
};
deviceView.OwnerDraw = true; // Enable custom drawing
deviceView.DrawColumnHeader += deviceView_DrawColumnHeader;
deviceView.DrawSubItem += deviceView_DrawSubItem;
Enabled = false;
deviceView.MouseDoubleClick += deviceView_MouseDoubleClick;
string latestVersion = "";
WaitForm waitForm = new WaitForm();
waitForm.mainText.Text = "Please wait while listing the available firmwares and devices...";
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (s, ev) =>
{
latestVersion = InternetFirmwares.GetAvailableVersions();
Esp32Devices.GetAvailableDevices();
};
worker.RunWorkerCompleted += (s, ev) =>
{
LatestVersion.Text = "Latest version available: " + latestVersion;
InternetFirmwares.PopulateVersions(this);
Esp32Devices.PopulateESP(this);
Enabled = true;
waitForm.Hide();
};
Enabled = false;
worker.RunWorkerAsync();
waitForm.ShowDialog();
}
private void UpdateZeDMDList()
{
Enabled = false;
WaitForm waitForm = new WaitForm();
waitForm.mainText.Text = "Please wait while updating the available devices...";
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (s, ev) =>
{
Esp32Devices.GetAvailableDevices();
};
worker.RunWorkerCompleted += (s, ev) =>
{
Esp32Devices.PopulateESP(this);
Enabled = true;
waitForm.Hide();
};
worker.RunWorkerAsync();
waitForm.ShowDialog();
}
private void deviceView_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.DrawDefault = true;
}
// Custom subitem coloring
private void deviceView_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
if (e.ColumnIndex == 4 && e.Item.SubItems[4].Text.StartsWith("*"))
{
e.Graphics.DrawString(e.SubItem.Text, deviceView.Font, Brushes.Red, e.Bounds);
}
else
{
e.DrawDefault = true; // Use default drawing for other items
}
}
private void radioWUdp_CheckedChanged(object sender, EventArgs e)
{
if (radioWUdp.Checked)
{
radioUsb.Checked = false;
numericUDelay.Enabled = true;
textSsid.Enabled = true;
textPassword.Enabled = true;
numericUPSize.Enabled = false;
}
}
private void radioWTcp_CheckedChanged(object sender, EventArgs e)
{
if (radioWTcp.Checked)
{
radioUsb.Checked = false;
numericUDelay.Enabled = false;
numericUDelay.Enabled = false;
textSsid.Enabled = true;
textPassword.Enabled = true;
numericUPSize.Enabled = false;
}
}
private void radioUsb_CheckedChanged(object sender, EventArgs e)
{
if (radioUsb.Checked)
{
radioWTcp.Checked = false;
radioWUdp.Checked = false;
numericUDelay.Enabled = false;
textSsid.Enabled = false;
textPassword.Enabled = false;
numericUPSize.Enabled = true;
}
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
int current = (int)numericPISpeed.Value;
// Find the closest allowed value
int closest = Esp32Devices.I2SallowedSpeed.OrderBy(v => Math.Abs(v - current)).First();
// Update if necessary
if (closest != current)
{
numericPISpeed.Value = closest;
}
}
private void deviceView_SelectedIndexChanged(object sender, EventArgs e)
{
if (deviceView.SelectedItems.Count == 1)
{
buttonFFile.Enabled = true;
Esp32Device ed = Esp32Devices.esp32Devices[deviceView.SelectedItems[0].Index];
if (ed.isZeDMD)
{
buttonLTest.Enabled = true;
buttonSNParameters.Enabled = true;
}
else
{
buttonLTest.Enabled = false;
buttonSNParameters.Enabled = false;
radio12832.Checked = true;
radio12864.Checked = false;
radio25664.Checked = false;
}
if (versionList.SelectedIndex >= 0) buttonFlash.Enabled = true;
else buttonFlash.Enabled = false;
Esp32Devices.FillEsp32Values(deviceView.SelectedItems[0].Index, this);
}
else
{
buttonFFile.Enabled = false;
buttonLTest.Enabled = false;
buttonSNParameters.Enabled = false;
Esp32Devices.FillEsp32Values(-1, this);
buttonFlash.Enabled = false;
}
}
private void versionList_SelectedIndexChanged(object sender, EventArgs e)
{
if (deviceView.SelectedItems.Count == 1)
{
if (versionList.SelectedIndex >= 0) buttonFlash.Enabled = true;
else buttonFlash.Enabled = false;
}
else buttonFlash.Enabled = false;
}
private void buttonFFile_Click(object sender, EventArgs e)
{
Enabled = false;
bool flashed = false;
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "BIN Files (*.bin)|*.bin",
Title = "Select a BIN file"
};
// Show the dialog and get result
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// Get the selected file path
string filePath = openFileDialog.FileName;
Esp32Device zeddev = Esp32Devices.esp32Devices[deviceView.SelectedItems[0].Index];
string devtype = "esp32";
if (zeddev.isS3 || zeddev.isLilygo) devtype = "esp32s3";
string commandArgs = "--chip " + devtype + " --port COM" + zeddev.ComId.ToString() + " write_flash 0x0 \"" + filePath + "\"";
flashed = FlashAndConfig.FlashEsp32(commandArgs);
if (flashed) CalcAndSetParameters();
}
Enabled = true;
if (flashed)
{
//MessageBox.Show("Configure your ZeDMD with the menu, exit the menu, then click \"OK\"");
UpdateZeDMDList();
}
}
private void buttonLTest_Click(object sender, EventArgs e)
{
Esp32Device.LedTest(Esp32Devices.esp32Devices[deviceView.SelectedItems[0].Index].ComId);
}
private void buttonFlash_Click(object sender, EventArgs e)
{
Enabled = false;
Esp32Device zeddev = Esp32Devices.esp32Devices[deviceView.SelectedItems[0].Index];
int vernum = versionList.SelectedIndex;
if (vernum < 0) return;
vernum = InternetFirmwares.navVersions - vernum - 1;
string strVersion = "v" + InternetFirmwares.avMVersion[vernum].ToString() + "." + InternetFirmwares.avmVersion[vernum].ToString() + "." + InternetFirmwares.avpVersion[vernum].ToString();
string zipFileUrl;
bool flashed = false;
zipFileUrl = "https://github.com/PPUC/ZeDMD/releases/download/" + strVersion + "/ZeDMD-";
if (zeddev.isLilygo)
{
if (radio12864.Checked || radio25664.Checked)
{
MessageBox.Show("Lilygo firmwares are only available for 128x32 format, this resolution is going to be flashed.");
radio12832.Checked = true;
radio12864.Checked = false;
radio25664.Checked = false;
}
zipFileUrl += "LilygoS3Amoled_128x32";
if (radioWUdp.Checked || radioWTcp.Checked) zipFileUrl += "_wifi";
}
else
{
if (zeddev.isS3) zipFileUrl += "S3-N16R8_";
if (radio12864.Checked) zipFileUrl += "128x64";
else if (radio25664.Checked) zipFileUrl += "256x64";
else zipFileUrl += "128x32";
}
zipFileUrl += ".zip";
string firmwareFileName = "ZeDMD.bin";
using (WebClient client = new WebClient())
{
byte[] zipData = client.DownloadData(zipFileUrl);
using (MemoryStream zipStream = new MemoryStream(zipData))
using (ZipArchive archive = new ZipArchive(zipStream))
{
ZipArchiveEntry firmwareEntry = archive.GetEntry(firmwareFileName);
if (firmwareEntry != null)
{
using (Stream firmwareStream = firmwareEntry.Open())
{
using (MemoryStream memoryStream = new MemoryStream())
{
firmwareStream.CopyTo(memoryStream);
byte[] firmwareData = memoryStream.ToArray();
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string filePath = Path.Combine(documentsPath, firmwareFileName);
System.IO.File.WriteAllBytes(filePath, firmwareData);
string devtype = "esp32";
if (zeddev.isS3 || zeddev.isLilygo) devtype = "esp32s3";
string commandArgs = "--chip " + devtype + " --port COM" + zeddev.ComId.ToString() + " write_flash 0x0 \"" + filePath + "\"";
flashed = FlashAndConfig.FlashEsp32(commandArgs);
System.IO.File.Delete(filePath);
if (flashed) CalcAndSetParameters();
}
firmwareStream.Close();
}
}
}
}
Enabled = true;
if (flashed)
{
//MessageBox.Show("Configure your ZeDMD with the menu, exit the menu, then click \"OK\"");
UpdateZeDMDList();
}
}
private void deviceView_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo hit = deviceView.HitTest(e.Location);
if (hit.Item != null && hit.SubItem != null)
{
int rowIndex = hit.Item.Index; // Row number
int columnIndex = hit.Item.SubItems.IndexOf(hit.SubItem); // Column number
if (rowIndex >= 0 && rowIndex < Esp32Devices.esp32Devices.Count && columnIndex == 2)
{
Esp32Device ed = Esp32Devices.esp32Devices[rowIndex];
if (ed.isS3)
{
ed.isS3 = false;
ed.isLilygo = false;
}
else ed.isS3 = true;
Esp32Devices.PopulateESP(this);
}
else if (rowIndex >= 0 && rowIndex < Esp32Devices.esp32Devices.Count && columnIndex == 3)
{
Esp32Device ed = Esp32Devices.esp32Devices[rowIndex];
if (ed.isLilygo) ed.isLilygo = false;
else
{
ed.isLilygo = true;
ed.isS3 = true;
}
Esp32Devices.PopulateESP(this);
}
}
}
private void buttonRescan_Click(object sender, EventArgs e)
{
UpdateZeDMDList();
}
private void CalcAndSetParameters()
{
int comport = Esp32Devices.esp32Devices[deviceView.SelectedIndices[0]].ComId;
int transport;
if (radioWUdp.Checked) transport = 1;
else if (radioWTcp.Checked) transport = 2;
else transport = 0;
int yoffs;
if (radio12864.Checked) yoffs = 16;
else yoffs = 0;
FlashAndConfig.SetZeDmdParameters(comport, (int)numericBrightness.Value, (int)numericROrder.Value, (int)numericPCPhase.Value,
(int)numericPDriver.Value, (int)numericPISpeed.Value, (int)numericPLBlanking.Value, (int)numericPMRRate.Value,
transport, (int)numericUDelay.Value, (int)numericUPSize.Value, textSsid.Text, textPassword.Text, yoffs);
}
private void buttonSNParameters_Click(object sender, EventArgs e)
{
CalcAndSetParameters();
}
}
}