Shop OBEX P1 Docs P2 Docs Learn Events
Javelin SPI Support — Parallax Forums

Javelin SPI Support

John MJohn M Posts: 3
edited 2008-06-04 16:32 in General Discussion
I am currently teaching myself Java and came across the Javelin Stamp Starter Kit which I am considering purchasing and using in future products that I may design. But I am hesitant to use the module because I see that it really doesn't support SPI which I really need in order to use the Javelin Module in my specific application. And I must ask, whatever happened to stamp.net.* as referenced in the Al William’s book?

And I have come across the example in the manual, "Creating and Using a Library Class" which I am having a problem with.

From all my Java Books and documents concerning Java I cannot find any references in support of the example in terms of creating the object myLib that is an instantiation of the LibraryFile.

I must admit the code will compile and seems to run correctly but my Eclipse SDK also complains the following:

"The static method countToTwenty() from the type LibraryFile should be accessed in a static way".

This complaint of course is due to the fact that those methods are declared in the example with the keyword static.

And I can't see that the following statement is correct in terms of properly instantiating an object according to Java documents;

"You can simply write: static LibraryFile myLib; This declaration creates an instance of a LibraryFile object named myLib."

Doesn't this just declare and not define an object of the LibraryFile class and isn't it simply a variable that can refer to an object?

If what the manual says is correct then how can myLib be instantiated in the example without using the "new" operator which from my understanding is the only documented and correct way of creating an instance of a class in Java. I must also mention that other methods such as the following will create a new object.

String s1, s2;
s1 = "abc";
s2 = s1.concat("def"); // s2 now references "abcdef" since concat returns a new object with the concatenated result, which is assigned to s2

but I must assume that these methods are using the new operator as well.


Anyway, from my recent studies the following code below could be used in the example in the manuel from a "static" perspective and the code following the "static" code could be used from an "instantiation" perspective.


And I also discovered the following in Program Listing 3.9 - For Loops

Is -> for (int j=0;i<=10;i++) System.out.println(i);

Should be -> for (int j=0;j<=10;j++) System.out.println(j);

and on page 37 :

Is -> i-i+1

Should be -> i=i+1

and on page 43

Is -> System.out.println(buf.toString());

Could be -> System.out.println(buf); // method println() will convert object buf to a String without the need for method toString()?


Instantiation example
import examples.manual_v1_0.*;
public class ExecutableUsesLibraryFile {
    public static void main() {
        LibraryFile myLib = new LibraryFile(); // create an instance
        System.out.println("Library file displays count to 10:");
        myLib.countToTen();
    }
}

package examples.manual_v1_0;
public class LibraryFile {
    public void countToTen() {
        for (int i = 0; i<=10; i++) {
            System.out.println(i);
        }
    }
    public void countToTwenty(){
        for (int i = 0; i <= 20; i++) {
            System.out.println(i);
        }
    }
}



Static example
import examples.manual_v1_0.*;
public class ExecutableUsesLibraryFile {
    public static void main() {
        System.out.println("Library file displays count to 10:");
        LibraryFile.countToTen();
    }
}

package examples.manual_v1_0;
public class LibraryFile {
    public static void countToTen() {
        for (int i = 0; i<=10; i++) {
            System.out.println(i);
        }
    }

    public static void countToTwenty() {
        for (int i = 0; i <= 20; i++) {
            System.out.println(i);
    }
}


Post Edited By Moderator (Chris Savage (Parallax)) : 6/16/2007 10:18:48 PM GMT

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-06-16 22:13
    SPI is supported by use of CPU.shiftOut() and CPU.shiftIn() methods.
    Static methods in a library class can be referenced by name,
    for example Terminal.byteAvailable(). If such a class only
    has public methods that are static, then that class does not need
    a constructor, and hence the new keyword does not apply.

    regards peter
  • John MJohn M Posts: 3
    edited 2007-06-17 01:21
    Peter,

    Thanks for the feedback concerning what methods are used for SPI.

    Concerning SPI I was thinking more in terms of the fact that they have an I2C Class but not an SPI Class in the stamp.peripheral.io package. From a hardware design standpoint, SPI currently seems to be the favorite serial data link compared to I2C in the electronics industry. I would also bet that more integrated circuit manufactures are incorporating SPI rather than I2C. And that is the situation I have encountered. The IC I am using in my design does not support I2C. Oh well, I guess I am being lazy and will have to write an SPI Class myself once I finish learning the basics of Java programming.

    And yes, I understand the difference between using a static method without the need to create an instance and creating a object so as to call those methods of that specific class that the object was created from.

    It's the example in the manual I take issue with. The example claims the "need" to create an instance which does not use the "new" operator as explained already and as a matter of fact, since they are using static methods they don't even need an instance in the first place as my code examples demonstrate. And they are claim the need to create an instance called myLib and then proceed to to attach a static method to this object myLib. And as you and I agree, the method should be called like this:

    LibraryFile.countToTen();

    not like this:

    mylib.countToTen();

    That is my issue and as previously stated my Eclipse IDE complains too.

    Regards, John
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-06-17 02:02
    John,

    It is always good to write a special class for some piece of hardware.
    As an example of how to setup a class for your SPI device,
    take a look at the DS1302 (real time clock) class.
    http://www.parallax.com/javelin/applications.asp#AN002

    It uses the shiftIn() and shiftOut() methods to do the actual
    data transfers between device and javelin. The methods in
    that class merely provide logical functions for the abilities
    of the device.

    regards peter
  • John MJohn M Posts: 3
    edited 2007-06-17 15:31
    Peter,

    Thanks for the info and I'll take a look at AN002.

    Thanks, John
  • Andrew LovattAndrew Lovatt Posts: 1
    edited 2007-11-01 16:03
    Page 3 of the Javelin manual refers to a foreground VP for SPI master.

    The previous discussion in this thread implies no such thing exists as such, just the generic Shift methods. If it does exist, what are the differences between background and foreground VPs, and where is the documenation for the SPI master?

    Thanks,

    Andrew.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-11-01 16:39
    The shiftIn and shiftOut methods, which are native methods, perform SPI master functions.
    Usually, SPI chips also have a Chip Select pin (CS), you need to control that with CPU.writePin().
    There is not a single SPI master VP. You need to write methods for reading and writing SPI devices
    by using the 3 native methods. The SPI VP is referred to as a foreground VP, because your
    main program does not continue until a call to shiftIn or shiftOut returns.

    regards peter
  • edgellmhedgellmh Posts: 85
    edited 2008-06-04 16:20
    I can not find the file referenced in the reply by Peter Verkaik. Where are the javelin application notes stored?

    marshall edgell
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-06-04 16:32
Sign In or Register to comment.