-
Notifications
You must be signed in to change notification settings - Fork 0
/
fakedNsend.c
executable file
·182 lines (137 loc) · 4.23 KB
/
fakedNsend.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/////////////////s///////////////////////////////////////////////////////////////////////////////////////
//
// Nunchuck Send 0.5
// Measures Pitch, Yaw, Roll
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma platform(NXT)
long nElapsedTime = 0;
long nNearEndSent = 0;
long nNearEndRead = 0;
int nFarEndSent = 0;
int nFarEndRead = 0;
long nSendBusy = 0;
int nMessage = 0;
long nRcxNoMsg = 0;
long nReadOutOfSequence = 0; // Out Of Sequence
long nMsgXmit = 0;
byte CMDNunchukInit[] = {0x03, 0xA4, 0x40, 0x00};
byte CMDNunchukWrite[] = {0x02, 0xA4, 0x00};
byte CMDNunchukRead[] = {0x01, 0xA4};
byte outbuf[6];
int outbuf1[6]; //ubyte workaround for ROBOTC
ubyte fakeDat[17] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};
int chuck1val[5];
int chuck2val[3];
int buttonval[4];
ubyte chuck1dat[8];
ubyte buttondat[2];
// This function allows conversion of an unsigned byte to a signed int
// This is a workaround for ROBOTC
int ubyteToInt(ubyte _byte) {
int _ret = 0;
_ret = (_byte & 0x80) ? (_byte & 0x7F) + 0x80 : _byte;
return _ret;
}
// port is the NXT port, i2c_cmd is the nunchuk internal address register
// at i2c_cmd=0...5 are 6 Byte of sensor values
// at i2c_cmd=0x20 ... 0x2F you get 16 Byte of calibration data
sub NunchukGetParamter(tSensors port, byte i2c_cmd)
{
// Initialize the Nunchuk
SensorType[port] = sensorI2CCustom;
//Ask for 0 bytes
sendI2CMsg(port, CMDNunchukInit[0], 0);
while (nI2CStatus[port] == STAT_COMM_PENDING) ; // ROBOTC Wait for I2C bus to be ready
//Write at 0x00: 6 Bytes of sensor values
CMDNunchukWrite[2] =i2c_cmd;
//Write Nunchuk register address
sendI2CMsg(port, CMDNunchukWrite[0], 0);
while (nI2CStatus[port] == STAT_COMM_PENDING) ; // ROBOTC Wait for I2C bus to be ready
byte count = 6;
//Ask for count bytes
sendI2CMsg(port, CMDNunchukRead[0], count);
while (nI2CStatus[port] == STAT_COMM_PENDING) ; // Wait for I2C bus to be ready
if(nI2CBytesReady[port] == count){
//Read data from buffer
readI2CReply(port, outbuf[0], 6);
for(int i=0; i<count; i++ ){
outbuf1[i]=ubyteToInt(outbuf[i]); //ubyte workaround for ROBOTC
outbuf1[i]=(outbuf1[i]^0x17) + 0x17;
}
}
else {
// error
memset(outbuf, 0, 0);
}
}
//
// Function Prorotype Declarations
//
task sendMessages();
void checkBTLinkConnected();
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Main Task
//
////////////////////////////////////////////////////////////////////////////////////////////////////////
task main()
{
StartTask(sendMessages);
wait1Msec(1);
//return;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
// Send Messages Task
////////////////////////////////////////////////////////////////////////////////////////////////////////
task sendMessages()
{
const int kMinTimeBetweenMsg = 50;
long nInterMsgTime;
static long nLastSendTime = 0;
while (true)
{
//
// Delay between messages.
//
if ((nPgmTime - nLastSendTime) < kMinTimeBetweenMsg)
{
wait1Msec(6);
continue;
}
if (bBTBusy)
{
++nSendBusy;
wait1Msec(5);
continue;
}
++nNearEndSent;
++nMsgXmit;
//if (nMsgXmit == 0)
nMsgXmit = 1; // Don't send illegal value
ubyte krik[3] = {1,1,0};
cCmdMessageWriteToBluetooth(3,krik,3,mailbox1);
wait10Msec(10);
ubyte moop[3] = {2,2,0};
cCmdMessageWriteToBluetooth(3,moop,3,mailbox1);
wait10Msec(1);
StopAllTasks();
wait1Msec(1);
//cCmdMessageWriteToBluetooth(1,buttondat,2,mailbox1);
nLastSendTime = nPgmTime;
}
return;
}
void checkBTLinkConnected()
{
if (nBTCurrentStreamIndex >= 0)
return;
PlaySound(soundLowBuzz);
PlaySound(soundLowBuzz);
eraseDisplay();
nxtDisplayCenteredTextLine(3, "BT not");
nxtDisplayCenteredTextLine(4, "Connected");
wait1Msec(3000);
StopAllTasks();
}