-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
385 lines (363 loc) · 15.9 KB
/
Form1.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using EVEBitmapViewer.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace EVEBitmapViewer
{
public partial class Form1 : Form
{
void MakeScreen_MatrixOrbital(int DotSize)
{
EVE.Send_CMD(EVE.CMD_DLSTART); //Start a new display list
EVE.Send_CMD(EVE.VERTEXFORMAT(0)); //setup VERTEX2F to take pixel coordinates
EVE.Send_CMD(EVE.CLEAR_COLOR_RGB(0, 0, 0)); //Determine the clear screen color
EVE.Send_CMD(EVE.CLEAR(1, 1, 1)); //Clear the screen and the curren display list
EVE.Send_CMD(EVE.COLOR_RGB(26, 26, 192)); // change colour to blue
EVE.Send_CMD(EVE.POINT_SIZE(DotSize * 16)); // set point size to DotSize pixels. Points = (pixels x 16)
EVE.Send_CMD(EVE.BEGIN(EVE.POINTS)); // start drawing point
EVE.Send_CMD(EVE.TAG(1)); // Tag the blue dot with a touch ID
EVE.Send_CMD(EVE.VERTEX2F(EVE.Display_Width()/2, EVE.Display_Height()/2)); // place blue point
EVE.Send_CMD(EVE.END()); // end drawing point
EVE.Send_CMD(EVE.COLOR_RGB(255, 255, 255)); //Change color to white for text
EVE.Cmd_Text((ushort)(EVE.Display_Width()/2), (ushort)(EVE.Display_Height()/2), 30, EVE.OPT_CENTER, " MATRIX ORBITAL"); //Write text in the center of the screen
EVE.Send_CMD(EVE.DISPLAY()); //End the display list
EVE.Send_CMD(EVE.CMD_SWAP); //Swap commands into RAM
EVE.UpdateFIFO(); //Trigger the CoProcessor to start processing the FIFO
}
void UploadToRam(UInt32 address, byte[] data)
{
GCHandle pinnedArray = GCHandle.Alloc(data, GCHandleType.Pinned);
IntPtr pointer = pinnedArray.AddrOfPinnedObject();
int size = data.Length;
int blockSize = Math.Min(1024 * 16, data.Length);
while (blockSize > 0)
{
EVE.StartCoProTransfer(address, 0);
EVE.EVE_SPI_WriteBuffer(pointer, (uint)blockSize);
EVE.EVE_SPI_Disable();
size = size - blockSize;
address = (uint)(address + blockSize);
pointer = new IntPtr(pointer.ToInt64() + blockSize);
blockSize = Math.Min(size, blockSize);
}
pinnedArray.Free();
}
public Form1()
{
InitializeComponent();
this.AllowDrop = true;
this.DragDrop += Form1_DragDrop;
this.DragEnter += Form1_DragEnter;
pictureBox1.Parent = label1;
pictureBox1.BackColor = Color.Transparent;
string PreviousDisplay = Settings.Default.Display; // Setting the datasource will reset this.
cbModel.ComboBox.DataSource = EVE.Displays;
var disp = EVE.Displays.Where(x => x.description == PreviousDisplay).FirstOrDefault();
if (disp != null)
{
cbModel.SelectedItem = disp;
}
cbScale.Text = Settings.Default.Scale;
SetConnectButton();
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
string[] formats = e.Data.GetFormats(true);
if (IsEveConnected)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap) ||
e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
else
{
e.Effect = DragDropEffects.None;
}
}
enum PictureScale
{
Stretch,
Proportional,
None
}
void DisplayImage(Image img)
{
this.pictureBox1.Image = null;
if (!CurrentBridgeDetectedState)
return;
int height = EVE.Display_Height();
Bitmap source = new Bitmap(EVE.Display_Width(), height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
PictureScale scale = (PictureScale)Enum.Parse(typeof(PictureScale), cbScale.Text);
using (Graphics g = Graphics.FromImage(source))
{
switch(scale)
{
case PictureScale.Proportional:
{
float fact_horizontal = (float)source.Width / (float)img.Width;
float w_horizontal = source.Width;
float h_horizontal = img.Height * fact_horizontal;
float fact_vertical = (float)source.Height / (float)img.Height;
float w_vertical = img.Width * fact_vertical;
float h_vertical = source.Height;
float x, y,w,h;
if (w_horizontal <= source.Width && h_horizontal <= source.Height)
{
x = (source.Width - w_horizontal) / 2;
y = (source.Height - h_horizontal) / 2;
w = w_horizontal;
h = h_horizontal;
}
else
{
x = (source.Width - w_vertical) / 2;
y = (source.Height - h_vertical) / 2;
w = w_vertical;
h = h_vertical;
}
g.Clear(Color.Black);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, x, y, w, h);
break;
}
case PictureScale.Stretch:
{
float fact = ((float)EVE.Display_Width()) / source.Width;
float w = source.Width * fact;
float h = source.Height * fact;
g.Clear(Color.Black);
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.DrawImage(img, 0, 0, w, h);
break;
}
case PictureScale.None:
g.Clear(Color.Black);
g.DrawImage(img, new Rectangle(0,0,source.Width, source.Height), new Rectangle(0,0,source.Width, source.Height), GraphicsUnit.Pixel);
break;
}
}
pictureBox1.Image = source;
bool halfres = false;
// Half the resolution if the bitmap does not fit in RAM
if (EVE.Display_Width() * EVE.Display_Height() * 2 > 1024 * 1024)
{
Bitmap HalfSource = new Bitmap(source.Width, source.Height / 2, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using (Graphics g = Graphics.FromImage(HalfSource))
{
g.Clear(Color.Black);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(source, 0, 0, source.Width, source.Height / 2);
}
halfres = true;
source = HalfSource;
}
//Creat a 16 bit copy of it using the graphics class
Bitmap dest = new Bitmap(EVE.Display_Width(), source.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb565);
using (Graphics g = Graphics.FromImage(dest))
{
g.DrawImageUnscaled(source, 0, 0);
}
//Get a copy of the byte array you need to send to your tft
Rectangle r = new Rectangle(0, 0, dest.Width, dest.Height);
BitmapData bd = dest.LockBits(r, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format16bppRgb565);
byte[] TftData = new byte[dest.Width * dest.Height * 2];
Marshal.Copy(bd.Scan0, TftData, 0, TftData.Length);
dest.UnlockBits(bd);
UploadToRam(0, TftData);
EVE.Send_CMD(EVE.CMD_DLSTART); //Start a new display list
EVE.Send_CMD(EVE.CLEAR_COLOR_RGB(0, 0, 0)); //Determine the clear screen color
EVE.Send_CMD(EVE.CLEAR(1, 1, 1)); //Clear the screen and the curren display list
EVE.Send_CMD(EVE.BITMAP_SOURCE(0));
EVE.Send_CMD(EVE.BITMAP_LAYOUT(EVE.RGB565, EVE.Display_Width() * 2, height));
EVE.Send_CMD(EVE.BITMAP_LAYOUTH(EVE.Display_Width() * 2, height));
EVE.Send_CMD(EVE.BEGIN(EVE.BITMAPS)); // Begin bitmap placement
if (halfres)
{
EVE.Send_CMD(EVE.CMD_LOADIDENTITY);
EVE.Send_CMD(EVE.CMD_SCALE);
EVE.Send_CMD(1 << 16);
EVE.Send_CMD(2 << 16);
EVE.Send_CMD(EVE.CMD_SETMATRIX);
}
EVE.Send_CMD(EVE.VERTEX2II(0, 0, 0, 0)); // Define the placement position of the previously defined holding area.
EVE.Send_CMD(EVE.END()); // end placing bitmaps
EVE.Send_CMD(EVE.DISPLAY()); //End the display list
EVE.Send_CMD(EVE.CMD_SWAP); //Swap commands into RAM
EVE.UpdateFIFO(); //Trigger the CoProcessor to start processing the FIFO
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
if (!IsEveConnected)
return;
// Handle FileDrop data.
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
try
{
for (int i = 0; i < files.Count(); i++)
{
CurrentImage = Image.FromFile(files[i]);
DisplayImage(CurrentImage);
Application.DoEvents();
}
}
catch (Exception)
{
MessageBox.Show("Error loading image.","Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
try
{
CurrentImage = (Image)e.Data.GetData(DataFormats.Bitmap);
DisplayImage(CurrentImage);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
this.Invalidate();
}
private void toolStripLabel2_Click(object sender, EventArgs e)
{
}
bool CurrentBridgeDetectedState = false;
bool IsEveConnected = false;
Image CurrentImage = null;
private void timer1_Tick(object sender, EventArgs e)
{
try
{
bool newBridgeState = EVE.SPI_NumChannels() > 0;
txtBridgeDetected.Text = newBridgeState ? "Yes" : "No";
txtBridgeDetected.BackgroundImageLayout = ImageLayout.Stretch;
txtBridgeDetected.BackgroundImage = new Bitmap(1, 1);
var g = Graphics.FromImage(txtBridgeDetected.BackgroundImage);
if (newBridgeState)
g.Clear(Color.PaleGreen);
else
g.Clear(Color.Transparent);
if (newBridgeState != CurrentBridgeDetectedState)
if (newBridgeState != CurrentBridgeDetectedState)
{
CurrentBridgeDetectedState = newBridgeState;
if (!newBridgeState)
{
this.pictureBox1.Image = null;
if (IsEveConnected)
{
IsEveConnected = false;
}
}
else
{
}
SetConnectButton();
}
}
catch
{
txtBridgeDetected.Text = "Error";
}
}
private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Settings.Default.Display = cbModel.SelectedItem.ToString();
Settings.Default.Save();
}
void SetConnectButton()
{
tbConnect.Text = (!IsEveConnected) ? "Connect" : "Disconnect";
txtEveID.Text = (IsEveConnected) ? txtEveID.Text : "No";
txtEveID.BackgroundImageLayout = ImageLayout.Stretch;
txtEveID.BackgroundImage = new Bitmap(1, 1);
var g = Graphics.FromImage(txtEveID.BackgroundImage);
if (IsEveConnected)
g.Clear(Color.PaleGreen);
else
g.Clear(Color.Transparent);
tbConnect.Enabled = CurrentBridgeDetectedState;
cbModel.Enabled = !IsEveConnected;
}
private void tbConnect_Click(object sender, EventArgs e)
{
if (!IsEveConnected)
{
EVE.EveDisplay disp = cbModel.SelectedItem as EVE.EveDisplay;
int model = disp.id;
int res = EVE.FT81x_Init(model, EVE.BOARD_EVE2, EVE.TOUCH_TPN);
if (res > 1)
{
string EVE_ID = string.Format("Unknown 0x{0:x8}",res);
switch(res)
{
case 0x00011008:
EVE_ID = "BT810";
break;
case 0x00011108:
EVE_ID = "BT811";
break;
case 0x00011208:
EVE_ID = "BT812";
break;
case 0x00011308:
EVE_ID = "BT813";
break;
case 0x00011508:
EVE_ID = "BT815";
break;
case 0x00011608:
EVE_ID = "BT816";
break;
case 0x00011708:
EVE_ID = "BT817";
break;
case 0x00011808:
EVE_ID = "BT818";
break;
}
txtEveID.Text = "Yes, " + EVE_ID;
MakeScreen_MatrixOrbital(24);
IsEveConnected = true;
}
else
{
MessageBox.Show("EVE not detected.");
txtEveID.Text = "No";
}
}
else
{
EVE.wr8(EVE.RAM_REG + EVE.REG_PWM_DUTY, 0);
IsEveConnected = false;
}
SetConnectButton();
}
private void cbScale_SelectedIndexChanged(object sender, EventArgs e)
{
if (CurrentImage != null && IsEveConnected)
{
DisplayImage(CurrentImage);
}
}
}
}