00001 #ifndef __SENSORONLYMODE_H__
00002 #define __SENSORONLYMODE_H__ 1
00003
00004 #include "FeedbackSensor.h"
00005
00006 #define MAX_PARAM 8
00007 #define MAX_PACKELEN MAX_PARAM+4
00008 #define BROADCAST_ID 0xFE
00009 #define SELF_ID 200
00010
00011
00012 #define ENABLE_BIT_DEFINITIONS
00013 #define cbi(REG,BITNUM) REG &= ~(_BV(BITNUM)) // clear bit
00014 #define sbi(REG,BITNUM) REG |= _BV(BITNUM) // set bit
00015
00016 #define WORD_MAX_SIZE 32767
00017 #define WORD_MIN_SIZE -32767
00018
00019
00020 #define highbyte_s(w) (w&0x80000000?(byte)(w >> 8)|(0x8000):(byte)(w >> 8))
00021
00022
00023 #define NO_ERROR 0
00024 #define E_INPUT_VOLTAGE 1
00025 #define E_ANGLE_LIMIT 2
00026 #define E_OVERHEATING 4
00027 #define E_RANGE 8
00028 #define E_CHECKSUM 16
00029 #define E_OVERLOAD 32
00030 #define E_INSTRUCTION 64
00031
00032
00033 #define INST_PING 0x01
00034 #define INST_READ 0x02
00035 #define INST_WRITE 0x03
00036 #define INST_REG_WRITE 0x04
00037 #define INST_ACTION 0x05
00038 #define INST_RESET 0x06
00039 #define INST_DIGITAL_RESET 0x07
00040 #define INST_SYSTEM_READ 0x0C
00041 #define INST_SYSTEM_WRITE 0x0D
00042 #define INST_SYNC_WRITE 0x83
00043 #define INST_SYNC_REG_WRITE 0x84
00044
00045
00046 #define P_DIST 1
00047
00048 typedef unsigned char byte;
00049
00050
00051
00052
00053
00054 typedef struct
00055 {
00056 byte id;
00057 byte len;
00058 byte cmd;
00059 byte paramPtr;
00060 byte param[ MAX_PARAM ];
00061 byte crc;
00062 } Packet;
00063
00064
00065
00066
00067
00068 #define STATE_IDLE 0
00069 #define STATE_1ST_FF_RCVD 1
00070 #define STATE_2ND_FF_RCVD 2
00071 #define STATE_ID_RCVD 3
00072 #define STATE_LENGTH_RCVD 4
00073 #define STATE_COMMAND_RCVD 5
00074 #define STATE_PKT_COMPLETE 6
00075
00076 typedef struct
00077 {
00078 byte id;
00079 byte state;
00080 Packet pkt;
00081 void (*callback)(Packet *pkt);
00082 } Statemachine;
00083
00084
00085
00086
00087
00088
00089
00090
00091 #define init(sb) (sb.readPtr = sb.writePtr = 0)
00092 #define isEmpty(sb) (sb.writePtr == sb.readPtr)
00093 #define push(sb, value) (sb.buffer[ sb.writePtr++ ] = value)
00094 #define pop(sb) (sb.buffer[ sb.readPtr++ ])
00095 #define len(sb) ( sb.readPtr > sb.writePtr ? (sb.writePtr - sb.readPtr + 256) : (sb.writePtr - sb.readPtr) )
00096
00097 typedef struct {
00098 volatile byte readPtr;
00099 volatile byte writePtr;
00100 byte buffer[ 256 ];
00101 } Smartbuffer;
00102
00103
00104
00105
00106
00107
00108
00109
00110 #define RS485_TXD sbi(PORTD, PD5), cbi(PORTD, PD6), cbi(UCSRB,RXCIE)
00111 #define RS485_RXD sbi(PORTD, PD6), cbi(PORTD, PD5), sbi(UCSRB,RXCIE)
00112
00113 #define SET_TXD_FINISH sbi(UCSRA,TXC)
00114 #define RESET_TXD_FINISH cbi(UCSRA,TXC)
00115 #define CHECK_TXD_FINISH bit_is_set(UCSRA,TXC)
00116
00117 #define TXD_READY bit_is_set(UCSRA,UDRE)
00118 #define TXD_DATA UDR
00119 #define RXD_DATA UDR
00120
00121 #define READDIST_X TCNT0
00122 #define READDIST_Y TCNT1
00123 #define RESET_MOUSE TCNT0 = TCNT1 = 0
00124 #define RESET_WHEEL wheel1 = wheel2 = 0
00125 #define RESET_SENSOR RESET_WHEEL
00126
00127
00128
00129
00130 #define XTAL 16000000
00131 #define BAUD 1000000
00132
00133
00134 int sensorMain (void);
00135
00136 #endif