/
SPIProtocolAnalysisDialog.java
1003 lines (875 loc) · 32.3 KB
/
SPIProtocolAnalysisDialog.java
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* OpenBench LogicSniffer / SUMP project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
*
*
* Copyright (C) 2010-2011 - J.W. Janssen, http://www.lxtreme.nl
*/
package nl.lxtreme.ols.tool.spi;
import static nl.lxtreme.ols.util.ExportUtils.HtmlExporter.*;
import static nl.lxtreme.ols.util.swing.SwingComponentUtils.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import java.text.*;
import java.util.*;
import java.util.List;
import java.util.logging.*;
import javax.swing.*;
import nl.lxtreme.ols.api.*;
import nl.lxtreme.ols.api.tools.*;
import nl.lxtreme.ols.api.util.*;
import nl.lxtreme.ols.tool.base.*;
import nl.lxtreme.ols.tool.base.ToolUtils.RestorableAction;
import nl.lxtreme.ols.util.*;
import nl.lxtreme.ols.util.ExportUtils.CsvExporter;
import nl.lxtreme.ols.util.ExportUtils.HtmlExporter;
import nl.lxtreme.ols.util.ExportUtils.HtmlExporter.Element;
import nl.lxtreme.ols.util.ExportUtils.HtmlExporter.MacroResolver;
import nl.lxtreme.ols.util.ExportUtils.HtmlFileExporter;
import nl.lxtreme.ols.util.NumberUtils.BitOrder;
import nl.lxtreme.ols.util.swing.*;
import nl.lxtreme.ols.util.swing.component.*;
import org.osgi.framework.*;
/**
* The Dialog Class
*
* @author Frank Kunz The dialog class draws the basic dialog with a grid
* layout. The dialog consists of three main parts. A settings panel, a
* table panel and three buttons.
*/
public final class SPIProtocolAnalysisDialog extends BaseToolDialog<SPIDataSet> implements ExportAware<SPIDataSet>,
PropertyChangeListener
{
// INNER TYPES
/**
* Provides a combobox renderer for {@link BitOrder} enums.
*/
static class BitOrderItemRenderer extends EnumItemRenderer<BitOrder>
{
// CONSTANTS
private static final long serialVersionUID = 1L;
// METHODS
/**
* @see nl.lxtreme.ols.util.swing.component.EnumItemRenderer#getDisplayValue(java.lang.Enum)
*/
@Override
protected String getDisplayValue( final BitOrder aValue )
{
switch ( aValue )
{
case LSB_FIRST:
return "LSB first";
case MSB_FIRST:
return "MSB first";
}
// Strange, we shouldn't be here...
LOG.warning( "We should not be here actually! Value = " + aValue );
return super.getDisplayValue( aValue );
}
}
static class SPIFIModeRenderer extends EnumItemRenderer<SPIFIMode>
{
// CONSTANTS
private static final long serialVersionUID = 1L;
// METHODS
/**
* {@inheritDoc}
*/
@Override
protected String getDisplayValue( final SPIFIMode aValue )
{
switch ( aValue )
{
case STANDARD:
return "Standard";
case DUAL:
return "Dual mode";
case QUAD:
return "Quad mode";
}
return super.getDisplayValue( aValue );
}
}
/**
* Provides a combobox renderer for SPIMode enums.
*/
static class SPIModeRenderer extends EnumItemRenderer<SPIMode>
{
// CONSTANTS
private static final long serialVersionUID = 1L;
// METHODS
/**
* @see nl.lxtreme.ols.util.swing.component.EnumItemRenderer#getDisplayValue(java.lang.Enum)
*/
@Override
protected String getDisplayValue( final SPIMode aValue )
{
switch ( aValue )
{
case AUTODETECT:
return "Auto-detect";
case MODE_0:
return "Mode 0";
case MODE_1:
return "Mode 1";
case MODE_2:
return "Mode 2";
case MODE_3:
return "Mode 3";
}
return super.getDisplayValue( aValue );
}
/**
* @see nl.lxtreme.ols.util.swing.component.EnumItemRenderer#getToolTip(java.lang.Object)
*/
@Override
protected String getToolTip( final Object aValue )
{
switch ( ( SPIMode )aValue )
{
case AUTODETECT:
return "Tries to determine the SPI mode based on the clock polarity (CPOL).";
case MODE_0:
return "CPOL = 0, CPHA = 0";
case MODE_1:
return "CPOL = 0, CPHA = 1";
case MODE_2:
return "CPOL = 1, CPHA = 0";
case MODE_3:
return "CPOL = 1, CPHA = 1";
}
// Strange, we shouldn't be here...
LOG.warning( "We should not be here actually! Value = " + aValue );
return super.getToolTip( aValue );
}
}
// CONSTANTS
private static final long serialVersionUID = 1L;
private static final Logger LOG = Logger.getLogger( SPIProtocolAnalysisDialog.class.getName() );
// VARIABLES
private JLabel misoLabel;
private JLabel mosiLabel;
private JLabel io2Label;
private JLabel io3Label;
private JComboBox sck;
private JComboBox miso; // IO0
private JComboBox mosi; // IO1
private JComboBox io2; // IO2
private JComboBox io3; // IO3
private JComboBox cs;
private JComboBox mode;
private JComboBox bits;
private JComboBox order;
private JComboBox spifiMode;
private JEditorPane outText;
private JCheckBox reportCS;
private JCheckBox honourCS;
private RestorableAction runAnalysisAction;
private Action exportAction;
private Action closeAction;
private SPIMode detectedSPIMode;
// CONSTRUCTORS
/**
* Creates a new SPIProtocolAnalysisDialog instance.
*
* @param aOwner
* the owner of this dialog;
* @param aToolContext
* the tool context;
* @param aContext
* the OSGi bundle context to use;
* @param aTool
* the {@link SPIAnalyser} tool.
*/
public SPIProtocolAnalysisDialog( final Window aOwner, final ToolContext aToolContext, final BundleContext aContext,
final SPIAnalyser aTool )
{
super( aOwner, aToolContext, aContext, aTool );
initDialog();
setLocationRelativeTo( getOwner() );
}
// METHODS
/**
* {@inheritDoc}
*/
@Override
public void exportToFile( final File aOutputFile, final ExportFormat aFormat ) throws IOException
{
if ( ExportFormat.HTML.equals( aFormat ) )
{
storeToHtmlFile( aOutputFile, getLastResult() );
}
else if ( ExportFormat.CSV.equals( aFormat ) )
{
storeToCsvFile( aOutputFile, getLastResult() );
}
}
/**
* {@inheritDoc}
*/
@Override
public void propertyChange( final PropertyChangeEvent aEvent )
{
final String name = aEvent.getPropertyName();
if ( SPIAnalyserTask.PROPERTY_AUTO_DETECT_MODE.equals( name ) )
{
SwingComponentUtils.invokeOnEDT( new Runnable()
{
@Override
public void run()
{
final Object value = aEvent.getNewValue();
setAutoDetectSPIMode( ( SPIMode )value );
}
} );
}
}
/**
* @see nl.lxtreme.ols.api.Configurable#readPreferences(nl.lxtreme.ols.api.UserSettings)
*/
@Override
public void readPreferences( final UserSettings aSettings )
{
this.spifiMode.setSelectedIndex( aSettings.getInt( "protocol", this.spifiMode.getSelectedIndex() ) );
this.reportCS.setSelected( aSettings.getBoolean( "reportCS", this.reportCS.isSelected() ) );
this.honourCS.setSelected( aSettings.getBoolean( "honourCS", this.honourCS.isSelected() ) );
this.sck.setSelectedIndex( aSettings.getInt( "sck", this.sck.getSelectedIndex() ) );
this.miso.setSelectedIndex( aSettings.getInt( "miso", this.miso.getSelectedIndex() ) );
this.mosi.setSelectedIndex( aSettings.getInt( "mosi", this.mosi.getSelectedIndex() ) );
this.io2.setSelectedIndex( aSettings.getInt( "io2", this.io2.getSelectedIndex() ) );
this.io3.setSelectedIndex( aSettings.getInt( "io3", this.io3.getSelectedIndex() ) );
this.cs.setSelectedIndex( aSettings.getInt( "cs", this.cs.getSelectedIndex() ) );
this.mode.setSelectedIndex( aSettings.getInt( "mode", this.mode.getSelectedIndex() ) );
this.bits.setSelectedIndex( aSettings.getInt( "bits", this.bits.getSelectedIndex() ) );
this.order.setSelectedIndex( aSettings.getInt( "order", this.order.getSelectedIndex() ) );
// Make sure the settings are reflected in the UI...
updateSPIFIModeSettings( ( SPIFIMode )this.spifiMode.getSelectedItem() );
}
/**
* @see nl.lxtreme.ols.tool.base.ToolDialog#reset()
*/
@Override
public void reset()
{
this.outText.setText( getEmptyHtmlPage() );
this.outText.setEditable( false );
this.runAnalysisAction.restore();
setControlsEnabled( true );
this.exportAction.setEnabled( false );
}
/**
* Sets the auto detected SPI mode to the given value.
*
* @param aMode
* the detected SPI mode, cannot be <code>null</code>.
*/
public void setAutoDetectSPIMode( final SPIMode aMode )
{
this.detectedSPIMode = aMode;
}
/**
* @see nl.lxtreme.ols.api.Configurable#writePreferences(nl.lxtreme.ols.api.UserSettings)
*/
@Override
public void writePreferences( final UserSettings aSettings )
{
aSettings.putBoolean( "reportCS", this.reportCS.isSelected() );
aSettings.putBoolean( "honourCS", this.honourCS.isSelected() );
aSettings.putInt( "protocol", this.spifiMode.getSelectedIndex() );
aSettings.putInt( "sck", this.sck.getSelectedIndex() );
aSettings.putInt( "miso", this.miso.getSelectedIndex() );
aSettings.putInt( "mosi", this.mosi.getSelectedIndex() );
aSettings.putInt( "io2", this.io2.getSelectedIndex() );
aSettings.putInt( "io3", this.io3.getSelectedIndex() );
aSettings.putInt( "cs", this.cs.getSelectedIndex() );
aSettings.putInt( "mode", this.mode.getSelectedIndex() );
aSettings.putInt( "bits", this.bits.getSelectedIndex() );
aSettings.putInt( "order", this.order.getSelectedIndex() );
}
/**
* @param aMode
* the SPIFI mode to update the settings to, may be <code>null</code>
* .
*/
final void updateSPIFIModeSettings( final SPIFIMode aMode )
{
boolean enabled = false;
if ( ( aMode == null ) || SPIFIMode.STANDARD.equals( aMode ) )
{
this.misoLabel.setText( "MISO" );
this.mosiLabel.setText( "MOSI" );
}
else
{
this.mosiLabel.setText( "IO0" );
this.misoLabel.setText( "IO1" );
enabled = SPIFIMode.QUAD.equals( aMode );
}
this.io2Label.setEnabled( enabled );
this.io2.setEnabled( enabled );
this.io3Label.setEnabled( enabled );
this.io3.setEnabled( enabled );
}
/**
* {@inheritDoc}
*/
@Override
protected void onToolEnded( final SPIDataSet aAnalysisResult )
{
try
{
final String htmlPage;
if ( aAnalysisResult != null )
{
htmlPage = toHtmlPage( null /* aFile */, aAnalysisResult );
this.exportAction.setEnabled( !aAnalysisResult.isEmpty() );
}
else
{
htmlPage = getEmptyHtmlPage();
this.exportAction.setEnabled( false );
}
this.outText.setText( htmlPage );
this.outText.setEditable( false );
this.runAnalysisAction.restore();
}
catch ( final IOException exception )
{
// Make sure to handle IO-interrupted exceptions properly!
if ( !HostUtils.handleInterruptedException( exception ) )
{
// Should not happen in this situation!
throw new RuntimeException( exception );
}
}
}
/**
* {@inheritDoc}
*/
@Override
protected void onToolStarted()
{
// No-op
}
/**
* {@inheritDoc}
*/
@Override
protected void prepareToolTask( final ToolTask<SPIDataSet> aToolTask )
{
SPIAnalyserTask toolTask = ( SPIAnalyserTask )aToolTask;
toolTask.setBitCount( Integer.parseInt( ( String )this.bits.getSelectedItem() ) - 1 );
toolTask.setCSIndex( this.cs.getSelectedIndex() );
toolTask.setSCKIndex( this.sck.getSelectedIndex() );
toolTask.setIO0Index( this.mosi.getSelectedIndex() - 1 );
toolTask.setIO1Index( this.miso.getSelectedIndex() - 1 );
toolTask.setIO2Index( this.io2.getSelectedIndex() - 1 );
toolTask.setIO3Index( this.io3.getSelectedIndex() - 1 );
toolTask.setProtocol( ( SPIFIMode )this.spifiMode.getSelectedItem() );
toolTask.setReportCS( this.reportCS.isSelected() );
toolTask.setHonourCS( this.honourCS.isSelected() );
toolTask.setOrder( ( BitOrder )this.order.getSelectedItem() );
toolTask.setSPIMode( ( SPIMode )this.mode.getSelectedItem() );
// Register ourselves as property change listener...
toolTask.addPropertyChangeListener( this );
}
/**
* set the controls of the dialog enabled/disabled
*
* @param aEnable
* status of the controls
*/
@Override
protected void setControlsEnabled( final boolean aEnable )
{
this.spifiMode.setEnabled( aEnable );
this.reportCS.setEnabled( aEnable );
this.honourCS.setEnabled( aEnable );
this.sck.setEnabled( aEnable );
this.miso.setEnabled( aEnable );
this.mosi.setEnabled( aEnable );
this.io2.setEnabled( aEnable );
this.io3.setEnabled( aEnable );
this.cs.setEnabled( aEnable );
this.mode.setEnabled( aEnable );
this.bits.setEnabled( aEnable );
this.order.setEnabled( aEnable );
this.closeAction.setEnabled( aEnable );
this.exportAction.setEnabled( aEnable );
}
/**
* {@inheritDoc}
*/
@Override
protected boolean validateToolSettings()
{
BitSet bitset = new BitSet();
bitset.set( this.cs.getSelectedIndex() );
bitset.set( this.sck.getSelectedIndex() );
int expectedBitCount = 2;
final SPIFIMode protocol = ( SPIFIMode )this.spifiMode.getSelectedItem();
if ( SPIFIMode.DUAL.equals( protocol ) )
{
// Both MOSI/IO0 & MISO/IO1 should be defined...
if ( ( this.mosi.getSelectedIndex() < 1 ) || ( this.miso.getSelectedIndex() < 1 ) )
{
JErrorDialog.showDialog( getOwner(), "Cannot start analysis!", "Invalid settings detected!",
"For dual-mode SPI, you need to assign both IO0 and IO1." );
return false;
}
bitset.set( this.mosi.getSelectedIndex() - 1 );
bitset.set( this.miso.getSelectedIndex() - 1 );
expectedBitCount += 2;
}
else if ( SPIFIMode.QUAD.equals( protocol ) )
{
// All IO0..3 should be defined...
if ( ( this.mosi.getSelectedIndex() < 1 ) || ( this.miso.getSelectedIndex() < 1 ) || //
( this.io2.getSelectedIndex() < 1 ) || ( this.io3.getSelectedIndex() < 1 ) )
{
JErrorDialog.showDialog( getOwner(), "Cannot start analysis!", "Invalid settings detected!",
"For quad-mode SPI, you need to assign IO0, IO1, IO2 and IO3." );
return false;
}
bitset.set( this.mosi.getSelectedIndex() - 1 );
bitset.set( this.miso.getSelectedIndex() - 1 );
bitset.set( this.io2.getSelectedIndex() - 1 );
bitset.set( this.io3.getSelectedIndex() - 1 );
expectedBitCount += 4;
}
else
{
if ( this.miso.getSelectedIndex() > 0 )
{
bitset.set( this.miso.getSelectedIndex() - 1 );
expectedBitCount++;
}
if ( this.mosi.getSelectedIndex() > 0 )
{
bitset.set( this.mosi.getSelectedIndex() - 1 );
expectedBitCount++;
}
}
if ( bitset.cardinality() != expectedBitCount )
{
JErrorDialog.showDialog( getOwner(), "Cannot start analysis!", "Invalid settings detected!",
"Not all signals are assigned to unique channels." );
return false;
}
return true;
}
/**
* Creates the HTML template for exports to HTML.
*
* @param aExporter
* the HTML exporter instance to use, cannot be <code>null</code>.
* @return a HTML exporter filled with the template, never <code>null</code>.
*/
private HtmlExporter createHtmlTemplate( final HtmlExporter aExporter )
{
aExporter.addCssStyle( "body { font-family: sans-serif; } " );
aExporter.addCssStyle( "table { border-width: 1px; border-spacing: 0px; border-color: gray;"
+ " border-collapse: collapse; border-style: solid; margin-bottom: 15px; } " );
aExporter.addCssStyle( "table th { border-width: 1px; padding: 2px; border-style: solid; border-color: gray;"
+ " background-color: #C0C0FF; text-align: left; font-weight: bold; font-family: sans-serif; } " );
aExporter.addCssStyle( "table td { border-width: 1px; padding: 2px; border-style: solid; border-color: gray;"
+ " font-family: monospace; } " );
aExporter.addCssStyle( ".date { text-align: right; font-size: x-small; margin-bottom: 15px; } " );
aExporter.addCssStyle( ".w100 { width: 100%; } " );
aExporter.addCssStyle( ".w35 { width: 35%; } " );
aExporter.addCssStyle( ".w30 { width: 30%; } " );
aExporter.addCssStyle( ".w15 { width: 15%; } " );
aExporter.addCssStyle( ".w10 { width: 10%; } " );
aExporter.addCssStyle( ".w8 { width: 8%; } " );
aExporter.addCssStyle( ".w7 { width: 7%; } " );
final Element body = aExporter.getBody();
body.addChild( H1 ).addContent( "SPI Analysis results" );
body.addChild( HR );
body.addChild( DIV ).addAttribute( "class", "date" ).addContent( "Generated: ", "{date-now}" );
Element table, tr, thead, tbody;
table = body.addChild( TABLE ).addAttribute( "class", "w100" );
tbody = table.addChild( TBODY );
tr = tbody.addChild( TR );
tr.addChild( TH ).addAttribute( "colspan", "2" ).addContent( "Configuration" );
tr = tbody.addChild( TR );
tr.addChild( TD ).addAttribute( "class", "w30" ).addContent( "SPI mode" );
tr.addChild( TD ).addContent( "{detected-spi-mode}" );
table = body.addChild( TABLE ).addAttribute( "class", "w100" );
thead = table.addChild( THEAD );
tr = thead.addChild( TR );
tr.addChild( TH ).addAttribute( "class", "w30" ).addAttribute( "colspan", "2" );
tr.addChild( TH ).addAttribute( "class", "w35" ).addAttribute( "colspan", "4" ).addContent( "MOSI" );
tr.addChild( TH ).addAttribute( "class", "w35" ).addAttribute( "colspan", "4" ).addContent( "MISO" );
tr = thead.addChild( TR );
tr.addChild( TH ).addAttribute( "class", "w15" ).addContent( "Index" );
tr.addChild( TH ).addAttribute( "class", "w15" ).addContent( "Time" );
tr.addChild( TH ).addAttribute( "class", "w10" ).addContent( "Hex" );
tr.addChild( TH ).addAttribute( "class", "w10" ).addContent( "Bin" );
tr.addChild( TH ).addAttribute( "class", "w8" ).addContent( "Dec" );
tr.addChild( TH ).addAttribute( "class", "w7" ).addContent( "ASCII" );
tr.addChild( TH ).addAttribute( "class", "w10" ).addContent( "Hex" );
tr.addChild( TH ).addAttribute( "class", "w10" ).addContent( "Bin" );
tr.addChild( TH ).addAttribute( "class", "w8" ).addContent( "Dec" );
tr.addChild( TH ).addAttribute( "class", "w7" ).addContent( "ASCII" );
tbody = table.addChild( TBODY );
tbody.addContent( "{decoded-data}" );
return aExporter;
}
/**
* @return
*/
private JPanel createPreviewPane()
{
final JPanel panTable = new JPanel( new GridLayout( 1, 1, 0, 0 ) );
this.outText = new JEditorPane( "text/html", getEmptyHtmlPage() );
this.outText.setEditable( false );
panTable.add( new JScrollPane( this.outText ) );
return panTable;
}
/**
* @return
*/
private JPanel createSettingsPane()
{
final int channelCount = getData().getChannels();
final JPanel settings = new JPanel( new SpringLayout() );
SpringLayoutUtils.addSeparator( settings, "Settings" );
settings.add( createRightAlignedLabel( "Protocol" ) );
this.spifiMode = new JComboBox( SPIFIMode.values() );
this.spifiMode.setSelectedIndex( 0 );
this.spifiMode.setRenderer( new SPIFIModeRenderer() );
this.spifiMode.addItemListener( new ItemListener()
{
@Override
public void itemStateChanged( final ItemEvent aEvent )
{
final JComboBox cb = ( JComboBox )aEvent.getSource();
updateSPIFIModeSettings( ( SPIFIMode )cb.getSelectedItem() );
}
} );
settings.add( this.spifiMode );
settings.add( createRightAlignedLabel( "/CS" ) );
this.cs = SwingComponentUtils.createChannelSelector( channelCount, 3 );
settings.add( this.cs );
settings.add( createRightAlignedLabel( "SCK" ) );
this.sck = SwingComponentUtils.createChannelSelector( channelCount, 0 );
settings.add( this.sck );
this.mosiLabel = createRightAlignedLabel( "MOSI" );
settings.add( this.mosiLabel );
this.mosi = SwingComponentUtils.createOptionalChannelSelector( channelCount, 3 );
settings.add( this.mosi );
this.misoLabel = createRightAlignedLabel( "MISO" );
settings.add( this.misoLabel );
this.miso = SwingComponentUtils.createOptionalChannelSelector( channelCount, 2 );
settings.add( this.miso );
this.io2Label = createRightAlignedLabel( "IO2" );
settings.add( this.io2Label );
this.io2 = SwingComponentUtils.createOptionalChannelSelector( channelCount, 0 );
settings.add( this.io2 );
this.io3Label = createRightAlignedLabel( "IO3" );
settings.add( this.io3Label );
this.io3 = SwingComponentUtils.createOptionalChannelSelector( channelCount, 0 );
settings.add( this.io3 );
settings.add( createRightAlignedLabel( "SPI Mode" ) );
this.mode = new JComboBox( SPIMode.values() );
this.mode.setSelectedIndex( 2 );
this.mode.setRenderer( new SPIModeRenderer() );
settings.add( this.mode );
settings.add( createRightAlignedLabel( "Bits" ) );
// #106: support up to 32 bits; not all intermediary values should be
// necessary, I guess. Alternatively, we could use /CS as indicator for
// the symbol-size, though that would imply that master and slave always
// talk in the same symbol-size during a transaction...
String[] bitarray = new String[] { "4", "5", "6", "7", "8", "9", "10", "11", //
"12", "13", "14", "15", "16", "24", "32" };
this.bits = new JComboBox( bitarray );
this.bits.setSelectedIndex( 4 );
settings.add( this.bits );
settings.add( createRightAlignedLabel( "Order" ) );
this.order = new JComboBox( BitOrder.values() );
this.order.setSelectedIndex( 0 );
this.order.setRenderer( new BitOrderItemRenderer() );
settings.add( this.order );
settings.add( createRightAlignedLabel( "Show /CS?" ) );
this.reportCS = new JCheckBox();
this.reportCS.setToolTipText( "Whether or not to show /CS transitions in analysis results?" );
this.reportCS.setSelected( true );
settings.add( this.reportCS );
settings.add( createRightAlignedLabel( "Honour /CS?" ) );
this.honourCS = new JCheckBox();
this.honourCS.setToolTipText( "Whether or not to use /CS in analysis results?" );
this.honourCS.setSelected( false );
settings.add( this.honourCS );
SpringLayoutUtils.makeEditorGrid( settings, 10, 4 );
updateSPIFIModeSettings( null );
return settings;
}
/**
* Generates an empty HTML page.
*
* @return String with HTML data.
*/
private String getEmptyHtmlPage()
{
final HtmlExporter exporter = createHtmlTemplate( ExportUtils.createHtmlExporter() );
return exporter.toString( new MacroResolver()
{
@Override
public Object resolve( final String aMacro, final Element aParent )
{
if ( "date-now".equals( aMacro ) )
{
final DateFormat df = DateFormat.getDateInstance( DateFormat.LONG );
return df.format( new Date() );
}
return null;
}
} );
}
/**
* Initializes this dialog.
*/
private void initDialog()
{
setMinimumSize( new Dimension( 640, 480 ) );
final JComponent settingsPane = createSettingsPane();
final JComponent previewPane = createPreviewPane();
final JPanel contentPane = new JPanel( new GridBagLayout() );
contentPane.add( settingsPane, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH,
GridBagConstraints.NONE, new Insets( 2, 0, 2, 0 ), 0, 0 ) );
contentPane.add( previewPane, new GridBagConstraints( 1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH,
GridBagConstraints.BOTH, new Insets( 2, 0, 2, 0 ), 0, 0 ) );
final JButton runAnalysisButton = ToolUtils.createRunAnalysisButton( this );
this.runAnalysisAction = ( RestorableAction )runAnalysisButton.getAction();
final JButton exportButton = ToolUtils.createExportButton( this );
this.exportAction = exportButton.getAction();
this.exportAction.setEnabled( false );
final JButton closeButton = ToolUtils.createCloseButton();
this.closeAction = closeButton.getAction();
final JComponent buttons = SwingComponentUtils.createButtonPane( runAnalysisButton, exportButton, closeButton );
SwingComponentUtils.setupDialogContentPane( this, contentPane, buttons, runAnalysisButton );
}
/**
* exports the table data to a CSV file
*
* @param aFile
* File object
*/
private void storeToCsvFile( final File aFile, final SPIDataSet aDataSet )
{
try
{
final CsvExporter exporter = ExportUtils.createCsvExporter( aFile );
exporter.setHeaders( "index", "start-time", "end-time", "event?", "event-type", "MOSI data", "MISO data" );
final List<SPIData> decodedData = aDataSet.getData();
for ( int i = 0; i < decodedData.size(); i++ )
{
final SPIData ds = decodedData.get( i );
final String startTime = UnitOfTime.format( aDataSet.getTime( ds.getStartSampleIndex() ) );
final String endTime = UnitOfTime.format( aDataSet.getTime( ds.getStartSampleIndex() ) );
final String mosiDataValue = ds.isMosiData() ? Integer.toString( ds.getDataValue() ) : null;
final String misoDataValue = ds.isMisoData() ? Integer.toString( ds.getDataValue() ) : null;
exporter.addRow( Integer.valueOf( i ), startTime, endTime, Boolean.valueOf( ds.isEvent() ), ds.getEventName(),
mosiDataValue, misoDataValue );
}
exporter.close();
}
catch ( final IOException exception )
{
// Make sure to handle IO-interrupted exceptions properly!
if ( !HostUtils.handleInterruptedException( exception ) )
{
LOG.log( Level.WARNING, "CSV export failed!", exception );
}
}
}
/**
* stores the data to a HTML file
*
* @param aFile
* file object
*/
private void storeToHtmlFile( final File aFile, final SPIDataSet aDataSet )
{
try
{
toHtmlPage( aFile, aDataSet );
}
catch ( final IOException exception )
{
// Make sure to handle IO-interrupted exceptions properly!
if ( !HostUtils.handleInterruptedException( exception ) )
{
LOG.log( Level.WARNING, "HTML export failed!", exception );
}
}
}
/**
* generate a HTML page
*
* @param aDataSet
* the data set to create the HTML page for, cannot be
* <code>null</code>.
* @return String with HTML data
*/
private String toHtmlPage( final File aFile, final SPIDataSet aDataSet ) throws IOException
{
final int bitCount = Integer.parseInt( ( String )this.bits.getSelectedItem() );
final int bitAdder = ( ( bitCount % 4 ) != 0 ) ? 1 : 0;
final MacroResolver macroResolver = new MacroResolver()
{
@Override
public Object resolve( final String aMacro, final Element aParent )
{
if ( "date-now".equals( aMacro ) )
{
final DateFormat df = DateFormat.getDateInstance( DateFormat.LONG );
return df.format( new Date() );
}
else if ( "detected-spi-mode".equals( aMacro ) )
{
String result = "<unknown>";
switch ( SPIProtocolAnalysisDialog.this.detectedSPIMode )
{
case MODE_0:
result = "Mode 0 (CPOL = 0, CPHA = 0)";
break;
case MODE_1:
result = "Mode 1 (CPOL = 0, CPHA = 1)";
break;
case MODE_2:
result = "Mode 2 (CPOL = 1, CPHA = 0)";
break;
case MODE_3:
result = "Mode 3 (CPOL = 1, CPHA = 1)";
break;
default:
break;
}
return result;
}
else if ( "decoded-data".equals( aMacro ) )
{
final List<SPIData> decodedData = aDataSet.getData();
Element tr;
for ( int i = 0; i < decodedData.size(); i++ )
{
final SPIData ds = decodedData.get( i );
if ( ds.isEvent() )
{
String event;
String bgColor;
// this is an event
if ( SPIDataSet.SPI_CS_LOW.equals( ds.getEventName() ) )
{
// start condition
event = ds.getEventName();
bgColor = "#c0ffc0";
}
else if ( SPIDataSet.SPI_CS_HIGH.equals( ds.getEventName() ) )
{
// stop condition
event = ds.getEventName();
bgColor = "#e0e0e0";
}
else
{
// unknown event
event = "UNKNOWN";
bgColor = "#ff8000";
}
tr = aParent.addChild( TR ).addAttribute( "style", "background-color: " + bgColor + ";" );
tr.addChild( TD ).addContent( String.valueOf( i ) );
tr.addChild( TD ).addContent( UnitOfTime.format( aDataSet.getTime( ds.getStartSampleIndex() ) ) );
tr.addChild( TD ).addContent( event );
tr.addChild( TD );
tr.addChild( TD );
tr.addChild( TD );
tr.addChild( TD ).addContent( event );
tr.addChild( TD );
tr.addChild( TD );
tr.addChild( TD );
}
else if ( ds.isData() )
{
final int sampleIdx = ds.getStartSampleIndex();
tr = aParent.addChild( TR );
tr.addChild( TD ).addContent( String.valueOf( i ) );
tr.addChild( TD ).addContent( UnitOfTime.format( aDataSet.getTime( sampleIdx ) ) );
int mosiValue = ds.isMosiData() ? ds.getDataValue() : 0;
int misoValue = ds.isMisoData() ? ds.getDataValue() : 0;
// Try to coalesce equal timestamps...
if ( ( i + 1 ) < decodedData.size() )
{
final SPIData nextDS = decodedData.get( i + 1 );
if ( nextDS.getStartSampleIndex() == sampleIdx )
{
mosiValue = nextDS.isMosiData() ? nextDS.getDataValue() : mosiValue;
misoValue = nextDS.isMisoData() ? nextDS.getDataValue() : misoValue;
// Make sure to skip this DS in the next iteration...
i++;
}
}
// MOSI value first, MISO value next...
addDataValues( tr, i, sampleIdx, mosiValue );
addDataValues( tr, i, sampleIdx, misoValue );
}
}
}
return null;
}
/**
* @param aTableRow
* @param aIdx
* @param aSampleIdx
* @param aValue
*/
private void addDataValues( final Element aTableRow, final int aIdx, final int aSampleIdx, final int aValue )
{
final String mosiDataHex = StringUtils.integerToHexString( aValue, ( bitCount / 4 ) + bitAdder );
final String mosiDataBin = StringUtils.integerToBinString( aValue, bitCount );
final String mosiDataDec = String.valueOf( aValue );
final String mosiDataASCII;
if ( Character.isLetterOrDigit( aValue ) )
{
mosiDataASCII = Character.toString( ( char )aValue );
}
else
{
mosiDataASCII = "";
}
aTableRow.addChild( TD ).addContent( "0x", mosiDataHex );
aTableRow.addChild( TD ).addContent( "0b", mosiDataBin );
aTableRow.addChild( TD ).addContent( mosiDataDec );
aTableRow.addChild( TD ).addContent( mosiDataASCII );
}
};
if ( aFile == null )
{
final HtmlExporter exporter = createHtmlTemplate( ExportUtils.createHtmlExporter() );
return exporter.toString( macroResolver );
}
else
{
final HtmlFileExporter exporter = ( HtmlFileExporter )createHtmlTemplate( ExportUtils.createHtmlExporter( aFile ) );
exporter.write( macroResolver );
exporter.close();
}