Skip to content

Commit 20a8b8d

Browse files
authored
Merge pull request #267 from bobjacobsen/bobjacobsen-add-actions-to-CDI
Improvements to CDI element support
2 parents 63a5ac6 + d1fbff2 commit 20a8b8d

File tree

6 files changed

+504
-71
lines changed

6 files changed

+504
-71
lines changed

sample.xml

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0"?>
22
<?xml-stylesheet type="text/xsl" href="XSLT/decoder.xsl"?>
3-
<cdi>
3+
<cdi xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://openlcb.org/schema/cdi/1/4/cdi.xsd">
45

56
<identification>
67
<manufacturer>Spacely Sprockets</manufacturer>
@@ -24,30 +25,68 @@
2425
<description>The EventIDs for the consumers</description>
2526
<eventid/>
2627
<eventid/>
28+
<blob size="10" mode="readwrite">
29+
<name>Blob to see if works in group element</name>
30+
</blob>
2731
</group>
28-
<bit>
29-
<name>Sample bit variable</name>
30-
<description>Doesn't do anything</description>
31-
</bit>
3232
<int size="2">
3333
<name>Sample integer variable</name>
3434
<description>Doesn't do anything</description>
3535
<min>1</min>
3636
<max>999</max>
3737
<default>12</default>
3838
</int>
39+
<int size="2">
40+
<name>Sample integer slider</name>
41+
<description>Doesn't do anything either</description>
42+
<min>0</min>
43+
<max>1000</max>
44+
<default>12</default>
45+
<hints>
46+
<slider divisions="5" />
47+
</hints>
48+
</int>
3949
</segment>
4050

4151
<segment origin="128" space="1">
4252
<int size="1">
43-
<name>Reset</name>
44-
<description>Controls reloading and clearing node memory. Board must be restarted for this to take effect.</description>
53+
<name>Reset via Map</name>
54+
<description>
55+
Controls reloading and clearing node memory.
56+
Board must be restarted for this to take effect.
57+
</description>
4558
<map>
46-
<relation><property>85</property><value>(No reset)</value></relation>
47-
<relation><property>0</property><value>Reset all to defaults</value></relation>
48-
<relation><property>170</property><value>Reset just EventIDs to defaults</value></relation>
59+
<relation><property>0</property><value>No reset (0)</value></relation>
60+
<relation><property>85</property><value>Reset just EventIDs to defaults (85)</value></relation>
61+
<relation><property>170</property><value>Reset all to defaults (170)</value></relation>
4962
</map>
5063
</int>
64+
<int size="1" offset="-1">
65+
<name>Reset Directly</name>
66+
<description>
67+
This accesses the same memory location as the
68+
mapped variable just above.
69+
</description>
70+
</int>
71+
<action size="2">
72+
<name>Factory Reset via address 129</name>
73+
<buttonText>Perform Reset</buttonText>
74+
<dialogText>Do a factory reset?</dialogText>
75+
<value>2</value>
76+
</action>
77+
<action size="2" offset="-2">
78+
<name>Reboot via address 129</name>
79+
<buttonText>Perform Reboot</buttonText>
80+
<dialogText></dialogText> <!-- no dialog -->
81+
<value>9</value>
82+
</action>
83+
<blob size="10" mode="readwrite">
84+
<name>Blob defined at address 131</name>
85+
</blob>
86+
<int size="1">
87+
<name>Yet Another Reset</name>
88+
<description>This should be stored at address 141.</description>
89+
</int>
5190
</segment>
5291

5392
</cdi>

sample2.xml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,6 @@
3535
<eventid />
3636
<eventid />
3737
</group>
38-
<bit>
39-
<name>Regular bit variable</name>
40-
<description>Demonstrate how a standard bit (boolean) variable can be shown</description>
41-
</bit>
42-
<bit>
43-
<name>Bit variable with named states</name>
44-
<description>Demonstrate how a map relabels the states of a bit (boolean) variable</description>
45-
<map>
46-
<relation>
47-
<property>true</property>
48-
<value>Lit</value>
49-
</relation>
50-
<relation>
51-
<property>false</property>
52-
<value>Not Lit</value>
53-
</relation>
54-
</map>
55-
</bit>
5638
</segment>
5739
<segment space="1" origin="128">
5840
<name>Resets</name>

src/org/openlcb/cdi/CdiRep.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,52 @@ public static interface Map {
7979
* @return a list of all user-visible values.
8080
*/
8181
public java.util.List<String> getValues();
82+
83+
/**
84+
* Add an item to the map. Useful if e.g. a non-mapped
85+
* value is found in a location.
86+
*/
87+
public void addItemToMap(String key, String entry);
8288
}
8389

8490
public static interface EventID extends Item {
8591
}
92+
8693
public static interface IntegerRep extends Item {
8794
public int getDefault();
8895
public long getMin();
8996
public long getMax();
9097

9198
public int getSize();
99+
100+
public boolean isSliderHint();
101+
public int getSliderDivisions();
92102
}
103+
93104
public static interface BitRep extends Item {
94105
public boolean getDefault();
95106

96107
public int getSize();
97108
}
109+
110+
public static interface UnknownRep extends Item {
111+
public boolean getDefault();
112+
113+
public int getSize();
114+
}
115+
98116
public static interface StringRep extends Item { // "String" causes too many name conflicts
99117

100118
public int getSize();
101119
}
102120

121+
public static interface ActionButtonRep extends Item {
122+
123+
public long getValue();
124+
public String getButtonText();
125+
public String getDialogText();
126+
127+
public int getSize();
128+
}
129+
103130
}

src/org/openlcb/cdi/impl/ConfigRepresentation.java

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ private long processGroup(String baseName, int segment, List<CdiRep.Item> items,
253253
entry = new EventEntry(name, (CdiRep.EventID) it, segment, origin);
254254
} else if (it instanceof CdiRep.StringRep) {
255255
entry = new StringEntry(name, (CdiRep.StringRep) it, segment, origin);
256+
} else if (it instanceof CdiRep.ActionButtonRep) {
257+
entry = new ActionButtonEntry(name, (CdiRep.ActionButtonRep) it, segment, origin);
258+
} else if (it instanceof CdiRep.UnknownRep) {
259+
entry = new UnknownEntry(name, (CdiRep.UnknownRep) it, segment, origin);
256260
} else {
257261
logger.log(Level.SEVERE, "could not process CDI entry type of {0}", it);
258262
}
@@ -303,12 +307,16 @@ public void visitEntry(CdiEntry e) {
303307
visitInt((IntegerEntry) e);
304308
} else if (e instanceof EventEntry) {
305309
visitEvent((EventEntry) e);
310+
} else if (e instanceof ActionButtonEntry) {
311+
visitActionButton((ActionButtonEntry) e);
306312
} else if (e instanceof GroupRep) {
307313
visitGroupRep((GroupRep) e);
308314
} else if (e instanceof GroupEntry) {
309315
visitGroup((GroupEntry) e);
310316
} else if (e instanceof SegmentEntry) {
311317
visitSegment((SegmentEntry) e);
318+
} else if (e instanceof UnknownEntry) {
319+
visitUnknown((UnknownEntry) e);
312320
} else if (e instanceof CdiContainer) {
313321
visitContainer((CdiContainer) e);
314322
} else {
@@ -331,6 +339,10 @@ public void visitEvent(EventEntry e) {
331339
visitLeaf(e);
332340
}
333341

342+
public void visitActionButton(ActionButtonEntry e) {
343+
visitLeaf(e);
344+
}
345+
334346
public void visitGroupRep(GroupRep e) {
335347
visitContainer(e);
336348
}
@@ -343,6 +355,10 @@ public void visitSegment(SegmentEntry e) {
343355
visitContainer(e);
344356
}
345357

358+
public void visitUnknown(UnknownEntry e) {
359+
visitLeaf(e);
360+
}
361+
346362
public void visitContainer(CdiContainer c) {
347363
for (CdiEntry e : c.getEntries()) {
348364
visitEntry(e);
@@ -700,4 +716,97 @@ public void setValue(String value) {
700716
}
701717
}
702718

719+
/**
720+
* Represents an unknown variable, perhaps due to a more-recent schema
721+
*/
722+
public class UnknownEntry extends CdiEntry {
723+
public CdiRep.UnknownRep rep;
724+
725+
UnknownEntry(String name, CdiRep.UnknownRep rep, int segment, long origin) {
726+
this.key = name;
727+
this.space = segment;
728+
this.origin = origin;
729+
this.rep = rep;
730+
this.size = rep.getSize();
731+
}
732+
733+
@Override
734+
public CdiRep.Item getCdiItem() {
735+
return rep;
736+
}
737+
738+
@Override
739+
protected void updateVisibleValue() {
740+
lastVisibleValue = getValue();
741+
}
742+
743+
@Override
744+
public boolean isNullTerminated() {
745+
return size > 64;
746+
}
747+
748+
public String getValue() {
749+
MemorySpaceCache cache = getCacheForSpace(space);
750+
byte[] b = cache.read(origin, size);
751+
if (b == null) return null;
752+
// We search for a terminating null byte and clip the string there.
753+
int len = 0;
754+
while (len < b.length && b[len] != 0) ++len;
755+
byte[] rep = new byte[len];
756+
System.arraycopy(b, 0, rep, 0, len);
757+
String ret = new String(rep, UTF8);
758+
return ret;
759+
}
760+
761+
public void setValue(String value) {
762+
MemorySpaceCache cache = getCacheForSpace(space);
763+
byte[] f;
764+
f = value.getBytes(UTF8);
765+
byte[] b = new byte[Math.min(size, f.length + 1)];
766+
System.arraycopy(f, 0, b, 0, Math.min(f.length, b.length - 1));
767+
cache.write(this.origin, b, this);
768+
}
769+
}
770+
771+
/**
772+
* Represents an action button variable.
773+
*/
774+
public class ActionButtonEntry extends CdiEntry {
775+
public CdiRep.ActionButtonRep rep;
776+
777+
ActionButtonEntry(String name, CdiRep.ActionButtonRep rep, int segment, long origin) {
778+
this.key = name;
779+
this.space = segment;
780+
this.origin = origin;
781+
this.rep = rep;
782+
this.size = rep.getSize();
783+
}
784+
785+
@Override
786+
public CdiRep.Item getCdiItem() {
787+
return rep;
788+
}
789+
790+
@Override
791+
protected void updateVisibleValue() {
792+
// does nothing in this class
793+
}
794+
795+
public long getValue() {
796+
// should not be called
797+
logger.log(Level.SEVERE, "ActionButtonEntry.getValue should not be called");
798+
return -1;
799+
}
800+
801+
public void setValue(long value) {
802+
MemorySpaceCache cache = getCacheForSpace(space);
803+
byte[] b = new byte[size];
804+
for (int i = size - 1; i >= 0; --i) {
805+
b[i] = (byte)(value & 0xff);
806+
value >>= 8;
807+
}
808+
cache.write(origin, b, this);
809+
}
810+
}
811+
703812
}

0 commit comments

Comments
 (0)