byte array in arduino library - parsing

ok so here is one for you maybe simple but i am not so sure.
i have the following code and it may be clear what i wish to do by looking at the code.
Arduino.ino
RF myRF; //Creation of RF class.
const int dataSize = 500;
byte storedData[dataSize];
//array is populated through program then the following is called
myRF.populate(storedData);
RF.CCP
const int dataSize = 500;
byte recivedData[dataSize];
void RF::populate(byte reciveddata){
recivedData = reciveddata;
}
RF.H
#include Arduino.h
#ifndef RF_H
#define RF_H
class RF {
public:
RF();
~RF();
void recive();
void send();
void print();
void sendnew(byte Storeddata);
};
#endif
this is however producing an error "byte is not declared"
Hope its clear what i intend to do and hope you can help thanks.

There are two problems in your files:
h file:
#include Arduino.h
You should write
#include <Arduino.h>
c file:
const int dataSize = 500;
byte recivedData[dataSize];
void RF::populate(byte reciveddata){
recivedData = reciveddata;
}
Here you have a big problem. You are declaring recivedData here, but you want to assign it a value coming from another part of the program. This is not how it works.
IMO you have two ways to do this.
1) store just the pointer to the memory; this way is faster and occupies less memory than solution 2, but you have to ensure that the storedData variable you pass in the .ino file is not changed during the function
#include "RF.h" // I hope you already included this
byte *recivedData;
void RF::populate(byte *reciveddata){
recivedData = reciveddata;
}
2) copy the content of the received array to this array; this way you have a copy of the array, so you occupy twice the memory (but you can edit storedData without problems).
#include "RF.h" // I hope you already included this
const int dataSize = 500;
byte recivedData[dataSize];
int recivedDataLength;
void RF::populate(byte *reciveddata, int reciveddatalength){
recivedDataLength = (reciveddatalength>dataSize) ? dataSize : reciveddatalength;
for (int i = 0; i < recivedDataLength; i++)
recivedData[i] = reciveddata[i];
}

Related

What's the difference between EEPROM.read(address) and EEPROM[address]

I'm using EEPROM on Arduino to store some large constant array. I noticed that both EEPROM.read(address) and EEPROM[address] works for my reading. But there are few documentations on the EEPROM[address] method. I also experienced occasional memory crash with that method.
EEPROM.read(address) has not been fully tested for long run. It does take more storage space when compiling. Is it safer for its behavior behind the scene?
EEPROM.read(adress) ->Read the EEPROM (address starting form 0)and send its value as unsigned char.
EEPROM[adress] -> reference eeprom cell with address
To reduce the size of the you can use avr/eeprom library , which has various function and macros for the eeprom usage. This is a reliable library and well tested.
avr/eeprom.h
Sample Code
#include <EEPROM.h>
#include <avr/eeprom.h>
void Eepromclr();
void setup() {
Serial.begin(9600);
eeprom_write_byte((void*)0,12);
int x = eeprom_read_byte((void*)0);\
Serial.println(x);
Eepromclr();
eeprom_update_byte((void*)0,6);
int y = eeprom_read_byte((void*)0);
Serial.println(y);
}
void loop() {
}
void Eepromclr() {
for (int i = 0 ; i < EEPROM.length() ; i++) {
EEPROM.write(i, 0);
}
Serial.println("Eeprom is cleared");
}
EEPROM[adress] will give you a reference to the eeprom cell while EEPROM.read(adress) will give you an unsigned char value from that cell.
In both cases you should ensure that your adress is valid.
make sure adress is >= 0 and < EEPROM.length().

How to read in 8 bytes of data from a DataInputStream and interpreted it as double in Vala

I looking for the equivalent of
java.io.DataInputStream.readDouble() for Vala.
Is it even possible?
Currently I have :
public double data;
public override void load (DataInputStream dis) throws IOError {
data = dis.read_int64 ();
}
But it just converting a int64 to a double which is not what I want.
I've tried all sort of casting and de-referencing, but nothing seems to work.
This worked for me:
int main()
{
int64 foo = 0; // Whatever value you have
double data = *(double*)(&foo); // This is where the "magic" happens
stdout.printf("%f", data);
return 0;
}
Mind you, you may have to set the correct byte order for the conversion to succeed.

c programming how to write this in main

You can write the prototypes without the variable names?
int example(examplestruct *var1, examplestruct *var2);
void done(examplestruct *var1,FILE *f);
struct {
int* field1;
int field2;
}examplestruct;
Is it possible to write the prototypes without name variables?
Can anyone tell me if this is acceptable in C language? You can write the prototypes without the variable names?
Yes.
As for the second question:
If you want a function to be inside main(), then take the body of the function, put it in main() and make sure that the arguments that the function had are well handled.
This example will clear things up:
#include <stdio.h>
void print(int);
void inc_p(int);
int main(void) {
int num = 5;
print(num);
inc_p(num);
// to get rid of inc_p(), copy paste it's body inside main
// and you will get this
// a++;
// print(a);
// However, a was an argument, here you need to use
// the variable declared in main(), i.e. 'num'
num++;
print(num);
return 0;
}
void print(int a) {
printf("%d\n", a);
}
void inc_p(int a) {
a++;
print(a);
}

How to pass ByteArray to C code using FlasCC

I want to pass ByteArray from ActionScript to C function.
basically I want do something like this:
void init() __attribute__((used,annotate("as3sig:public function init(byteData: ByteArray):int"),
annotate("as3package:example")));
void init()
{
//here I want to pass byteArray data to C variable.
//similar to AS3_GetScalarFromVar(cVar, asVar)
}
Unforunately I cannot find any function in flascc docs to help me with this.
Example:
void _init_c(void) __attribute((used,
annotate("as3sig:public function init(byteData:ByteArray) : void"),
annotate("as3import:flash.utils.ByteArray")));
void _init_c()
{
char *byteArray_c;
unsigned int len;
inline_as3("%0 = byteData.bytesAvailable;" : "=r"(len));
byteArray_c = (char *)malloc(len);
inline_as3("CModule.ram.position = %0;" : : "r"(byteArray_c));
inline_as3("byteData.readBytes(CModule.ram);");
// Now byteArray_c points to a copy of the data from byteData.
// Note that byteData.position has changed to the end of the stream.
// ... do stuff ...
free(byteArray_c);
}
The key here is that the heap in C is exposed on the AS3 side as CModule.ram, which is a ByteArray object.
A pointer malloc'd in C is seen in AS3 as an offset into CModule.ram.
You should use CModule.malloc and CModule.writeBytes methods to manipulate with pointers in C-style manner. Take a look on $FLASCC/samples/06_SWIG/PassingData/PassData.as
void _init_c(void) __attribute((used,
annotate("as3sig:public function init(byteData:ByteArray) : void"),
annotate("as3import:flash.utils.ByteArray")));
void _init_c()
{
char *byteArray_c;
unsigned int len;
inline_as3("%0 = byteData.bytesAvailable;" : "=r"(len));
byteArray_c = (char *) malloc(len);
inline_as3("byteData.readBytes(CModule.ram, %0, %1);" : : "r"(byteArray_c), "r"(len));
// Now byteArray_c points to a copy of the data from byteData.
// Note that byteData.position has changed to the end of the stream.
// ... do stuff ...
free(byteArray_c);
}

Modifying PC in jmp_buf to go to another function

For a user-lever thread library, I need to figure out jumping to a function by modifying PC value stored in jmp_buf.
This is what I have written:
jmp_buf env;
void print (void) {
printf("\nHello World!");
}
static int ptr_mangle(int p) {
unsigned int ret;
asm(" movl %1, %%eax;\n"
" xorl %%gs:0x18, %%eax;"
" roll $0x9, %%eax;"
" movl %%eax, %0;"
: "=r"(ret)
: "r"(p)
: "%eax"
);
return ret;
}
int main() {
int i = setjmp(env);
env[0].__jmpbuf[5] = ptr_mangle(print);
longjmp(env, 2);
return 0;
}
I am trying to modify PC in jmp_buf by setting it to the address of the function I am trying to jump to.
I am getting a segmentation fault.
I am unable to figure out what exactly needs to be done. Do I need to modify SP as well?
Any help would be very much appreciated.
What are you trying to do? Are you not checking for the return value of setjmp? I don't think you are doing this correctly. Have a look at the sample code below to see what would be the output be:
#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>
void subroutine(jmp_buf);
int main(void)
{
int value;
jmp_buf jumper;
value = setjmp(jumper);
if (value != 0)
{
printf("Longjmp with value %d\n", value);
exit(value);
}
printf("About to call subroutine ... \n");
subroutine(jumper);
return 0;
}
void subroutine(jmp_buf jumper)
{
longjmp(jumper,1);
}
The output would be:
About to call subroutine...
Longjmp with a value of 1.
Which begs the question - why are you trying to modify the IP? It sounds like you overwrote something or the code 'jumped' off into the woods and trampled something and came back with a hard landing i.e. segfault.
The variable env is specifically a struct, do not use an array subscript as you have done. I suspect that is why you got a segfault...
Hope this helps,
Best regards,
Tom.

Resources