Skip to content

Commit 1a5c175

Browse files
authored
serial4j/README.md: appendix section
1 parent 9e77201 commit 1a5c175

File tree

1 file changed

+85
-1
lines changed
  • electrostatic-sandbox-framework/electrostatic4j/serial4j

1 file changed

+85
-1
lines changed

electrostatic-sandbox-framework/electrostatic4j/serial4j/README.md

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,91 @@ and the [linux-kernel-hid-intro](https://docs.kernel.org/hid/hidintro.html).
111111
> ![preview](https://github.com/Software-Hardware-Codesign/Serial4j/assets/60224159/b83a2cca-e76f-4d50-8a26-5084cf02a7de)
112112
>
113113
114-
## Credits:
114+
## Appendix:
115+
116+
### Bitwise and Switching Algebra Algorithms:
117+
**Test for a specific bit pattern in a value (From Serial4j-Electrostatic4j Framework):**
118+
```java
119+
/**
120+
* Test whether a flag constant exists in this flag.
121+
*
122+
* @param flag the flag constant to test against
123+
* @return true if the flag exists, or false otherwise
124+
*/
125+
public boolean hasFlag(FlagConst flag) {
126+
return (value & flag.getValue()) == flag.getValue();
127+
}
128+
```
129+
**Disables a specific bit pattern in a value (From Serial4j-Electrostatic4j Framework):**
130+
```java
131+
/**
132+
* Disables the flags specified by this parameter.
133+
*
134+
* @param flag the flag to disable
135+
* @return this instance for chained calls
136+
*/
137+
public AppendableFlag disable(FlagConst flag) {
138+
this.value &= ~flag.getValue();
139+
return this;
140+
}
141+
```
142+
143+
**An algorithm to read analog data sent to the UART register in 8-bit frames:**
144+
> [!NOTE]
145+
> * **Idea**: Read the analog signals sent from an ADC register into the UART register over wire.
146+
> * **Formalization**:
147+
> * Let, $$F$$ be a set of frames composed of 8 bits per frame (i.e., cardinality of 8 members),
148+
> * therefore: $$F = \\{f_n: f_n = \\{Bit_0, Bit_1, Bit_2, Bit_3, Bit_4, Bit_5, Bit_6, Bit_7\\} \land n \in N\\}$$;
149+
> * Now, let $$R$$ be the resolution of the ADC register in digital decimal format (e.g., if 10-bit resolution is selected, then the equivalent decimal is $$(2^0 + 2^1 + 2^2 + 2^3 + 2^4 + 2^5 + 2^6 + 2^7 + 2^8 + 2^9) = 1023$$, and if 8-bit resolution is selected, then the equivalent decimal is $$255$$); with the assertion of the property `R mod 8 == 0` or $$M = \\{m \ is\ the\ modulo\ result\ : m = Resolution_{Assigned} - (n * Resolution_{Base}) \land m = 0 \land Resolution_{Assigned}, n \in N \land Resolution_{Base} \in [8, +\infty[ \land \log_2{Resolution_{Base}} = x \land x \in N\\}$$.
150+
> * So, both the base resolution (or the default resolution), and the assigned resolution must be a power of 2; and the assigned resolution must be an integer multiple of the base resolution.
151+
> * Let $$Reg_{UART}$$ be a hardware data register of the UART of $$|Reg_{UART}|$$-bit data register; then the number of frames required for the UART to read the data from an ADC of resolution $$R$$ (aka. the cardinality of the $$F$$) can be calculated by this formula: $$|F| = \lceil{|R| / |Reg_{UART}|}\rceil$$; which given an example of an 8-bit (255) ADC resolution signal needs 1 frame in order to be fully read by an 8-bit UART data register (i.e., $$|F| = \lceil{8/8}\rceil = 1$$); unlike the 10-bit register data, the number of frames required to transfer a data of resolution 1023 or 10-bits will be $$|F| = \lceil{10/8}\rceil = 2$$.
152+
> * Let $$i$$ be the index of the current frame; such that $$F = \\{f_i: i \in [0, \infty[ \land f_i = \\{Bit_0, Bit_1, Bit_2, Bit_3, Bit_4, Bit_5, Bit_6, Bit_7\\} \land n \in N\\}$$; then to calculate the start index of the new captured frame in a data buffer, it's reasonable to multiply the $$i$$ by the UART data register size in bits, so $$P_f = \\{p_f\ is\ the\ position\ of\ the\ new\ frame: p_f = i * buffer_{data\ register} \land i \in [0, \infty[\\}$$.
153+
> * Eventually, to place the new frame in its position, construct a left shift bitwise operation (equivalent to $$*2^{p}$$) on the new captured data frame; and add it to the old buffer using the bitwise OR operation.
154+
>
155+
```java
156+
...
157+
@Override
158+
public void receive() {
159+
super.decode(dataRegisterBufferLength -> {
160+
for (int frame = 0; terminalDevice.iread(dataRegisterBufferLength) > 0 &&
161+
frame < reportDescriptor.getReportLength(); frame++, inputClock.incrementAndGet()) {
162+
final int data = terminalDevice.getBuffer()[0];
163+
// obtain a shift-value scaled according to the current frame to place
164+
// the bits in their right position
165+
final int bits = frame * Constants.DEFAULT_DATA_REGISTER_BUFFER_LENGTH;
166+
// replace the bits to the left aka. from LSB to MSB
167+
// add the bits
168+
inputBuffer.set(inputBuffer.intValue() | (data << bits));
169+
// if the input clocks completed sending the report, flush the input and return the result
170+
if (inputClock.get() == (reportDescriptor.getReportLength() - 1)) {
171+
final int value = inputBuffer.get();
172+
inputBuffer.set(0); // flush the input buffer
173+
inputClock.set(0);
174+
return value;
175+
}
176+
}
177+
return null; // skip the decoder dispatch if no end-character is found
178+
});
179+
}
180+
...
181+
/**
182+
* Adjusts the ADC resolution in bits unit (e.g: 8 for 8-bit resolution).
183+
*
184+
* <p>
185+
* Resolution should be a result of power of two, the minimum value and the default is 8-bit.
186+
* </p>
187+
*
188+
* @param resolution the adc resolution in bits unit
189+
*/
190+
public void setResolution(int resolution) {
191+
if (resolution % 8 != 0) {
192+
throw new InvalidResolutionException("Resolution " + resolution + "-bits is not a power of two!");
193+
}
194+
((ReportDescriptor) this.reportDescriptor).setResolution(resolution);
195+
}
196+
```
197+
198+
### Credits:
115199
- [The jMonkeyEngine Platform](https://github.com/jMonkeyEngine/jmonkeyengine)
116200
- [Minie Bullet Physics By Stephen Gold](https://github.com/stephengold/Minie)
117201
- [GNU/Linux Interfaces](https://www.gnu.org/)

0 commit comments

Comments
 (0)