This repository was archived by the owner on Apr 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandBlock.cs
More file actions
269 lines (220 loc) · 7.5 KB
/
CommandBlock.cs
File metadata and controls
269 lines (220 loc) · 7.5 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace BlockFirmwareWriter
{
public class CommandBlock
{
public CommandBlock() { }
public void WriteHex(Avrdude dude, string hexPath)
{
if (!canExecute(dude)) throw new InvalidOperationException("avrdudeの実行に失敗しました。");
if (!File.Exists(hexPath))
{
throw new FileNotFoundException("ファームウェアファイルが存在しません。");
}
var args = $"-U flash:w:\"{hexPath}\":i";
dude.Execute(args);
}
public void WriteRom(Avrdude dude, EEPROM rom)
{
if (!canExecute(dude)) throw new InvalidOperationException("avrdudeの実行に失敗しました。");
var intel_hex_data = ":0700000001FF";
intel_hex_data += $"{rom.GetRoleId():X2}{rom.UID_H:X2}{rom.UID_L:X2}FF{rom.GetModeId():X2}";
var sum = 0x07 + 0x01 + 0xFF + rom.GetRoleId() + rom.UID_H + rom.UID_L + 0xFF + rom.GetModeId();
intel_hex_data += $"{(byte)(~sum + 1):X2}";
string tempPath = null;
try
{
tempPath = Path.GetTempFileName();
File.WriteAllText(tempPath, intel_hex_data);
if (!dude.Execute($"-U eeprom:w:\"{tempPath}\":i").Success)
{
throw new InvalidOperationException("EEPROMへの書き込みに失敗しました。接続や設定を確認してください。");
}
}
finally
{
if (tempPath != null && File.Exists(tempPath))
{
File.Delete(tempPath);
}
}
}
public EEPROM GetRom(Avrdude dude)
{
if (!canExecute(dude)) throw new InvalidOperationException("avrdudeの実行に失敗しました。");
var ret = dude.Execute("-U eeprom:r:-:i -q -q");
if ((!ret.Success) || (ret.StdOut.Split('\n').Length < 1))
{
throw new InvalidOperationException("EEPROMの読み込みに失敗しました。。接続や設定を確認してください。");
}
// :(start_code), byte count, address, record_type => 9 chars
// flag (FF) role uidH uidL (FF) mode => 7 bytes * 2 chars = 14 chars
// no checksum check
var intel_hex_data = ret.StdOut.Split('\n')[0];
var sep_hex = intel_hex_data.Skip(9).Take(14)
.Select((v, i) => new { v, i }).GroupBy(x => x.i / 2).Select(g => g.Select(x => x.v))
.Select(h => string.Concat(h))
.ToArray();
byte _saved = Convert.ToByte(sep_hex[0], 16);
byte _role = Convert.ToByte(sep_hex[2], 16);
byte _uid_h = Convert.ToByte(sep_hex[3], 16);
byte _uid_l = Convert.ToByte(sep_hex[4], 16);
byte _mode = Convert.ToByte(sep_hex[6], 16);
var data = new EEPROM();
data.Set(_saved, _role, _uid_h, _uid_l, _mode);
return data;
}
public void WriteFuseBit(Avrdude dude, byte fuse = 0xE2)
{
if (!canExecute(dude)) throw new InvalidOperationException("avrdudeの実行に失敗しました。");
var args = $"-B 3 -U lfuse:w:0x{fuse:X2}:m";
dude.Execute(args);
}
public FuseBit GetFuseBit(Avrdude dude)
{
if (!canExecute(dude)) throw new InvalidOperationException("avrdudeの実行に失敗しました。");
var m = new Dictionary<string, byte>();
foreach (var name in new[] { "efuse", "hfuse", "lfuse" })
{
var ret = dude.Execute($"-B 3 -U {name}:r:-:d -q -q");
if (!ret.Success || string.IsNullOrEmpty(ret.StdOut)) throw new InvalidOperationException("フューズビットの読み込みに失敗しました。");
m[name] = Convert.ToByte(ret.StdOut.Trim(), 10);
}
return new FuseBit(m["efuse"], m["hfuse"], m["lfuse"]);
}
private bool canExecute(Avrdude avrdude)
{
try
{
return avrdude.Execute("-B 3").Success;
}
catch (Exception)
{
return false;
}
}
}
/// <summary>
/// フューズビット
/// </summary>
public class FuseBit
{
public FuseBit(byte efuse, byte hfuse, byte lfuse)
{
ExtFuse = efuse;
HFuse = hfuse;
LFuse = lfuse;
}
/// <summary>
/// Extended Fuse Byte
/// </summary>
public byte ExtFuse { get; }
/// <summary>
/// Fuse High Byte
/// </summary>
public byte HFuse { get; }
/// <summary>
/// Fuse Low Byte
/// </summary>
public byte LFuse { get; }
}
/// <summary>
///
/// </summary>
public class EEPROM
{
public EEPROM() {}
public void Set(byte saved, byte role, byte uid_h, byte uid_l, byte mode)
{
// Saved
Saved = saved == 0x01;
// Role
if (Enum.IsDefined(typeof(FlocRole), role))
{
Role = (FlocRole)Enum.ToObject(typeof(FlocRole), role);
} else
{
Role = null;
}
// Mode
if (Enum.IsDefined(typeof(FlocMode), mode))
{
Mode = (FlocMode)Enum.ToObject(typeof(FlocMode), mode);
}
else
{
Mode = null;
}
// UID_H
UID_H = uid_h;
// UID_L
UID_L = uid_l;
}
public byte GetRoleId()
{
return Role == null ? (byte)0xFF : (byte)Role;
}
public byte GetModeId()
{
return Mode == null ? (byte)0xFF : (byte)Mode;
}
/// <summary>
/// 情報が保存されているか
/// </summary>
public bool Saved { get; private set; }
/// <summary>
/// UID (上位ビット)
/// </summary>
public byte UID_H { get; private set; }
/// <summary>
/// UID (下位ビット)
/// </summary>
public byte UID_L { get; private set; }
/// <summary>
/// ブロックのRole
/// </summary>
public FlocRole? Role { get; private set; }
/// <summary>
/// ブロックの実行モード
/// </summary>
public FlocMode? Mode { get; private set; }
}
/// <summary>
/// ふろっく ブロックのRole
/// </summary>
public enum FlocRole : byte
{
MoveFront = 0x11,
MoveBack = 0x19,
TurnLeft = 0x21,
TurnRight = 0x29,
TurnLeft90 = 0x22,
TurnRight90 = 0x2A,
ShakeLeftHand = 0x31,
ShakeRightHand = 0x35,
ShakeBothHands = 0x39,
ShakeLeftHead = 0x41,
ShakeRightHead = 0x49,
PureCore = 0x81,
IfBrightness = 0x91,
IfObject = 0x92,
For1 = 0xC1,
For2 = 0xC2,
For3 = 0xC3,
For4 = 0xC4,
For5 = 0xC5,
None = 0xFF
}
/// <summary>
/// ふろっく コマンドブロックの動作モード
/// </summary>
public enum FlocMode : byte
{
PRODUCTION = 0x01,
CONFIG = 0x11,
DEBUG = 0x12
}
}