-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESPGUITOOL
201 lines (193 loc) · 6.97 KB
/
ESPGUITOOL
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
using System.Collections.ObjectModel;
using System.IO.Ports;
using System.Management;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
namespace ESP_Flash
{
public partial class Form1 : Form
{
private List<String> portnames = new List<String>();
private List<string> extensions = new List<string>();
private string filePath;
private string filename;
private string p_FileToFlash;
private string p_COMToFlash;
private string p_espToolForFlash;
public Form1()
{
InitializeComponent();
allPorts();
}
private List<string> scanCOMPorts()
{
using (var searchPort = new ManagementObjectSearcher("SELECT * FROM " + "Win32_PnPEntity WHERE Caption like '%(COM%'"))
{
String[] ports = SerialPort.GetPortNames();
var port = searchPort.Get().Cast<ManagementBaseObject>().ToList().Select(p => p["Caption"].ToString());
List<String> portList = ports.Select(n => n + " - " + port.FirstOrDefault(s => s.Contains(n))).ToList();
return portList;
}
}
/// <summary>
///
/// </summary>
private void allPorts()
{
portnames = scanCOMPorts();
foreach (string port in portnames)
{
comPorts.Items.Add(port);
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void scanBtn_Click(object sender, EventArgs e)
{
allPorts();
}
/// <summary>
///
/// </summary>
private void flash_Files()
{
FolderBrowserDialog folderDlg = new FolderBrowserDialog();
DialogResult result = folderDlg.ShowDialog();
if (result == DialogResult.OK)
{
filePath = folderDlg.SelectedPath;
Environment.SpecialFolder root = folderDlg.RootFolder;
}
List<string> extensions = new List<string> { "BIN" };
string[] files_ = GetFilesWithExtensions(filePath, extensions);
filesToFlash.Items.AddRange(files_);
}
/// <summary>
///
/// </summary>
private void esp_tool()
{
FolderBrowserDialog folderDlg = new FolderBrowserDialog();
DialogResult result = folderDlg.ShowDialog();
if (result == DialogResult.OK)
{
filePath = folderDlg.SelectedPath;
Environment.SpecialFolder root = folderDlg.RootFolder;
}
List<string> extensions = new List<string> { "EXE" };
string[] files_ = GetFilesWithExtensions(filePath, extensions);
espToolsFiles.Items.AddRange(files_);
}
/// <summary>
///
/// </summary>
/// <param name="path"></param>
/// <param name="extensions"></param>
/// <returns></returns>
private string[] GetFilesWithExtensions(string path, List<string> extensions)
{
string[] allFilesInFolder = Directory.GetFiles(path);
return allFilesInFolder.Where(f => extensions.Contains(f.ToUpper().Split('.').Last())).ToArray();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void searchFilesBtn_Click(object sender, EventArgs e)
{
flash_Files();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void filesToFlash_SelectedIndexChanged(object sender, EventArgs e)
{
string strPath = filesToFlash.SelectedItem.ToString();
filename = Path.GetFileName(strPath);
p_FileToFlash = strPath;
theFlashBin.Text = filename;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void comPorts_SelectedIndexChanged(object sender, EventArgs e)
{
theCOMPort.Text = comPorts.SelectedItem.ToString();
p_COMToFlash = theCOMPort.Text;
string[] words = p_COMToFlash.Split(' ');
for (int i = 0; i < words.Length; i++)
{
if (i == 0)
{
theCOMPort.Text = words[i];
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void esptoolBtn_Click(object sender, EventArgs e)
{
esp_tool();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void espToolsFiles_SelectedIndexChanged(object sender, EventArgs e)
{
string strPath = espToolsFiles.SelectedItem.ToString();
filename = Path.GetFileName(strPath);
p_espToolForFlash = strPath;
esptoolFile.Text = filename;
}
/// <summary>
///
/// </summary>
/// <param name="script"></param>
/// <returns></returns>
private string runPowershell(string script)
{
Runspace runScript = RunspaceFactory.CreateRunspace();
runScript.Open();
Pipeline pipeline = runScript.CreatePipeline();
pipeline.Commands.AddScript(script);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
runScript.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results) stringBuilder.AppendLine(obj.ToString());
return stringBuilder.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FlashBtn_Click(object sender, EventArgs e)
{
try
{
pShellWindow.Clear();
theProgress.Text = "PLEASE WAIT. . . ERASING ESP FLASH MEMORY!";
pShellWindow.Text = runPowershell(p_espToolForFlash + " --chip esp8266 --port " + theCOMPort.Text + " erase_flash");
theProgress.Text = "PLEASE WAIT. . . FLASHING " + filename;
pShellWindow.Text = runPowershell(p_espToolForFlash + " --chip esp8266 --port " + theCOMPort.Text + " --baud 115200 write_flash 0x00000 " + p_FileToFlash);
theProgress.Text = "FLASHING COMPLETE!";
}
catch { MessageBox.Show("Unable to Flash the connected ESP device", "Flashing ESP Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); }
}
}
}