package stamp.peripheral.gps; import stamp.core.*; public class GPS_Parallax { //class for Parallax GPS receiver module (stock #28146) //currently only smart mode is supported private static final int GetInfo = 0x00; private static final int GetValid = 0x01; private static final int GetSats = 0x02; private static final int GetTime = 0x03; private static final int GetDate = 0x04; private static final int GetLat = 0x05; private static final int GetLong = 0x06; private static final int GetAlt = 0x07; private static final int GetSpeed = 0x08; private static final int GetHead = 0x09; private Uart rx; private int modePin; private boolean smartMode = true; private char[] buf = new char[5]; //public results for fast access (read only) public static int hours,minutes,seconds; public static int day,month,year; public static int latDegrees,latMinutes,latFracmin,latDirection; public static int lngDegrees,lngMinutes,lngFracmin,lngDirection; /** * Constructor * * @param rx Receive uart (also used for transmission of command) * @param modePin Pin used to select smart or raw mode (use 0 if modepin not used) */ public GPS_Parallax(Uart rx, int modePin) { this.rx = rx; this.modePin = modePin; } /* Cmd Constant Description Returned Bytes Variables (1 Byte Each Unless Noted) 0x00 GetInfo GPS Receiver Module version 2 Hardware, Firmware 0x01 GetValid Check validity of data string 1 0 = Not Valid, 1 = Valid 0x02 GetSats Number of acquired satellites (12 maximum) 1 0x03 GetTime Time (UTC/Greenwich Mean Time) 3 Hours, Minutes, Seconds 0x04 GetDate Date (UTC/Greenwich Mean Time) 3 Day, Month, Year 0x05 GetLat Latitude 5 Degrees, Minutes, Fractional Minutes (Word), Direction (0 = N, 1 = S) 0x06 GetLong Longitude 5 Degrees, Minutes, Fractional Minutes (Word), Direction (0 = E, 1 = W) 0x07 GetAlt Altitude above mean-sea-level (in tenths of meters, 65535 maximum) 2 Altitude (Word) 0x08 GetSpeed Speed (in tenths of knots) 2 Speed (Word) 0x09 GetHead Heading/direction of travel (in tenths of degrees) 2 Heading (Word) */ public int getInfo() { smartRequest(GetInfo,2); return assembleWord(); //hardware in lowbyte, firmware in highbyte } public boolean getValid() { smartRequest(GetValid,1); return (buf[0] == 0) ? false : true; } public int getSats() { smartRequest(GetSats,1); return buf[0]&0xFF; } public String getTime() { StringBuffer time = new StringBuffer(32); smartRequest(GetTime,3); hours = buf[0]&0xFF; minutes = buf[1]&0xFF; seconds = buf[2]&0xFF; time.append(hours); time.append(':'); time.append(minutes); time.append(':'); time.append(seconds); return time.toString(); } public void getDate() { smartRequest(GetDate,3); day = buf[0]&0xFF; month = buf[1]&0xFF; year = buf[2]&0xFF; } public int getLatDegrees() { smartRequest(GetLat,5); latDegrees = buf[0]&0xFF; return latDegrees; } public int getLatMinutes() { smartRequest(GetLat,5); latMinutes = buf[1]&0xFF; return latMinutes; } public int getLatFracmin() { smartRequest(GetLat,5); latFracmin = (buf[2]&0xFF)|(buf[3]<<8); return latFracmin; } public int getLatDirection() { smartRequest(GetLat,5); latDirection = buf[4]&0xFF; return latDirection; } public int getLongDegrees() { smartRequest(GetLong,5); lngDegrees = buf[0]&0xFF; return lngDegrees; } public int getLongMinutes() { smartRequest(GetLong,5); lngMinutes = buf[1]&0xFF; return lngMinutes; } public int getLongFracmin() { smartRequest(GetLong,5); lngFracmin = (buf[2]&0xFF)|(buf[3]<<8); return lngFracmin; } public int getLongDirection() { smartRequest(GetLong,5); lngDirection = buf[4]&0xFF; return lngDirection; } public int getAlt() { smartRequest(GetAlt,2); return assembleWord(); } public int getSpeed() { smartRequest(GetSpeed,2); return assembleWord(); } public int getHead() { smartRequest(GetHead,2); return assembleWord(); } private void smartRequest(int cmd, int response) { int i=0; if (modePin != 0) CPU.setInput(modePin); //enter smart mode rx.setDirection(Uart.dirTransmit); //prepare for transmit rx.sendByte('!'); rx.sendByte('G'); rx.sendByte('P'); rx.sendByte('S'); rx.sendByte(cmd); while (!rx.sendBufferEmpty()) ; //wait for transmit buffer to be empty and nothing is sent rx.setDirection(Uart.dirReceive); //prepare for receive while (i