Shop OBEX P1 Docs P2 Docs Learn Events
sending data to an array — Parallax Forums

sending data to an array

darnitdarnit Posts: 25
edited 2006-05-06 14:26 in General Discussion
I cant seem to find the proper sentax that will allow me to send the information i have in variables in my main program,·to the CRC routine (a separate class file). the crc routine initially sets up a character array and i dont know how to populate that array from the main program.


This is the data from the main program. (cmd,dlen,brightness):


while (menumode == 0){
········· while (brightness <=100){···· //increment brightness <= 100
··········· ++brightness;
··········· cmd = 14;
··········· dlen = 1;


This is where i need to send it:


public class CRC16 {
· public static char[noparse]/noparse data = new char[noparse][[/noparse]3];

·static void main() {
··· int crc;

··· data[noparse][[/noparse]0] = 14;·· //hard coded data just to get it to compile
··· data[noparse][[/noparse]1] = 1;··· //
··· data[noparse][[/noparse]2] = 12;· //

··· crc = compute(data);
··· Format.printf("%04x\n",crc);


please help!
·

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-04-26 18:17
    Important, only your main program class must have a main().
    Library classes like your CRC16 must not have a main() method.

    public class CRC16 {

    ··char[noparse]/noparse array = new char[noparse][[/noparse]3];

    · //constructor
    · public CRC16(int cmd, int dlen, int brightness) {
    ··· initArray(cmd,dlen,brightness); //set array to initial values
    · }

    · //method to setup array
    · public void initArray(int cmd, int dlen, int brightness) {
    ··· array[noparse][[/noparse]0] = (char)cmd;
    ··· array[noparse][[/noparse]1] = (char)dlen;
    ··· array[noparse][[/noparse]2] = (char)brightness;
    · }

    · //more methods here

    }

    //Your main program
    public class MainProgram {

    · static CRC16 crc = new CRC16(14,1,12); //initialize array to·cmd=14, dlen=1, brightness=12

    · static void main() {
    ··· //access CRC16 methods like crc.initArray(cmd,dlen,brightness)
    · }
    }

    Note you can not call methods in your main program class from library classes,
    unless you define abtract methods in your library class. The main program class
    then must extend the library class. I advice against this road. Better to include
    methods that access the CRC variables inside the CRC class (like initArray).

    regards peter
  • darnitdarnit Posts: 25
    edited 2006-04-26 18:49
    Thanks Peter for your help. As usual..it will take me a while to understand it.....but i do feel as if i am learning.
  • darnitdarnit Posts: 25
    edited 2006-04-27 14:56
    Peter,
    I tried to incorporate your suggestions into the code and i cant seem to get it to work.· there is probably something simple i am missing. I am attaching the two files so you can look at them.....i cant seem to get my head around this problem.·

    I am trying to send values developed in getpackets.java......send them to CRC16.java........and get the crc value back into getpackets.java so I can eventually send everything out to the lcd (my next problem).
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-04-27 15:48
    I defined the array in CRC16 public, so you can pass it to compute().
    That should do it. (Both files are changed)

    regards peter
  • Jeff DegeJeff Dege Posts: 85
    edited 2006-04-27 17:17
    [noparse][[/noparse]quote]only your main program class must have a main()

    Are you sure?

    It's a common idiom in Java to put the test code for a class in the main() method of that class. And I could swear I'd done exactly that on the Javelin.

    I was under the impression that execution begain in the main() of the class I marked for download to the Javelin, and that any main() functions in other linked classes were ignored.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-04-27 17:45
    You are right about the main().
    The class you select for download must have a main()
    and this main is the entry point.
    Any main() in other classes are just like other methods.

    I usually test a class by writing a special test class containing the main().
    So·my library classes will never have a main().
    This also ensures me, that any class that I have, that has a main(),
    is a useful program. It also keeps the libraries smaller, which is nice
    because classes are entirely included in a compilation.

    regards peter
  • darnitdarnit Posts: 25
    edited 2006-05-05 18:35
    i have run into a snag that i cant seem to solve.·· i now have to include a string in the data i am sending to the crc routine.· i am able to convert the integer to a string by way of the string buffer, however, i cant seem to get it over to the crc routine.· i have included a subset of the code in· hopes someone can shed some light on this.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-05 18:54
    You can convert a string to a char array:

    ········ mycrc = crc.compute(s.toCharArray());

    That way you pass s to method compute().
    Is that what you want? Array2 is in this way not involved.

    regards peter
  • darnitdarnit Posts: 25
    edited 2006-05-05 19:25
    my problem is...when using the sendbyte commands the lcd will display "G" instead of 71(something a real programmer would have realized ages ago). so i believe i need to convert 71 to a string and then send that to the crc so that the crc can be calculated using the bytes in the array (Arrray2) and the string "71".
    eventually i would like to use this for all messages so that i can get away from hard coding some of the commands. but.....i have to take small steps so i can learn..and with your help and others in this forum i have been learning.

    thanks
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-05 20:05
    It is still unclear to me what you want.
    Please provide a written example of input variables (eg. strings, integers)
    and then apply some crc math that leads to a crc value.
    For example input = "MYTEXT"
    Write out how to calculate its crc value.

    I think you are complicating the issue more than necessary.
    A writtenout example may provide insight in what you want
    and how to get it.
    If necessary write it out binary.

    regards peter
  • Ryan ClarkeRyan Clarke Posts: 738
    edited 2006-05-05 20:06
    I would like to echo Peter's coding style- I never put a main() in my classes. Any time I need a test I write a simple test class-

    Ryan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Ryan Clarke
    Parallax Tech Support

    RClarke@Parallax.com
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-06 14:26
    I rewrote your crc class and a test program to show how to calculate checksum
    for different kinds of data (brightness and data array).
    I made the crc class a static class with no private arrays to be filled.
    You assemble the array for which to calculate the checksum in your main class.
    This establishes·a much clearer·project buildup.

    I defined two assemble methods for two kinds of data (brightness and array).
    The test class simply calculates and displays the checksum for these data.
    Simply add more assemble methods for other kind of data you need.

    Both files go into your projectA folder.

    regards peter
    ·

    Post Edited (Peter Verkaik) : 5/6/2006 2:29:52 PM GMT
Sign In or Register to comment.