Note that there are some explanatory texts on larger screens.

plurals
  1. POCInt not working as expected in .Net MF
    primarykey
    data
    text
    <p>I've got something pretty noddy to cross-fade the colour of an RGB led and I'm having problems getting the desired values.</p> <p>I've stripped this example down to a bare minimum but <code>Start</code>, <code>End</code> and <code>Steps</code> will all be variable...</p> <p>This <em>should</em> linearly increment <code>R</code> (UInt) from 0 to 255 in 100 steps. I'm not too worried about precision here so I don't really care if it rounds properly or just does a <code>Floor</code>/<code>Ceiling</code></p> <pre><code> Dim Start As UInteger = 0 Dim [End] As UInteger = 255 Dim Steps = 100 For x = 1 To Steps Dim Temp = ([End] - Start) / Steps * x + Start Dim R = CUInt(Temp) Debug.Print(x.ToString &amp; ": Temp: " &amp; Temp.ToString &amp; ", R:" &amp; R.ToString) LedR.SetPulse(255, R) Threading.Thread.Sleep(20) Next </code></pre> <p>This Outputs:</p> <pre><code>1: Temp: 2.5500, R:196608 2: Temp: 5.1000, R:327680 3: Temp: 7.6500, R:524288 4: Temp: 10.2000, R:655360 5: Temp: 12.7499, R:851968 ... 95: Temp: 242.2488, R:15859712 96: Temp: 244.7988, R:16056320 97: Temp: 247.3488, R:16187392 98: Temp: 249.8988, R:16384000 99: Temp: 252.4488, R:16515072 100: Temp: 254.9988, R:16711680 </code></pre> <p>As you can see, the Temp variable is pretty-much correct, allowing for some slight imprecision but I can't see what the correlation is with <code>R</code>.</p> <p>It's possible that CUint is simply reading the bits for Temp and treating the <code>Double</code> as a <code>UInt</code> - but if so, how do I get it to convert it correctly?</p> <p>(Incidentally, this is running on a <a href="http://netduino.com/" rel="nofollow">netduino</a>), .Net Micro Framework 4.2</p> <p>Edit: Tests requested below:</p> <pre><code> Dim Test As Integer = 2 Debug.Print(Test.ToString) Debug.Print(CInt(Test).ToString) Debug.Print(CUInt(Test).ToString) 2 2 2 Dim Test As Double = 2.5 Debug.Print(Test.ToString) Debug.Print(CInt(Test).ToString) Debug.Print(CUInt(Test).ToString) 2.5000 131072 131072 </code></pre> <p>Using the following code...</p> <pre><code> For x = 0 To Steps Dim Temp As Long = ([End] - Start) \ Steps * x + Start Dim R = CType(Temp, UInteger) Debug.Print(x.ToString &amp; ": Temp: " &amp; Temp.ToString &amp; ", R:" &amp; R.ToString) LedR.SetPulse(255, R) Threading.Thread.Sleep(20) Next </code></pre> <p>I get (on the <code>Dim Temp</code> line):</p> <pre><code>An unhandled exception of type 'System.Exception' occurred in Netduino_VBTest.exe </code></pre> <p>There's no inner exception but just above it in the output is a first chance exception which I'm guessing is the root cause:</p> <pre><code>A first chance exception of type 'System.NotImplementedException' occurred in Microsoft.SPOT.Native.dll </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload