B4R Question Reading UInt from PROGMEM

Johan Schoeman

Expert
Licensed User
Longtime User
Need some help please. I am reading UInt's from PROGMEM. The last value in the PROGMEM table is 0x05DD but the value logged in the IDE is 221 and not 1501 (i.e the upper byte is not taken into consideration). Can someone please show me where I am going wrong?

Thanks

JS


B4X:
Sub AStream_NewData (Buffer() As Byte)
.....
.....
.....
Dim prup(72) As UInt
    
    For i = 0 To 71
        prup(i) = GetUint(i)
        Log("UNIT = ", GetUint(i))
        Log("prup = ", prup(i))
    Next
    
End Sub

Private Sub GetUint(Index As UInt) As UInt
    Return RunNative("getdata", Index)
End Sub

#If C

#include <avr/pgmspace.h>
const PROGMEM uint16_t progup[] = { //change the data here
0x0000, 0x006D, 0x0000, 0x0022, 0x012F, 0x0098, 0x0013, 0x0013, 0x0013, 0x0039, 0x0013, 0x0039, 0x0013, 0x0013, 0x0013, 0x0039, 0x0013, 0x0039, 0x0013, 0x0039, 0x0013, 0x0039, 0x0013, 0x0039, 0x0013, 0x0013, 0x0013, 0x0013, 0x0013, 0x0039, 0x0013, 0x0013, 0x0013, 0x0039, 0x0013, 0x0013, 0x0013, 0x0013, 0x0013, 0x0013, 0x0013, 0x0039, 0x0013, 0x0013, 0x0013, 0x0013, 0x0013, 0x0039, 0x0013, 0x0013, 0x0013, 0x0039, 0x0013, 0x0039, 0x0013, 0x0039, 0x0013, 0x0013, 0x0013, 0x0039, 0x0013, 0x0039, 0x0013, 0x0013, 0x0013, 0x0013, 0x0013, 0x0013, 0x0013, 0x0013, 0x0013, 0x05DD
};

    B4R::Object beo1;

   
    B4R::Object* getdata(B4R::Object* o) {
       return beo1.wrapNumber(pgm_read_byte_near(progup + o->toLong()));
    }   

#End If
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
pgm_read_byte_near reads a single byte. You need to read two bytes.

B4X:
Private Sub GetUint(Index As UInt) As UInt
    Index = Index * 2
    Dim low As Byte = RunNative("getdata", Index)
    Index = Index + 1
    Dim high As Byte = RunNative("getdata", Index)
    Return Bit.Or(low, Bit.ShiftLeft(high, 8))
End Sub

inline code:
B4X:
B4R::Object beo1;
B4R::Object* getdata(B4R::Object* o) {
   return beo1.wrapNumber(pgm_read_byte_near((Byte*)progup + o->toLong()));
}
 
Upvote 0
Top