LMS 2012
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
c_input.c
Go to the documentation of this file.
1 /*
2  * LEGO® MINDSTORMS EV3
3  *
4  * Copyright (C) 2010-2013 The LEGO Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 
104 #include <string.h>
105 #include <math.h>
106 #include "lms2012.h"
107 #include "c_input.h"
108 #include "c_output.h"
109 #include "c_ui.h"
110 #include "c_com.h"
111 #ifndef DISABLE_DAISYCHAIN_COM_CALL
112 #include "c_daisy.h"
113 #endif
114 #include "../../c_memory/source/c_memory.h"
115 
116 #ifdef DEBUG_C_INPUT
117 #define DEBUG
118 #define DEBUG_TRACE_MODE_CHANGE
119 #define DEBUG_C_INPUT_DAISYCHAIN
120 #endif
121 
122 #if (HARDWARE != SIMULATION)
123 #include <stdio.h>
124 #include <stdlib.h>
125 #include <fcntl.h>
126 #include <unistd.h>
127 #include <sys/mman.h>
128 #include <sys/ioctl.h>
129 #include <sys/stat.h>
130 
131 #ifndef DISABLE_DAISYCHAIN_COM_CALL
132  static DATA8 DaisyBuf[64];
133  static DATA8 ActLayer = 0;
134  static DATA8 MoreLayers = 0;
135 
136 #endif
137 
138 
140 #else
141 #define snprintf _snprintf
142 #include <stdio.h>
143 #include <stdlib.h>
144 
146 
148 {
149  gInputInstance = _Instance;
150 }
151 
153 {
154  return gInputInstance;
155 }
156 #endif
157 
158 
159 #ifndef DISABLE_DAISYCHAIN
160 RESULT cInputComSetDeviceType(DATA8 Layer,DATA8 Port,DATA8 Type,DATA8 Mode);
161 #endif
162 
164 {
165 // Name Type Connection Mode DataSets Format Figures Decimals Views RawMin RawMax PctMin PctMax SiMin SiMax Time IdValue Pins Symbol
166  { "PORT ERROR" , TYPE_ERROR , CONN_ERROR , 0 , 0 , 0 , 4 , 0 , 1 , 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 'f', "" },
167  { "NONE" , TYPE_NONE , CONN_NONE , 0 , 0 , 0 , 4 , 0 , 1 , 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 'f', "" },
168  { "UNKNOWN" , TYPE_UNKNOWN , CONN_UNKNOWN , 0 , 1 , 1 , 4 , 0 , 1 , 0.0, 1023.0, 0.0, 100.0, 0.0, 1023.0, 0, 0, 'f', "" },
169  { "\0" }
170 };
171 
172 #ifdef DEBUG
173 #define LINESIZE 255
174 
175 void cInputPrintLine(char *pBuffer)
176 {
177  printf("%s",pBuffer);
178 }
179 
180 void cInputShowTypeData(void)
181 {
182  char Buffer[LINESIZE];
183  UBYTE Index;
184  DATA8 Type;
185  DATA8 Mode;
186  DATA8 Connection;
187  DATA8 LastType;
188  int Pos;
189 
190 
191  snprintf(Buffer,LINESIZE,"//! \\page types Known Device Types\r\n");
192  cInputPrintLine(Buffer);
193  snprintf(Buffer,LINESIZE,"//!\r\n");
194  cInputPrintLine(Buffer);
195  snprintf(Buffer,LINESIZE,"//! <hr size=\"1\"/>\r\n");
196  cInputPrintLine(Buffer);
197  snprintf(Buffer,LINESIZE,"//! Following devices are supported (except devices starting with //)\\n\r\n");
198  cInputPrintLine(Buffer);
199  snprintf(Buffer,LINESIZE,"//! Devices marked with * means that the data is supplied by the device itself\\n\r\n");
200  cInputPrintLine(Buffer);
201  snprintf(Buffer,LINESIZE,"//! Devices marked with # is not supported in View and Datalog apps\r\n");
202  cInputPrintLine(Buffer);
203  snprintf(Buffer,LINESIZE,"//!\r\n");
204  cInputPrintLine(Buffer);
205  snprintf(Buffer,LINESIZE,"//! \\verbatim\r\n");
206  cInputPrintLine(Buffer);
207  snprintf(Buffer,LINESIZE,"\r\n");
208  cInputPrintLine(Buffer);
209  snprintf(Buffer,LINESIZE,"// Type Mode Name DataSets Format Figures Decimals Views Conn. Pins RawMin RawMax PctMin PctMax SiMin SiMax Time IdValue Symbol\r\n");
210  cInputPrintLine(Buffer);
211  snprintf(Buffer,LINESIZE,"// ---- ---- -------- -------- ------ ------- -------- ----- ----- ---- ------- ------- ------ ------ ------- ------- ---- ------- --------\r\n");
212  cInputPrintLine(Buffer);
213 
214  LastType = TYPE_ERROR;
215  Index = 0;
216 
217 
218  while ((Index < InputInstance.MaxDeviceTypes) && (InputInstance.TypeData[Index].Name[0] != 0))
219  {
220  Type = InputInstance.TypeData[Index].Type;
221 
222  if (Type <= MAX_VALID_TYPE)
223  {
224  Mode = InputInstance.TypeData[Index].Mode;
225  Connection = InputInstance.TypeData[Index].Connection;
226 
227  if (Type != LastType)
228  {
229  snprintf(Buffer,LINESIZE,"\r\n");
230  cInputPrintLine(Buffer);
231 
232  LastType = Type;
233  }
234 
235  Pos = 0;
236 
237  if (Connection == CONN_INPUT_UART)
238  {
239  if (Mode < InputInstance.TypeData[Index].Views)
240  {
241  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"* ");
242  }
243  else
244  {
245  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"* # ");
246  }
247  }
248  else
249  {
250  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
251  }
252 
253  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"%-4u",Type);
254 
255  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
256  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"%-4u",Mode);
257 
258  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
259  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"%-*.*s",12,12,InputInstance.TypeData[Index].Name);
260 
261  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
262  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"%u",InputInstance.TypeData[Index].DataSets);
263 
264  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
265  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"%u",InputInstance.TypeData[Index].Format);
266 
267  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
268  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"%u",InputInstance.TypeData[Index].Figures);
269 
270  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
271  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"%u",InputInstance.TypeData[Index].Decimals);
272 
273  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
274  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"%u",InputInstance.TypeData[Index].Views);
275 
276  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
277  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"%3u",Connection);
278 
279  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
280  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"0x%02X",InputInstance.TypeData[Index].Pins);
281 
282  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
283  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"%8.1f",InputInstance.TypeData[Index].RawMin);
284 
285  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
286  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"%8.1f",InputInstance.TypeData[Index].RawMax);
287 
288  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
289  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"%4.0f",InputInstance.TypeData[Index].PctMin);
290 
291  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
292  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"%4.0f",InputInstance.TypeData[Index].PctMax);
293 
294  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
295  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"%8.1f",InputInstance.TypeData[Index].SiMin);
296 
297  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
298  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"%8.1f",InputInstance.TypeData[Index].SiMax);
299 
300  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
301  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"%4u",InputInstance.TypeData[Index].InvalidTime);
302 
303  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
304  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"%5u",InputInstance.TypeData[Index].IdValue);
305 
306  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos," ");
307 
308  if (InputInstance.TypeData[Index].Symbol[0])
309  {
310  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"%s",InputInstance.TypeData[Index].Symbol);
311  }
312  else
313  {
314  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"_");
315  }
316 
317  Pos += snprintf(&Buffer[Pos],LINESIZE - Pos,"\r\n");
318  cInputPrintLine(Buffer);
319  }
320  Index++;
321  }
322  snprintf(Buffer,LINESIZE,"\r\n");
323  cInputPrintLine(Buffer);
324 
325  snprintf(Buffer,LINESIZE,"//! \\endverbatim\r\n");
326  cInputPrintLine(Buffer);
327  snprintf(Buffer,LINESIZE,"//! See connection types \\ref connectiontypes \"Conn.\"\r\n");
328  cInputPrintLine(Buffer);
329  snprintf(Buffer,LINESIZE,"//! \r\n");
330  cInputPrintLine(Buffer);
331  snprintf(Buffer,LINESIZE,"//! \\n\r\n");
332  cInputPrintLine(Buffer);
333 
334 }
335 #endif
336 
337 
338 void Memcpy(void *pDest,const void *pSrc,size_t No)
339 {
340 #ifndef DISABLE_AD_WORD_PROTECT
341  UWORD *pWDest;
342  UWORD *pWSrc;
343  ULONG *pLDest;
344  ULONG *pLSrc;
345 
346  if (((No & 3) == 0) && (((int)pDest & 3) == 0) && (((int)pSrc & 3) == 0))
347  {
348  pLDest = (ULONG*)pDest;
349  pLSrc = (ULONG*)pSrc;
350  while (No)
351  {
352  *pLDest = *pLSrc;
353  pLDest++;
354  pLSrc++;
355  No -= 4;
356  }
357  }
358  else
359  {
360  if (((No & 1) == 0) && (((int)pDest & 1) == 0) && (((int)pSrc & 1) == 0))
361  {
362  pWDest = (UWORD*)pDest;
363  pWSrc = (UWORD*)pSrc;
364  while (No)
365  {
366  *pWDest = *pWSrc;
367  pWDest++;
368  pWSrc++;
369  No -= 2;
370  }
371  }
372  else
373  {
374  memcpy(pDest,pSrc,No);
375  }
376  }
377 #else
378  memcpy(pDest,pSrc,No);
379 #endif
380 }
381 
382 
383 IMGDATA CLR_LAYER_CLR_CHANGES[] = { opINPUT_DEVICE,CLR_CHANGES,0,0,opINPUT_DEVICE,CLR_CHANGES,0,1,opINPUT_DEVICE,CLR_CHANGES,0,2,opINPUT_DEVICE,CLR_CHANGES,0,3,opOBJECT_END };
384 IMGDATA CLR_LAYER_CLR_BUMBED[] = { opUI_BUTTON,FLUSH,opOBJECT_END };
385 IMGDATA CLR_LAYER_OUTPUT_RESET[] = { opOUTPUT_RESET,0,15,opOBJECT_END };
386 IMGDATA CLR_LAYER_OUTPUT_CLR_COUNT[] = { opOUTPUT_CLR_COUNT,0,15,opOBJECT_END };
387 IMGDATA CLR_LAYER_INPUT_WRITE[] = { opINPUT_WRITE,0,0,1,DEVCMD_RESET,opINPUT_WRITE,0,1,1,DEVCMD_RESET,opINPUT_WRITE,0,2,1,DEVCMD_RESET,opINPUT_WRITE,0,3,1,DEVCMD_RESET,opOBJECT_END };
388 
389 
390 void ClrLayer(void)
391 {
392  ExecuteByteCode(CLR_LAYER_CLR_CHANGES,NULL,NULL);
393  ExecuteByteCode(CLR_LAYER_CLR_BUMBED,NULL,NULL);
394  ExecuteByteCode(CLR_LAYER_OUTPUT_RESET,NULL,NULL);
395  ExecuteByteCode(CLR_LAYER_OUTPUT_CLR_COUNT,NULL,NULL);
396  ExecuteByteCode(CLR_LAYER_INPUT_WRITE,NULL,NULL);
397 }
398 
399 
400 IMGDATA STOP_LAYER[] = { opOUTPUT_PRG_STOP, opOBJECT_END };
401 
402 void StopLayer(void)
403 {
404  ExecuteByteCode(STOP_LAYER,NULL,NULL);
405 }
406 
407 
408 // DEVICE MAPPING
409 //
410 // Device 0 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
411 //
412 // Layer 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3
413 // Port (INPUT) 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 16 17 18 19 16 17 18 19 16 17 18 19 16 17 18 19
414 // Port (OUTPUT) - - - - - - - - - - - - - - - - 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
415 // Output 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
416 
417 RESULT cInputExpandDevice(DATA8 Device,DATA8 *pLayer,DATA8 *pPort,DATA8 *pOutput)
418 { // pPort: pOutput=0 -> 0..3 , pOutput=1 -> 0..3
419 
420  RESULT Result = FAIL;
421 
422  if ((Device >= 0) && (Device < DEVICES))
423  {
424  if (Device >= INPUT_DEVICES)
425  { // OUTPUT
426 
427  *pOutput = 1;
428  Device -= INPUT_DEVICES;
429  *pPort = (Device % OUTPUT_PORTS);
430  *pPort += INPUT_DEVICES;
431  }
432  else
433  { // INPUT
434 
435  *pOutput = 0;
436  *pPort = Device % INPUT_PORTS;
437 
438  }
439  *pLayer = Device / CHAIN_DEPT;
440 
441  Result = OK;
442  }
443 
444  return (Result);
445 }
446 
447 
448 RESULT cInputCompressDevice(DATA8 *pDevice,UBYTE Layer,UBYTE Port)
449 { // Port: Input 0..3 , Output 16..19
450 
451  RESULT Result = FAIL;
452 
453  if (Port >= INPUT_DEVICES)
454  { // OUTPUT
455 
456  *pDevice = (OUTPUT_PORTS * Layer);
457  *pDevice += Port;
458 
459  }
460  else
461  { // INPUT
462 
463  *pDevice = (INPUT_PORTS * Layer);
464  *pDevice += Port;
465  }
466 
467  if ((*pDevice >= 0) && (*pDevice < DEVICES))
468  {
469  Result = OK;
470  }
471 
472  return (Result);
473 }
474 
475 
476 RESULT cInputInsertNewIicString(DATA8 Type,DATA8 Mode,DATA8 *pManufacturer,DATA8 *pSensorType,DATA8 SetupLng,ULONG SetupString,DATA8 PollLng,ULONG PollString,DATA8 ReadLng)
477 {
478  RESULT Result = FAIL; // FAIL=full, OK=new, BUSY=found
479  IICSTR *pTmp;
480  UWORD Index = 0;
481 
482  if ((Type >= 0) && (Type < (MAX_DEVICE_TYPE + 1)) && (Mode >= 0) && (Mode < MAX_DEVICE_MODES))
483  { // Type and mode valid
484 
485 
486  while ((Index < InputInstance.IicDeviceTypes) && (Result != BUSY))
487  { // trying to find device type
488 
489  if ((InputInstance.IicString[Index].Type == Type) && (InputInstance.IicString[Index].Mode == Mode))
490  { // match on type and mode
491 
492  Result = BUSY;
493  }
494  Index++;
495  }
496  if (Result != BUSY)
497  { // device type not found
498 
499  if (InputInstance.IicDeviceTypes < MAX_DEVICE_TYPES)
500  { // Allocate room for a new type/mode
501 
502  if (cMemoryRealloc((void*)InputInstance.IicString,(void**)&pTmp,(DATA32)(sizeof(IICSTR) * (InputInstance.IicDeviceTypes + 1))) == OK)
503  { // Success
504 
505  InputInstance.IicString = pTmp;
506 
507  InputInstance.IicString[Index].Type = Type;
508  InputInstance.IicString[Index].Mode = Mode;
509  snprintf((char*)InputInstance.IicString[Index].Manufacturer,IIC_NAME_LENGTH + 1,"%s",(char*)pManufacturer);
510  snprintf((char*)InputInstance.IicString[Index].SensorType,IIC_NAME_LENGTH + 1,"%s",(char*)pSensorType);
511  InputInstance.IicString[Index].SetupLng = SetupLng;
512  InputInstance.IicString[Index].SetupString = SetupString;
513  InputInstance.IicString[Index].PollLng = PollLng;
514  InputInstance.IicString[Index].PollString = PollString;
515  InputInstance.IicString[Index].ReadLng = ReadLng;
516 // printf("cInputInsertNewIicString %-3u %01u IIC %u 0x%08X %u 0x%08X %s %s\r\n",Type,Mode,SetupLng,SetupString,PollLng,PollString,pManufacturer,pSensorType);
517 
518  InputInstance.IicDeviceTypes++;
519  Result = OK;
520  }
521  }
522  }
523  if (Result == FAIL)
524  { // No room for type/mode
525 
527  }
528  }
529  else
530  { // Type or mode invalid
531 
532  printf("Iic error %d: m=%d IIC\r\n",Type,Mode);
533  }
534 
535  return (Result);
536 }
537 
538 
539 RESULT cInputGetIicString(DATA8 Type,DATA8 Mode,IICSTR *IicStr)
540 {
541  RESULT Result = FAIL; // FAIL=full, OK=new, BUSY=found
542  UWORD Index = 0;
543 
544 
545  (*IicStr).SetupLng = 0;
546  (*IicStr).SetupString = 0;
547  (*IicStr).PollLng = 0;
548  (*IicStr).PollString = 0;
549  (*IicStr).ReadLng = 0;
550 
551  if ((Type >= 0) && (Type < (MAX_DEVICE_TYPE + 1)) && (Mode >= 0) && (Mode < MAX_DEVICE_MODES))
552  { // Type and mode valid
553 
554 
555  while ((Index < InputInstance.IicDeviceTypes) && (Result != OK))
556  { // trying to find device type
557 
558  if ((InputInstance.IicString[Index].Type == Type) && (InputInstance.IicString[Index].Mode == Mode))
559  { // match on type and mode
560 
561  (*IicStr).Type = Type;
562  (*IicStr).Mode = Mode;
563  snprintf((char*)IicStr->Manufacturer, sizeof(IicStr->Manufacturer),"%s", (char*)InputInstance.IicString[Index].Manufacturer);
564  snprintf((char*)IicStr->SensorType, sizeof(IicStr->SensorType), "%s",(char*)InputInstance.IicString[Index].SensorType);
565  (*IicStr).SetupLng = InputInstance.IicString[Index].SetupLng;
566  (*IicStr).SetupString = InputInstance.IicString[Index].SetupString;
567  (*IicStr).PollLng = InputInstance.IicString[Index].PollLng;
568  (*IicStr).PollString = InputInstance.IicString[Index].PollString;
569  (*IicStr).ReadLng = InputInstance.IicString[Index].ReadLng;
570 
571  Result = OK;
572  }
573  Index++;
574  }
575  }
576 
577  return (Result);
578 }
579 
580 
581 RESULT cInputGetNewTypeDataPointer(SBYTE *pName,DATA8 Type,DATA8 Mode,DATA8 Connection,TYPES **ppPlace)
582 {
583  RESULT Result = FAIL; // FAIL=full, OK=new, BUSY=found
584  UWORD Index = 0;
585 
586  *ppPlace = NULL;
587 
588  if ((Type >= 0) && (Type < (MAX_DEVICE_TYPE + 1)) && (Mode >= 0) && (Mode < MAX_DEVICE_MODES))
589  { // Type and mode valid
590 
591  while ((Index < InputInstance.MaxDeviceTypes) && (Result != BUSY))
592  { // trying to find device type
593 
594  if ((InputInstance.TypeData[Index].Type == Type) && (InputInstance.TypeData[Index].Mode == Mode) && (InputInstance.TypeData[Index].Connection == Connection))
595  { // match on type, mode and connection
596 
597  *ppPlace = &InputInstance.TypeData[Index];
598  Result = BUSY;
599  }
600  Index++;
601  }
602  if (Result != BUSY)
603  { // device type not found
604 
605  if (InputInstance.MaxDeviceTypes < MAX_DEVICE_TYPES)
606  { // Allocate room for a new type/mode
607 
608  if (cMemoryRealloc((void*)InputInstance.TypeData,(void**)ppPlace,(DATA32)(sizeof(TYPES) * (InputInstance.MaxDeviceTypes + 1))) == OK)
609  { // Success
610 
611  InputInstance.TypeData = *ppPlace;
612 
613  *ppPlace = &InputInstance.TypeData[InputInstance.MaxDeviceTypes];
614  InputInstance.TypeModes[Type]++;
615  InputInstance.MaxDeviceTypes++;
616  Result = OK;
617  }
618  }
619  }
620  if (Result == FAIL)
621  { // No room for type/mode
622 
624  }
625  }
626  else
627  { // Type or mode invalid
628 
629  printf("Type error %d: m=%d c=%d n=%s\r\n",Type,Mode,Connection,pName);
630  }
631 
632  return (Result);
633 }
634 
635 
636 RESULT cInputInsertTypeData(char *pFilename)
637 {
638  RESULT Result = FAIL;
639  LFILE *pFile;
640  char Buf[256];
641  char Name[256];
642  char Symbol[256];
643  char Manufacturer[256];
644  char SensorType[256];
645  unsigned int Type;
646  unsigned int Connection;
647  unsigned int Mode;
648  unsigned int DataSets;
649  unsigned int Format;
650  unsigned int Figures;
651  unsigned int Decimals;
652  unsigned int Views;
653  unsigned int Pins;
654  unsigned int Time;
655  unsigned int IdValue;
656  unsigned int SetupLng;
657  unsigned int SetupString;
658  unsigned int PollLng;
659  unsigned int PollString;
660  int ReadLng;
661  char *Str;
662  TYPES Tmp;
663  TYPES *pTypes;
664  int Count;
665 
666  pFile = fopen (pFilename,"r");
667  if (pFile != NULL)
668  {
669  do
670  {
671  Str = fgets(Buf,255,pFile);
672  Buf[255] = 0;
673  if (Str != NULL)
674  {
675  if ((Buf[0] != '/') && (Buf[0] != '*'))
676  {
677  Count = sscanf(Buf,"%u %u %s %u %u %u %u %u %u %x %f %f %f %f %f %f %u %u %s",&Type,&Mode,Name,&DataSets,&Format,&Figures,&Decimals,&Views,&Connection,&Pins,&Tmp.RawMin,&Tmp.RawMax,&Tmp.PctMin,&Tmp.PctMax,&Tmp.SiMin,&Tmp.SiMax,&Time,&IdValue,Symbol);
678  if (Count == TYPE_PARAMETERS)
679  {
680  Tmp.Type = (DATA8)Type;
681  Tmp.Mode = (DATA8)Mode;
682  Tmp.DataSets = (DATA8)DataSets;
683  Tmp.Format = (DATA8)Format;
684  Tmp.Figures = (DATA8)Figures;
685  Tmp.Decimals = (DATA8)Decimals;
686  Tmp.Connection = (DATA8)Connection;
687  Tmp.Views = (DATA8)Views;
688  Tmp.Pins = (DATA8)Pins;
689  Tmp.InvalidTime = (UWORD)Time;
690  Tmp.IdValue = (UWORD)IdValue;
691 
692  Result = cInputGetNewTypeDataPointer((SBYTE*)Name,(DATA8)Type,(DATA8)Mode,(DATA8)Connection,&pTypes);
693 // printf("cInputTypeDataInit\r\n");
694  if (Result == OK)
695  {
696  (*pTypes) = Tmp;
697 
698  Count = 0;
699  while ((Name[Count]) && (Count < TYPE_NAME_LENGTH))
700  {
701  if (Name[Count] == '_')
702  {
703  (*pTypes).Name[Count] = ' ';
704  }
705  else
706  {
707  (*pTypes).Name[Count] = Name[Count];
708  }
709  Count++;
710  }
711  (*pTypes).Name[Count] = 0;
712 
713  if (Symbol[0] == '_')
714  {
715  (*pTypes).Symbol[0] = 0;
716  }
717  else
718  {
719  Count = 0;
720  while ((Symbol[Count]) && (Count < SYMBOL_LENGTH))
721  {
722  if (Symbol[Count] == '_')
723  {
724  (*pTypes).Symbol[Count] = ' ';
725  }
726  else
727  {
728  (*pTypes).Symbol[Count] = Symbol[Count];
729  }
730  Count++;
731  }
732  (*pTypes).Symbol[Count] = 0;
733  }
734  if (Tmp.Connection == CONN_NXT_IIC)
735  { // NXT IIC sensor
736 
737  // setup string + poll string
738  // 3 0x01420000 2 0x01000000
739 
740  Count = sscanf(Buf,"%u %u %s %u %u %u %u %u %u %x %f %f %f %f %f %f %u %u %s %s %s %u %X %u %X %d",&Type,&Mode,Name,&DataSets,&Format,&Figures,&Decimals,&Views,&Connection,&Pins,&Tmp.RawMin,&Tmp.RawMax,&Tmp.PctMin,&Tmp.PctMax,&Tmp.SiMin,&Tmp.SiMax,&Time,&IdValue,Symbol,Manufacturer,SensorType,&SetupLng,&SetupString,&PollLng,&PollString,&ReadLng);
741  if (Count == (TYPE_PARAMETERS + 7))
742  {
743  cInputInsertNewIicString(Type,Mode,(DATA8*)Manufacturer,(DATA8*)SensorType,(DATA8)SetupLng,(ULONG)SetupString,(DATA8)PollLng,(ULONG)PollString,(DATA8)ReadLng);
744 // printf("%02u %01u IIC %u 0x%08X %u 0x%08X %u\r\n",Type,Mode,SetupLng,SetupString,PollLng,PollString,ReadLng);
745  }
746  }
747  }
748  }
749  }
750  }
751  }
752  while (Str != NULL);
753  fclose (pFile);
754 
755  Result = OK;
756  }
757 
758  return (Result);
759 }
760 
761 
763 {
764  char PrgNameBuf[255];
765  UWORD Index = 0;
766  UBYTE TypeDataFound = 0;
767 
768  // Set TypeMode to mode = 0
769  Index = 0;
770  while (Index < (MAX_DEVICE_TYPE + 1))
771  {
772  InputInstance.TypeModes[Index] = 0;
773  Index++;
774  }
775 
776  // Insert default types into TypeData
777  Index = 0;
778  while ((Index < (InputInstance.MaxDeviceTypes + 1)) && (TypeDefault[Index].Name[0] != 0))
779  {
780  InputInstance.TypeData[Index] = TypeDefault[Index];
781 
782  snprintf((char*)InputInstance.TypeData[Index].Name,TYPE_NAME_LENGTH + 1,"%s",(char*)TypeDefault[Index].Name);
783 
784  if (InputInstance.TypeData[Index].Type == TYPE_NONE)
785  {
786  InputInstance.NoneIndex = Index;
787  }
788  if (InputInstance.TypeData[Index].Type == TYPE_UNKNOWN)
789  {
790  InputInstance.UnknownIndex = Index;
791  }
792  InputInstance.TypeModes[InputInstance.TypeData[Index].Type]++;
793  Index++;
794  }
795 
796 // printf("Search start\r\n");
797  snprintf(PrgNameBuf,vmFILENAMESIZE,"%s/%s%s",vmSETTINGS_DIR,TYPEDATE_FILE_NAME,EXT_CONFIG);
798 
799  if (cInputInsertTypeData(PrgNameBuf) == OK)
800  {
801  TypeDataFound = 1;
802  }
803 
804  for (Index = TYPE_THIRD_PARTY_START;Index <= TYPE_THIRD_PARTY_END;Index++)
805  {
806  snprintf(PrgNameBuf,vmFILENAMESIZE,"%s/%s%02d%s",vmSETTINGS_DIR,TYPEDATE_FILE_NAME,Index,EXT_CONFIG);
807  if (cInputInsertTypeData(PrgNameBuf) == OK)
808  {
809  TypeDataFound = 1;
810  }
811  }
812 // printf("Done\r\n");
813 
814  if (!TypeDataFound)
815  {
817  }
818 }
819 
820 
821 RESULT cInputSetupDevice(DATA8 Device,DATA8 Repeat,DATA16 Time,DATA8 WrLng,DATA8 *pWrData,DATA8 RdLng,DATA8 *pRdData)
822 {
823 
824  InputInstance.IicDat.Result = FAIL;
825 
826  if (Device < INPUTS)
827  {
828  if (InputInstance.DeviceData[Device].Connection == CONN_NXT_IIC)
829  { // Device is an IIC device
830 
831  if (InputInstance.IicFile >= MIN_HANDLE)
832  { // Driver installed
833 
834  if (WrLng > MAX_DEVICE_DATALENGTH)
835  {
836  WrLng = MAX_DEVICE_DATALENGTH;
837  }
838  if (RdLng > MAX_DEVICE_DATALENGTH)
839  {
840  RdLng = MAX_DEVICE_DATALENGTH;
841  }
842 
843  if (Time != 0)
844  {
845  if (Time < MIN_IIC_REPEAT_TIME)
846  {
847  Time = MIN_IIC_REPEAT_TIME;
848  }
849  if (Time > MAX_IIC_REPEAT_TIME)
850  {
851  Time = MAX_IIC_REPEAT_TIME;
852  }
853  }
854 
855  InputInstance.IicDat.Result = BUSY;
856  InputInstance.IicDat.Port = Device;
857  InputInstance.IicDat.Repeat = Repeat;
858  InputInstance.IicDat.Time = Time;
859  InputInstance.IicDat.WrLng = WrLng;
860  InputInstance.IicDat.RdLng = RdLng;
861 
862  Memcpy(&InputInstance.IicDat.WrData[0],pWrData,InputInstance.IicDat.WrLng);
863 
864  ioctl(InputInstance.IicFile,IIC_SETUP,&InputInstance.IicDat);
865 
866  if (InputInstance.IicDat.Result == OK)
867  {
868  Memcpy(pRdData,&InputInstance.IicDat.RdData[0],InputInstance.IicDat.RdLng);
869  }
870  }
871  }
872  }
873 
874  return (InputInstance.IicDat.Result);
875 }
876 
877 
878 #ifndef DISABLE_DAISYCHAIN
879 /* Daisy chain interface
880 
881 c_com
882 RESULT cComSetDeviceInfo(DATA8 Length,UBYTE *pInfo); // Write type data to chain
883 RESULT cComGetDeviceInfo(DATA8 Length,UBYTE *pInfo); // Read type data from chain
884 RESULT cComSetDeviceType(DATA8 Layer,DATA8 Port,DATA8 Type,DATA8 Mode); // Write mode to chain
885 RESULT cComGetDeviceData(DATA8 Layer,DATA8 Port,DATA8 Length,DATA8 *pType,DATA8 *pMode,DATA8 *pData); // Read device data from chain
886 
887 c_input
888 RESULT cInputGetDeviceData(DATA8 Layer,DATA8 Port,DATA8 Length,DATA8 *pType,DATA8 *pMode,DATA8 *pData); // Chain read device data
889 */
890 #endif
891 
892 RESULT cInputGetData(DATA8 Layer,DATA8 Port,DATA16 Time,DATA16 *pInit,DATA8 Length,DATA8 *pType,DATA8 *pMode,DATA8 *pData);
893 
894 
895 RESULT cInputFindDumbInputDevice(DATA8 Device,DATA8 Type,DATA8 Mode,UWORD *pTypeIndex)
896 {
897  RESULT Result = FAIL;
898  UWORD IdValue = 0;
899  UWORD Index = 0;
900  UWORD Tmp;
901 
902  // get actual id value
903  IdValue = CtoV((*InputInstance.pAnalog).InPin1[Device]);
904 
905  while ((Index < InputInstance.MaxDeviceTypes) && (Result != OK))
906  {
907  Tmp = InputInstance.TypeData[Index].IdValue;
908 
909  if (Tmp >= IN1_ID_HYSTERESIS)
910  {
911  if ((IdValue >= (Tmp - IN1_ID_HYSTERESIS)) && (IdValue < (Tmp + IN1_ID_HYSTERESIS)))
912  { // id value match
913 
914  if (Type == TYPE_UNKNOWN)
915  { // first search
916 
917  // "type data" entry found
918  *pTypeIndex = Index;
919 
920  }
921  else
922  { // next search
923 
924  if (Type == InputInstance.TypeData[Index].Type)
925  { //
926 
927  *pTypeIndex = Index;
928  }
929  }
930  if (Mode == InputInstance.TypeData[Index].Mode)
931  { // mode match
932 
933  // "type data" entry found
934  *pTypeIndex = Index;
935 
936  // skip looping
937  Result = OK;
938  }
939  }
940  }
941  Index++;
942  }
943 
944  return (Result);
945 }
946 
947 
948 RESULT cInputFindDumbOutputDevice(DATA8 Device,DATA8 Type,DATA8 Mode,UWORD *pTypeIndex)
949 {
950  RESULT Result = FAIL;
951  UWORD IdValue = 0;
952  UWORD Index = 0;
953  UWORD Tmp;
954 
955  // get actual id value
956  IdValue = (*InputInstance.pAnalog).OutPin5Low[Device - INPUT_DEVICES];
957 
958  while ((Index < InputInstance.MaxDeviceTypes) && (Result != OK))
959  {
960  Tmp = InputInstance.TypeData[Index].IdValue;
961 
962  if (Tmp >= OUT5_ID_HYSTERESIS)
963  {
964  if ((IdValue >= (Tmp - OUT5_ID_HYSTERESIS)) && (IdValue < (Tmp + OUT5_ID_HYSTERESIS)))
965  { // id value match
966 
967  // "type data" entry found
968  *pTypeIndex = Index;
969 
970  if (Mode == InputInstance.TypeData[Index].Mode)
971  { // mode match
972 
973  // "type data" entry found
974  *pTypeIndex = Index;
975 
976  // skip looping
977  Result = OK;
978  }
979  }
980  }
981  Index++;
982  }
983 
984  return (Result);
985 }
986 
987 
988 RESULT cInputFindDevice(DATA8 Type,DATA8 Mode,UWORD *pTypeIndex)
989 {
990  RESULT Result = FAIL;
991  UWORD Index = 0;
992 
993  while ((Index < InputInstance.MaxDeviceTypes) && (Result != OK))
994  {
995  if (Type == InputInstance.TypeData[Index].Type)
996  { // type match
997 
998  *pTypeIndex = Index;
999 
1000  if (Mode == InputInstance.TypeData[Index].Mode)
1001  { // mode match
1002 
1003  // "type data" entry found
1004  *pTypeIndex = Index;
1005 
1006  // skip looping
1007  Result = OK;
1008  }
1009  }
1010  Index++;
1011  }
1012 
1013  return (Result);
1014 }
1015 
1016 
1017 void cInputResetDevice(DATA8 Device,UWORD TypeIndex)
1018 {
1019  PRGID TmpPrgId;
1020 
1021  InputInstance.DeviceType[Device] = InputInstance.TypeData[TypeIndex].Type;
1022  InputInstance.DeviceMode[Device] = InputInstance.TypeData[TypeIndex].Mode;
1023 
1024  InputInstance.DeviceData[Device].InvalidTime = InputInstance.TypeData[TypeIndex].InvalidTime;
1025  InputInstance.DeviceData[Device].DevStatus = BUSY;
1026 
1027  // save new type
1028  InputInstance.DeviceData[Device].TypeIndex = TypeIndex;
1029 
1030  if (InputInstance.DeviceType[Device] != TYPE_UNKNOWN)
1031  {
1032  // configuration changed
1033  for (TmpPrgId = 0;TmpPrgId < MAX_PROGRAMS;TmpPrgId++)
1034  {
1035  InputInstance.ConfigurationChanged[TmpPrgId]++;
1036  }
1037  }
1038 }
1039 
1040 
1041 void cInputSetDeviceType(DATA8 Device,DATA8 Type, DATA8 Mode,int Line)
1042 {
1043  UWORD Index;
1044  char Buf[INPUTS * 2 + 1];
1045  UWORD TypeIndex;
1046  DATA8 Layer;
1047  DATA8 Port;
1048  DATA8 Output;
1049 
1050  if (cInputExpandDevice(Device,&Layer,&Port,&Output) == OK)
1051  { // Device within range
1052 
1053  if (InputInstance.DeviceData[Device].Connection == CONN_NONE)
1054  {
1055  Type = TYPE_NONE;
1056  }
1057  // default type is unknown!
1058  TypeIndex = InputInstance.UnknownIndex;
1059 
1060  if (Layer == 0)
1061  { // Local device
1062 
1063  if (Output == 0)
1064  { // Input device
1065 
1066  // TRY TO FIND DUMB INPUT DEVICE
1067  if (InputInstance.DeviceData[Device].Connection == CONN_INPUT_DUMB)
1068  { // search "type data" for matching "dumb" input device
1069 
1070  cInputFindDumbInputDevice(Device,Type,Mode,&TypeIndex);
1071  }
1072 
1073  // IF NOT FOUND YET - TRY TO FIND TYPE ANYWAY
1074  if (TypeIndex == InputInstance.UnknownIndex)
1075  { // device not found or not "dumb" input/output device
1076 
1077  cInputFindDevice(Type,Mode,&TypeIndex);
1078  }
1079 
1080  if (InputInstance.DeviceData[Device].TypeIndex != TypeIndex)
1081  { // type or mode has changed
1082 
1083  cInputResetDevice(Device,TypeIndex);
1084 
1085  (*InputInstance.pUart).Status[Device] &= ~UART_DATA_READY;
1086  (*InputInstance.pIic).Status[Device] &= ~IIC_DATA_READY;
1087  (*InputInstance.pAnalog).Updated[Device] = 0;
1088 
1089  if (InputInstance.DeviceData[Device].Connection == CONN_NXT_IIC)
1090  { // Device is an IIC device
1091 
1092  cInputGetIicString(InputInstance.DeviceType[Device],InputInstance.DeviceMode[Device],&InputInstance.IicStr);
1093  InputInstance.IicStr.Port = Device;
1094  InputInstance.IicStr.Time = InputInstance.DeviceData[Device].InvalidTime;
1095 
1096  if ((InputInstance.IicStr.SetupLng) || (InputInstance.IicStr.PollLng))
1097  {
1098 // printf("%u %-4u %-3u %01u IIC %u 0x%08X %u 0x%08X %d\r\n",InputInstance.IicStr.Port,InputInstance.IicStr.Time,InputInstance.IicStr.Type,InputInstance.IicStr.Mode,InputInstance.IicStr.SetupLng,InputInstance.IicStr.SetupString,InputInstance.IicStr.PollLng,InputInstance.IicStr.PollString,InputInstance.IicStr.ReadLng);
1099 
1100  if (InputInstance.IicFile >= MIN_HANDLE)
1101  {
1102  ioctl(InputInstance.IicFile,IIC_SET,&InputInstance.IicStr);
1103  }
1104  }
1105  }
1106 
1107  // SETUP DRIVERS
1108  for (Index = 0;Index < INPUTS;Index++)
1109  { // Initialise pin setup string to do nothing
1110 
1111  Buf[Index] = '-';
1112  }
1113  Buf[Index] = 0;
1114 
1115  // insert "pins" in setup string
1116  Buf[Device] = InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].Pins;
1117 
1118  // write setup string to "Device Connection Manager" driver
1119  if (InputInstance.DcmFile >= MIN_HANDLE)
1120  {
1121  write(InputInstance.DcmFile,Buf,INPUTS);
1122  }
1123 
1124  for (Index = 0;Index < INPUTS;Index++)
1125  { // build setup string for UART and IIC driver
1126 
1127  InputInstance.DevCon.Connection[Index] = InputInstance.DeviceData[Index].Connection;
1128  InputInstance.DevCon.Type[Index] = InputInstance.TypeData[InputInstance.DeviceData[Index].TypeIndex].Type;
1129  InputInstance.DevCon.Mode[Index] = InputInstance.TypeData[InputInstance.DeviceData[Index].TypeIndex].Mode;
1130  }
1131 
1132  // write setup string to "UART Device Controller" driver
1133  if (InputInstance.UartFile >= MIN_HANDLE)
1134  {
1135  ioctl(InputInstance.UartFile,UART_SET_CONN,&InputInstance.DevCon);
1136  }
1137  if (InputInstance.IicFile >= MIN_HANDLE)
1138  {
1139  ioctl(InputInstance.IicFile,IIC_SET_CONN,&InputInstance.DevCon);
1140  }
1141 
1142 #ifdef DEBUG_TRACE_MODE_CHANGE
1143  printf("c_input cInputSetDeviceType: I D=%-3d C=%-3d Ti=%-3d N=%s\r\n",Device,InputInstance.DeviceData[Device].Connection,InputInstance.DeviceData[Device].TypeIndex,InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].Name);
1144 #endif
1145  }
1146  }
1147  else
1148  { // Output device
1149 
1150  // TRY TO FIND DUMB OUTPUT DEVICE
1151  if (InputInstance.DeviceData[Device].Connection == CONN_OUTPUT_DUMB)
1152  { // search "type data" for matching "dumb" output device
1153 
1154  cInputFindDumbInputDevice(Device,Type,Mode,&TypeIndex);
1155  }
1156 
1157  // IF NOT FOUND YET - TRY TO FIND TYPE ANYWAY
1158  if (TypeIndex == InputInstance.UnknownIndex)
1159  { // device not found or not "dumb" input/output device
1160 
1161  cInputFindDevice(Type,Mode,&TypeIndex);
1162  }
1163 
1164  if (InputInstance.DeviceData[Device].TypeIndex != TypeIndex)
1165  { // type or mode has changed
1166 
1167  cInputResetDevice(Device,TypeIndex);
1168 
1169  for (Index = 0;Index < OUTPUT_PORTS;Index++)
1170  { // build setup string "type" for output
1171 
1172  Buf[Index] = InputInstance.DeviceType[Index + INPUT_DEVICES];
1173  }
1174  Buf[Index] = 0;
1175  cOutputSetTypes(Buf);
1176 
1177 #ifdef DEBUG_TRACE_MODE_CHANGE
1178  printf("c_input cInputSetDeviceType: O D=%-3d C=%-3d Ti=%-3d N=%s\r\n",Device,InputInstance.DeviceData[Device].Connection,InputInstance.DeviceData[Device].TypeIndex,InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].Name);
1179 #endif
1180  }
1181  }
1182 
1183 
1184  }
1185  else
1186  { // Not local device
1187 
1188  // IF NOT FOUND YET - TRY TO FIND TYPE ANYWAY
1189  if (TypeIndex == InputInstance.UnknownIndex)
1190  { // device not found or not "dumb" input/output device
1191 
1192  cInputFindDevice(Type,Mode,&TypeIndex);
1193  }
1194 
1195  if (InputInstance.DeviceData[Device].TypeIndex != TypeIndex)
1196  { // type or mode has changed
1197 
1198 #ifndef DISABLE_DAISYCHAIN
1199 
1200  if ((InputInstance.DeviceData[Device].Connection != CONN_NONE) && (InputInstance.DeviceData[Device].Connection != CONN_ERROR) && (InputInstance.DeviceType[Device] != TYPE_UNKNOWN))
1201  {
1202  if (cDaisyReady() != BUSY)
1203  {
1204 
1205  cInputResetDevice(Device,TypeIndex);
1206  InputInstance.DeviceData[Device].InvalidTime = DAISYCHAIN_MODE_TIME;
1207  cInputComSetDeviceType(Layer,Port,InputInstance.DeviceType[Device],InputInstance.DeviceMode[Device]);
1208 
1209 #ifdef DEBUG_TRACE_MODE_CHANGE
1210  printf("c_input cInputSetDeviceType: D D=%-3d C=%-3d Ti=%-3d N=%s\r\n",Device,InputInstance.DeviceData[Device].Connection,InputInstance.DeviceData[Device].TypeIndex,InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].Name);
1211 #endif
1212 
1213 #ifdef ENABLE_STATUS_TEST
1214  if (Device == TESTDEVICE)
1215  {
1216  VMInstance.Status &= ~0x08;
1217  }
1218 #endif
1219  }
1220  else
1221  {
1222 #ifdef ENABLE_STATUS_TEST
1223  if (Device == TESTDEVICE)
1224  {
1225  VMInstance.Status |= 0x08;
1226  }
1227 #endif
1228  InputInstance.DeviceData[Device].DevStatus = BUSY;
1229  }
1230  }
1231  else
1232  {
1233  cInputResetDevice(Device,TypeIndex);
1234  }
1235 
1236 #else
1237 
1238  cInputResetDevice(Device,TypeIndex);
1239 
1240 #ifdef DEBUG_TRACE_MODE_CHANGE
1241  printf("c_input cInputSetDeviceType: D D=%-3d C=%-3d Ti=%-3d N=%s\r\n",Device,InputInstance.DeviceData[Device].Connection,InputInstance.DeviceData[Device].TypeIndex,InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].Name);
1242 #endif
1243 #endif
1244 
1245  }
1246  }
1247 #ifdef BUFPRINTSIZE
1248  BufPrint('p',"%-4d - D=%d SetDevice T=%-3d M=%d C=%-3d\r\n",Line,Device,InputInstance.DeviceType[Device],InputInstance.DeviceMode[Device],InputInstance.DeviceData[Device].Connection);
1249 #endif
1250  }
1251 }
1252 
1253 
1255 {
1256  DATA8 Type;
1257  DATA8 Mode;
1258  int File;
1259  char PrgNameBuf[vmFILENAMESIZE];
1260 
1261  snprintf(PrgNameBuf,vmFILENAMESIZE,"%s/%s%s",vmSETTINGS_DIR,vmCALDATA_FILE_NAME,vmEXT_CONFIG);
1262  File = open(PrgNameBuf,O_RDONLY);
1263  if (File >= MIN_HANDLE)
1264  {
1265  if (read(File,(UBYTE*)&InputInstance.Calib,sizeof(InputInstance.Calib)) != sizeof(InputInstance.Calib))
1266  {
1267  close (File);
1268  File = -1;
1269  }
1270  else
1271  {
1272  close (File);
1273  }
1274  }
1275  if (File < 0)
1276  {
1277  for (Type = 0;Type < (MAX_DEVICE_TYPE);Type++)
1278  {
1279  for (Mode = 0;Mode < MAX_DEVICE_MODES;Mode++)
1280  {
1281  InputInstance.Calib[Type][Mode].InUse = 0;
1282  }
1283  }
1284  }
1285 }
1286 
1287 
1289 {
1290  int File;
1291  char PrgNameBuf[vmFILENAMESIZE];
1292 
1293  snprintf(PrgNameBuf,vmFILENAMESIZE,"%s/%s%s",vmSETTINGS_DIR,vmCALDATA_FILE_NAME,vmEXT_CONFIG);
1294  File = open(PrgNameBuf,O_CREAT | O_WRONLY | O_TRUNC,SYSPERMISSIONS);
1295  if (File >= MIN_HANDLE)
1296  {
1297  write(File,(UBYTE*)&InputInstance.Calib,sizeof(InputInstance.Calib));
1298  close(File);
1299  }
1300 }
1301 
1302 
1303 #ifndef DISABLE_OLD_COLOR
1304 
1305 #define FALSE 0
1306 #define TRUE 1
1307 
1308 #define SENSOR_RESOLUTION 1023L
1309 
1310 /* Remember this is ARM AD converter - 3,3 VDC as max voltage */
1311 /* When in color mode background value is substracted => min = 0!!! */
1312 #define AD_MAX 2703L
1313 #define AD_FS 3300L
1314 
1315 #define COLORSENSORBGMIN (214/(AD_FS/AD_MAX))
1316 #define COLORSENSORMIN (1L/(AD_FS/AD_MAX)) /* 1 inserted else div 0 (1L/(120/AD_MAX)) */
1317 #define COLORSENSORMAX ((AD_MAX * AD_FS)/3300)
1318 #define COLORSENSORPCTDYN (UBYTE)(((COLORSENSORMAX - COLORSENSORMIN) * 100L)/AD_MAX)
1319 #define COLORSENSORBGPCTDYN (UBYTE)(((COLORSENSORMAX - COLORSENSORBGMIN) * 100L)/AD_MAX)
1320 
1321 void cInputCalcFullScale(UWORD *pRawVal, UWORD ZeroPointOffset, UBYTE PctFullScale, UBYTE InvStatus)
1322 {
1323  if (*pRawVal >= ZeroPointOffset)
1324  {
1325  *pRawVal -= ZeroPointOffset;
1326  }
1327  else
1328  {
1329  *pRawVal = 0;
1330  }
1331 
1332  *pRawVal = (*pRawVal * 100)/PctFullScale;
1333  if (*pRawVal > SENSOR_RESOLUTION)
1334  {
1335  *pRawVal = SENSOR_RESOLUTION;
1336  }
1337  if (TRUE == InvStatus)
1338  {
1339  *pRawVal = SENSOR_RESOLUTION - *pRawVal;
1340  }
1341 }
1342 
1343 
1344 void cInputCalibrateColor(COLORSTRUCT *pC, UWORD *pNewVals)
1345 {
1346  UBYTE CalRange;
1347 
1348  if ((pC->ADRaw[BLANK]) < pC->CalLimits[1])
1349  {
1350  CalRange = 2;
1351  }
1352  else
1353  {
1354  if ((pC->ADRaw[BLANK]) < pC->CalLimits[0])
1355  {
1356  CalRange = 1;
1357  }
1358  else
1359  {
1360  CalRange = 0;
1361  }
1362  }
1363 
1364  pNewVals[RED] = 0;
1365  if ((pC->ADRaw[RED]) > (pC->ADRaw[BLANK]))
1366  {
1367  pNewVals[RED] = (UWORD)(((ULONG)((pC->ADRaw[RED]) - (pC->ADRaw[BLANK])) * (pC->Calibration[CalRange][RED])) >> 16);
1368  }
1369 
1370  pNewVals[GREEN] = 0;
1371  if ((pC->ADRaw[GREEN]) > (pC->ADRaw[BLANK]))
1372  {
1373  pNewVals[GREEN] = (UWORD)(((ULONG)((pC->ADRaw[GREEN]) - (pC->ADRaw[BLANK])) * (pC->Calibration[CalRange][GREEN])) >> 16);
1374  }
1375 
1376  pNewVals[BLUE] = 0;
1377  if ((pC->ADRaw[BLUE]) > (pC->ADRaw[BLANK]))
1378  {
1379  pNewVals[BLUE] = (UWORD)(((ULONG)((pC->ADRaw[BLUE]) -(pC->ADRaw[BLANK])) * (pC->Calibration[CalRange][BLUE])) >> 16);
1380  }
1381 
1382  pNewVals[BLANK] = (pC->ADRaw[BLANK]);
1384  (pNewVals[BLANK]) = (UWORD)(((ULONG)(pNewVals[BLANK]) * (pC->Calibration[CalRange][BLANK])) >> 16);
1385 }
1386 
1387 
1388 DATAF cInputCalculateColor(COLORSTRUCT *pC)
1389 {
1390  DATAF Result ;
1391 
1392 
1393  Result = DATAF_NAN;
1394 
1395  // Color Sensor values has been calculated -
1396  // now calculate the color and put it in Sensor value
1397  if (((pC->SensorRaw[RED]) > (pC->SensorRaw[BLUE] )) &&
1398  ((pC->SensorRaw[RED]) > (pC->SensorRaw[GREEN])))
1399  {
1400 
1401  // If all 3 colors are less than 65 OR (Less that 110 and bg less than 40)
1402  if (((pC->SensorRaw[RED]) < 65) ||
1403  (((pC->SensorRaw[BLANK]) < 40) && ((pC->SensorRaw[RED]) < 110)))
1404  {
1405  Result = (DATAF)BLACKCOLOR;
1406  }
1407  else
1408  {
1409  if (((((pC->SensorRaw[BLUE]) >> 2) + ((pC->SensorRaw[BLUE]) >> 3) + (pC->SensorRaw[BLUE])) < (pC->SensorRaw[GREEN])) &&
1410  ((((pC->SensorRaw[GREEN]) << 1)) > (pC->SensorRaw[RED])))
1411  {
1412  Result = (DATAF)YELLOWCOLOR;
1413  }
1414  else
1415  {
1416 
1417  if ((((pC->SensorRaw[GREEN]) << 1) - ((pC->SensorRaw[GREEN]) >> 2)) < (pC->SensorRaw[RED]))
1418  {
1419 
1420  Result = (DATAF)REDCOLOR;
1421  }
1422  else
1423  {
1424 
1425  if ((((pC->SensorRaw[BLUE]) < 70) ||
1426  ((pC->SensorRaw[GREEN]) < 70)) ||
1427  (((pC->SensorRaw[BLANK]) < 140) && ((pC->SensorRaw[RED]) < 140)))
1428  {
1429  Result = (DATAF)BLACKCOLOR;
1430  }
1431  else
1432  {
1433  Result = (DATAF)WHITECOLOR;
1434  }
1435  }
1436  }
1437  }
1438  }
1439  else
1440  {
1441 
1442  // Red is not the dominant color
1443  if ((pC->SensorRaw[GREEN]) > (pC->SensorRaw[BLUE]))
1444  {
1445 
1446  // Green is the dominant color
1447  // If all 3 colors are less than 40 OR (Less that 70 and bg less than 20)
1448  if (((pC->SensorRaw[GREEN]) < 40) ||
1449  (((pC->SensorRaw[BLANK]) < 30) && ((pC->SensorRaw[GREEN]) < 70)))
1450  {
1451  Result = (DATAF)BLACKCOLOR;
1452  }
1453  else
1454  {
1455  if ((((pC->SensorRaw[BLUE]) << 1)) < (pC->SensorRaw[RED]))
1456  {
1457  Result = (DATAF)YELLOWCOLOR;
1458  }
1459  else
1460  {
1461  if ((((pC->SensorRaw[RED]) + ((pC->SensorRaw[RED])>>2)) < (pC->SensorRaw[GREEN])) ||
1462  (((pC->SensorRaw[BLUE]) + ((pC->SensorRaw[BLUE])>>2)) < (pC->SensorRaw[GREEN])))
1463  {
1464  Result = (DATAF)GREENCOLOR;
1465  }
1466  else
1467  {
1468  if ((((pC->SensorRaw[RED]) < 70) ||
1469  ((pC->SensorRaw[BLUE]) < 70)) ||
1470  (((pC->SensorRaw[BLANK]) < 140) && ((pC->SensorRaw[GREEN]) < 140)))
1471  {
1472  Result = (DATAF)BLACKCOLOR;
1473  }
1474  else
1475  {
1476  Result = (DATAF)WHITECOLOR;
1477  }
1478  }
1479  }
1480  }
1481  }
1482  else
1483  {
1484 
1485  // Blue is the most dominant color
1486  // Colors can be blue, white or black
1487  // If all 3 colors are less than 48 OR (Less that 85 and bg less than 25)
1488  if (((pC->SensorRaw[BLUE]) < 48) ||
1489  (((pC->SensorRaw[BLANK]) < 25) && ((pC->SensorRaw[BLUE]) < 85)))
1490  {
1491  Result = (DATAF)BLACKCOLOR;
1492  }
1493  else
1494  {
1495  if ((((((pC->SensorRaw[RED]) * 48) >> 5) < (pC->SensorRaw[BLUE])) &&
1496  ((((pC->SensorRaw[GREEN]) * 48) >> 5) < (pC->SensorRaw[BLUE])))
1497  ||
1498  (((((pC->SensorRaw[RED]) * 58) >> 5) < (pC->SensorRaw[BLUE])) ||
1499  ((((pC->SensorRaw[GREEN]) * 58) >> 5) < (pC->SensorRaw[BLUE]))))
1500  {
1501  Result = (DATAF)BLUECOLOR;
1502  }
1503  else
1504  {
1505 
1506  // Color is white or Black
1507  if ((((pC->SensorRaw[RED]) < 60) ||
1508  ((pC->SensorRaw[GREEN]) < 60)) ||
1509  (((pC->SensorRaw[BLANK]) < 110) && ((pC->SensorRaw[BLUE]) < 120)))
1510  {
1511  Result = (DATAF)BLACKCOLOR;
1512  }
1513  else
1514  {
1515  if ((((pC->SensorRaw[RED]) + ((pC->SensorRaw[RED]) >> 3)) < (pC->SensorRaw[BLUE])) ||
1516  (((pC->SensorRaw[GREEN]) + ((pC->SensorRaw[GREEN]) >> 3)) < (pC->SensorRaw[BLUE])))
1517  {
1518  Result = (DATAF)BLUECOLOR;
1519  }
1520  else
1521  {
1522  Result = (DATAF)WHITECOLOR;
1523  }
1524  }
1525  }
1526  }
1527  }
1528  }
1529 
1530 
1531  return (Result);
1532 }
1533 
1534 
1535 #ifndef DISABLE_DAISYCHAIN
1536 
1537 RESULT cInputGetColor(DATA8 Device,DATA8 *pData)
1538 {
1539  RESULT Result = FAIL;
1540 
1541  cInputCalibrateColor(&(*InputInstance.pAnalog).NxtCol[Device],(*InputInstance.pAnalog).NxtCol[Device].SensorRaw);
1542 
1543  switch (InputInstance.DeviceMode[Device])
1544  {
1545  case 2 :
1546  { // NXT-COL-COL
1547 
1548  pData[0] = cInputCalculateColor(&(*InputInstance.pAnalog).NxtCol[Device]);
1549  Result = OK;
1550  }
1551  break;
1552 
1553  case 1 :
1554  { // NXT-COL-AMB
1555 
1556  Memcpy((void*)pData,(const void*)&(*InputInstance.pAnalog).NxtCol[Device].ADRaw[BLANK],(size_t)2);
1557  Result = OK;
1558  }
1559  break;
1560 
1561  case 0 :
1562  { // NXT-COL-RED
1563 
1564  Memcpy((void*)pData,(const void*)&(*InputInstance.pAnalog).NxtCol[Device].ADRaw[RED],(size_t)2);
1565  Result = OK;
1566  }
1567  break;
1568 
1569  case 3 :
1570  { // NXT-COL-GRN
1571 
1572  Memcpy((void*)pData,(const void*)&(*InputInstance.pAnalog).NxtCol[Device].ADRaw[GREEN],(size_t)2);
1573  Result = OK;
1574  }
1575  break;
1576 
1577  case 4 :
1578  { // NXT-COL-BLU
1579 
1580  Memcpy((void*)pData,(const void*)&(*InputInstance.pAnalog).NxtCol[Device].ADRaw[BLUE],(size_t)2);
1581  Result = OK;
1582  }
1583  break;
1584 
1585  case 5 :
1586  { // NXT-COL-RAW
1587 
1588  Memcpy((void*)pData,(const void*)&(*InputInstance.pAnalog).NxtCol[Device].SensorRaw[0],(size_t)(COLORS * 2));
1589  Result = OK;
1590  }
1591  break;
1592  }
1593 
1594  return (Result);
1595 }
1596 
1597 #else
1598 
1599 DATAF cInputGetColor(DATA8 Device,DATA8 Index)
1600 {
1601  DATAF Result ;
1602 
1603  Result = DATAF_NAN;
1604 
1605  cInputCalibrateColor(&(*InputInstance.pAnalog).NxtCol[Device],(*InputInstance.pAnalog).NxtCol[Device].SensorRaw);
1606 
1607  switch (InputInstance.DeviceMode[Device])
1608  {
1609  case 2 :
1610  { // NXT-COL-COL
1611 
1612  Result = cInputCalculateColor(&(*InputInstance.pAnalog).NxtCol[Device]);
1613  }
1614  break;
1615 
1616  case 1 :
1617  { // NXT-COL-AMB
1618 
1619  Result = (*InputInstance.pAnalog).NxtCol[Device].ADRaw[BLANK];
1620  }
1621  break;
1622 
1623  case 0 :
1624  { // NXT-COL-RED
1625 
1626  Result = (*InputInstance.pAnalog).NxtCol[Device].ADRaw[RED];
1627  }
1628  break;
1629 
1630  case 3 :
1631  { // NXT-COL-GRN
1632 
1633  Result = (*InputInstance.pAnalog).NxtCol[Device].ADRaw[GREEN];
1634  }
1635  break;
1636 
1637  case 4 :
1638  { // NXT-COL-BLU
1639 
1640  Result = (*InputInstance.pAnalog).NxtCol[Device].ADRaw[BLUE];
1641  }
1642  break;
1643 
1644  case 5 :
1645  { // NXT-COL-RAW
1646 
1647  if (Index < COLORS)
1648  {
1649  Result = (DATAF)(*InputInstance.pAnalog).NxtCol[Device].SensorRaw[Index];
1650  }
1651  }
1652  break;
1653  }
1654 
1655  return (Result);
1656 }
1657 
1658 #endif
1659 
1660 #endif
1661 
1662 
1663 #ifndef DISABLE_DAISYCHAIN
1664 /*
1665 
1666 DEVICE INFO FROM FIRST SLAVE
1667 ============================
1668 
1669 cInputUpdate(UWORD Time)
1670 |
1671 cInputDcmUpdate(UWORD Time)
1672 |
1673 cInputComGetDeviceInfo(DATA8 Length,UBYTE *pInfo)
1674 |
1675 cComGetDeviceInfo(DATA8 Length,UBYTE *pInfo)
1676 |
1677 cDaisyGetDeviceInfo(DATA8 Length,UBYTE *pInfo)
1678 |
1679 
1680 -------------------- USB DAISYCHAIN --------------------
1681 
1682 ^
1683 (cDaisySetDeviceInfo(DATA8 Length,UBYTE *pInfo))
1684 ^
1685 cComSetDeviceInfo(DATA8 Length,UBYTE *pInfo)
1686 ^
1687 cInputComSetDeviceInfo(DATA8 Length,UBYTE *pInfo)
1688 ^
1689 cInputCheckUartInfo(UBYTE Port)
1690 ^
1691 cInputDcmUpdate(UWORD Time)
1692 
1693 
1694 
1695 
1696 CHANGE MODE ON FIRST SLAVE
1697 ==============================
1698 
1699 opINPUT_DEVICE(READY_SI,1,..)
1700 |
1701 cInputSetDeviceType(DATA8 Device,DATA8 Type, DATA8 Mode)
1702 |
1703 cInputComSetDeviceType(DATA8 Layer,DATA8 Port,DATA8 Type,DATA8 Mode)
1704 |
1705 cComSetDeviceType(DATA8 Layer,DATA8 Port,DATA8 Type,DATA8 Mode)
1706 |
1707 cDaisySetDeviceType(DATA8 Layer,DATA8 Port,DATA8 Type,DATA8 Mode)
1708 |
1709 
1710 -------------------- USB DAISYCHAIN --------------------
1711 
1712 |
1713 cInputSetChainedDeviceType(DATA8 Layer,DATA8 Port,DATA8 Type,DATA8 Mode)
1714 |
1715 cInputSetDeviceType(DATA8 Device,DATA8 Type, DATA8 Mode)
1716 
1717 
1718 
1719 
1720 
1721 READ SENSOR VALUE FROM FIRST SLAVE
1722 ==================================
1723 
1724 opINPUT_DEVICE(READY_SI,1,..)
1725 |
1726 cInputReadDeviceSi(DATA8 Device,DATA8 Index,DATA16 Time,DATA16 *pInit)
1727 |
1728 cInputReadDeviceRaw(DATA8 Device,DATA8 Index,DATA16 Time,DATA16 *pInit)
1729 |
1730 cInputGetData(DATA8 Layer,DATA8 Port,DATA16 Time,DATA16 *pInit,DATA8 Length,DATA8 *pType,DATA8 *pMode,DATA8 *pData)
1731 |
1732 cInputComGetDeviceData(DATA8 Layer,DATA8 Port,DATA8 Length,DATA8 *pType,DATA8 *pMode,DATA8 *pData)
1733 |
1734 cComGetDeviceData(DATA8 Layer,DATA8 Port,DATA8 Length,DATA8 *pType,DATA8 *pMode,DATA8 *pData)
1735 |
1736 cDaisyGetDownstreamData(DATA8 Layer,DATA8 Sensor,DATA8 Length,DATA8 *pType,DATA8 *pMode,DATA8 *pData)
1737 |
1738 
1739 -------------------- USB DAISYCHAIN --------------------
1740 
1741 |
1742 cDaisyPushUpStream(void)
1743 |
1744 cInputGetDeviceData(DATA8 Layer,DATA8 Port,DATA8 Length,DATA8 *pType,DATA8 *pMode,DATA8 *pData)
1745 |
1746 cInputGetData(DATA8 Layer,DATA8 Port,DATA16 Time,DATA16 *pInit,DATA8 Length,DATA8 *pType,DATA8 *pMode,DATA8 *pData)
1747 
1748 */
1749 
1750 
1751 
1752 RESULT cInputSetChainedDeviceType(DATA8 Layer,DATA8 Port,DATA8 Type,DATA8 Mode)
1753 {
1754  RESULT Result = FAIL;
1755  DATA8 Device;
1756 
1757  Result = cInputCompressDevice(&Device,Layer,Port);
1758  if (Result == OK)
1759  { // Device valid
1760 
1761  if ((InputInstance.DeviceType[Device] > 0) && (InputInstance.DeviceType[Device] <= MAX_VALID_TYPE))
1762  { // Device type valid
1763 
1764  if (Type != TYPE_UNKNOWN)
1765  {
1766  cInputSetDeviceType(Device,Type,Mode,__LINE__);
1767  }
1768 #ifdef DEBUG_TRACE_DAISYCHAIN
1769  printf("c_input cInputSetDeviceType: L=%-2d P=%-2d T=%-3d M=%d\r\n",Layer,Port,Type,Mode);
1770 #endif
1771  }
1772  }
1773  else
1774  {
1775 #ifdef DEBUG_TRACE_DAISYCHAIN
1776  printf("c_input cInputSetDeviceType: FAIL L=%-2d P=%-2d T=%-3d M=%d\r\n",Layer,Port,Type,Mode);
1777 #endif
1778  }
1779 
1780  return (Result);
1781 }
1782 
1783 
1784 RESULT cInputComSetDeviceInfo(DATA8 Length,UBYTE *pInfo)
1785 {
1786  RESULT Result = FAIL;
1787 #ifdef DEBUG_TRACE_DAISYCHAIN
1788  TYPES *pType;
1789 #endif
1790 
1791 #ifndef DISABLE_DAISYCHAIN_COM_CALL
1792 
1793  if (cDaisyReady() != BUSY)
1794  {
1795  Result = cComSetDeviceInfo(Length,pInfo);
1796  }
1797  else
1798  {
1799  Result = BUSY;
1800  }
1801 
1802 #else
1803  Result = OK;
1804 #endif
1805 
1806 #ifdef DEBUG_TRACE_DAISYCHAIN
1807  pType = (TYPES*)pInfo;
1808 
1809  if (Result == OK)
1810  {
1811  printf("c_com cComSetDeviceInfo: l=%-2d N=%s\r\n",Length,(*pType).Name);
1812  }
1813  else
1814  {
1815  if (Result == BUSY)
1816  {
1817  printf("c_com cComSetDeviceInfo: BUSY l=%-2d N=%s\r\n",Length,(*pType).Name);
1818  }
1819  else
1820  {
1821  printf("c_com cComSetDeviceInfo: FAIL l=%-2d N=%s\r\n",Length,(*pType).Name);
1822  }
1823  }
1824 #endif
1825 
1826  return (Result);
1827 }
1828 
1829 
1830 RESULT cInputComGetDeviceInfo(DATA8 Length,UBYTE *pInfo)
1831 {
1832  RESULT Result = FAIL;
1833 #ifdef DEBUG_TRACE_DAISYCHAIN
1834  TYPES *pType;
1835 #endif
1836 
1837 #ifndef DISABLE_DAISYCHAIN_COM_CALL
1838  Result = cComGetDeviceInfo(Length,pInfo);
1839 #else
1840  Result = FAIL;
1841 #endif
1842 
1843 #ifdef DEBUG_TRACE_DAISYCHAIN
1844  if (Result == OK)
1845  {
1846  pType = (TYPES*)pInfo;
1847  printf("c_com cComGetDeviceInfo: C=%-3d N=%s\r\n",(*pType).Connection,(*pType).Name);
1848  }
1849 #endif
1850 
1851  return (Result);
1852 }
1853 
1854 
1855 RESULT cInputComSetDeviceType(DATA8 Layer,DATA8 Port,DATA8 Type,DATA8 Mode)
1856 {
1857  RESULT Result = FAIL;
1858 
1859 #ifndef DISABLE_DAISYCHAIN_COM_CALL
1860  Result = cComSetDeviceType(Layer,Port,Type,Mode);
1861 #else
1862  Result = OK;
1863 #endif
1864 
1865 #ifdef DEBUG_TRACE_DAISYCHAIN
1866  if (Result == OK)
1867  {
1868  printf("c_com cComSetDeviceType: L=%-2d P=%-2d T=%-3d M=%d\r\n",Layer,Port,Type,Mode);
1869  }
1870  else
1871  {
1872  if (Result == BUSY)
1873  {
1874  printf("c_com cComSetDeviceType: BUSY L=%-2d P=%-2d T=%-3d M=%d\r\n",Layer,Port,Type,Mode);
1875  }
1876  else
1877  {
1878  printf("c_com cComSetDeviceType: FAIL L=%-2d P=%-2d T=%-3d M=%d\r\n",Layer,Port,Type,Mode);
1879  }
1880  }
1881 #endif
1882 
1883  return (Result);
1884 }
1885 
1886 
1887 RESULT cInputComGetDeviceData(DATA8 Layer,DATA8 Port,DATA8 Length,DATA8 *pType,DATA8 *pMode,DATA8 *pData)
1888 {
1889  RESULT Result = FAIL;
1890 
1891 #ifndef DISABLE_DAISYCHAIN_COM_CALL
1892  Result = cComGetDeviceData(Layer,Port,Length,pType,pMode,pData);
1893 
1894 #ifdef DEBUG_TRACE_DAISYCHAIN
1895  if (Result == OK)
1896  {
1897  printf("c_com cComGetDeviceData: L=%-2d P=%-2d T=%-3d M=%d 0x%02X\r\n",Layer,Port,*pType,*pMode,*pData);
1898  }
1899  else
1900  {
1901  if (Result == BUSY)
1902  {
1903  printf("c_com cComGetDeviceData: BUSY L=%-2d P=%-2d T=%-3d M=%d\r\n",Layer,Port,*pType,*pMode);
1904  }
1905  else
1906  {
1907  printf("c_com cComGetDeviceData: FAIL L=%-2d P=%-2d T=%-3d M=%d\r\n",Layer,Port,*pType,*pMode);
1908  }
1909  }
1910 #endif
1911 
1912 #else
1913  Result = FAIL;
1914 #endif
1915 
1916  return (Result);
1917 }
1918 
1919 
1920 RESULT cInputComGetDeviceType(DATA8 Layer,DATA8 Port,DATA8 Length,DATA8 *pType,DATA8 *pMode,DATA8 *pData)
1921 {
1922  RESULT Result = FAIL;
1923 
1924 #ifndef DISABLE_DAISYCHAIN_COM_CALL
1925  Result = cComGetDeviceData(Layer,Port,Length,pType,pMode,pData);
1926 
1927 #else
1928  Result = FAIL;
1929 #endif
1930 
1931  return (Result);
1932 }
1933 
1934 
1935 RESULT cInputGetDeviceData(DATA8 Layer,DATA8 Port,DATA8 Length,DATA8 *pType,DATA8 *pMode,DATA8 *pData)
1936 {
1937  RESULT Result;
1938 
1939  Result = cInputGetData(Layer,Port,0,NULL,Length,pType,pMode,pData);
1940 
1941 #ifdef DEBUG_C_INPUT_DAISYCHAIN
1942  if (Result == OK)
1943  {
1944  printf("c_com cInputGetDeviceData: L=%-2d P=%-2d T=%-3d M=%d\r\n",Layer,Port,*pType,*pMode);
1945  }
1946  else
1947  {
1948  if (Result == BUSY)
1949  {
1950  printf("c_com cInputGetDeviceData: BUSY L=%-2d P=%-2d T=%-3d M=%d\r\n",Layer,Port,*pType,*pMode);
1951  }
1952  else
1953  {
1954  printf("c_com cInputGetDeviceData: FAIL L=%-2d P=%-2d T=%-3d M=%d\r\n",Layer,Port,*pType,*pMode);
1955  }
1956  }
1957 #endif
1958 
1959  return (Result);
1960 }
1961 
1962 
1963 RESULT cInputGetData(DATA8 Layer,DATA8 Port,DATA16 Time,DATA16 *pInit,DATA8 Length,DATA8 *pType,DATA8 *pMode,DATA8 *pData)
1964 {
1965 #ifdef DEBUG_C_INPUT_FAST_DATALOG
1966 #ifndef DISABLE_FAST_DATALOG_BUFFER
1967  static UWORD Cnt;
1968  static UWORD Old;
1969  UWORD New;
1970  DATA16 TmpTime;
1971 #endif
1972 #endif
1973  RESULT Result = FAIL;
1974  DATA8 Device;
1975 #ifndef DISABLE_FAST_DATALOG_BUFFER
1976  DATA16 InPointer;
1977  DATA16 Pointer;
1978 #endif
1979 
1980  if (cInputCompressDevice(&Device,Layer,Port) == OK)
1981  { // Device valid
1982 
1983 #ifdef DEBUG_C_INPUT_DAISYCHAIN
1984  printf("c_input cInputGetDeviceData D=%-3d L=%d P=%-2d l=%-2d\r\n",Device,Layer,Port,Length);
1985 #endif
1986 
1987  if (Length >= MAX_DEVICE_DATALENGTH)
1988  { // Length OK
1989 
1990  memset(pData,0,(size_t)Length);
1991 
1992  *pType = InputInstance.DeviceType[Device];
1993  *pMode = InputInstance.DeviceMode[Device];
1994 
1995  if ((InputInstance.DeviceData[Device].Connection != CONN_NONE) && (InputInstance.DeviceData[Device].Connection != CONN_ERROR))
1996  { // Device connected
1997 
1998  if (Layer == 0)
1999  { // Device local
2000 
2001  if (Port < INPUT_DEVICES)
2002  { // Device is input device
2003 
2004  if ((Time < 0) || (Time > DEVICE_LOGBUF_SIZE))
2005  {
2006  Time = 0;
2007  }
2008 
2009  if (InputInstance.DeviceData[Device].Connection == CONN_INPUT_UART)
2010  { // Device is an UART device
2011 
2012 #ifndef DISABLE_FAST_DATALOG_BUFFER
2013 
2014  Pointer = (*InputInstance.pUart).Actual[Port];
2015  InPointer = (*InputInstance.pUart).LogIn[Port];
2016  if (pInit != NULL)
2017  {
2018 #ifdef DEBUG_C_INPUT_DATALOG
2019  printf("c_input cInputGetDeviceData Act=%-4d In=%-4d Out=%-4d Time=%-4d ",(*InputInstance.pUart).Actual[Port],(*InputInstance.pUart).LogIn[Port],*pInit,Time);
2020 #endif
2021 
2022  if ((*pInit < 0) || (*pInit > DEVICE_LOGBUF_SIZE))
2023  {
2024  *pInit = Pointer;
2025  (*InputInstance.pUart).Repeat[Port][*pInit] = 0;
2026 #ifdef DEBUG_C_INPUT_FAST_DATALOG
2027  if (Port == 0)
2028  {
2029  Cnt = 0;
2030  }
2031 #endif
2032  }
2033 
2034  if (Time != 0)
2035  {
2036 #ifdef DEBUG_C_INPUT_FAST_DATALOG
2037  TmpTime = Time;
2038 
2039  if ((Port == 0) && ((*InputInstance.pUart).Repeat[Port][*pInit] > 1))
2040  {
2041  printf("C=%-5d A=%-4d I=%-4d P=%-4d O=%-3d N=%-3d R=%-3d \r\n",Cnt,(*InputInstance.pUart).Actual[Port],(*InputInstance.pUart).LogIn[Port],Pointer,Old,New,(*InputInstance.pUart).Repeat[Port][*pInit]);
2042  }
2043 #endif
2044 
2045  Pointer = *pInit;
2046  while ((Time) && ((*InputInstance.pUart).Repeat[Port][Pointer] > 1))
2047  {
2048  ((*InputInstance.pUart).Repeat[Port][Pointer])--;
2049  Time--;
2050  }
2051  while ((Time) && (Pointer != InPointer))
2052  {
2053 
2054  if (++Pointer >= DEVICE_LOGBUF_SIZE)
2055  {
2056  Pointer = 0;
2057  }
2058 
2059  if (Pointer != InPointer)
2060  {
2061  (*pInit) = Pointer;
2062  }
2063  Time--;
2064  }
2065  Pointer = (*pInit);
2066 #ifdef DEBUG_C_INPUT_FAST_DATALOG
2067  if ((Port == 0) && (TmpTime >= 1))
2068  {
2069  New = (*InputInstance.pUart).Raw[Device][Pointer][0];
2070  if ((New < (Old - TmpTime)) || (New > (Old + TmpTime)))
2071  {
2072  if ((New != 0) && ((*InputInstance.pUart).Repeat[Port][*pInit]))
2073  {
2074  printf("C=%-5d A=%-4d I=%-4d P=%-4d O=%-3d N=%-3d R=%-3d ",Cnt,(*InputInstance.pUart).Actual[Port],InPointer,Pointer,Old,New,(*InputInstance.pUart).Repeat[Port][*pInit]);
2075  }
2076  }
2077  Old = New;
2078  }
2079  if (Port == 0)
2080  {
2081  Cnt++;
2082  }
2083 #endif
2084  }
2085 #ifdef DEBUG_C_INPUT_DATALOG
2086  printf("Pointer=%-4d\r\n",Pointer);
2087 #endif
2088 
2089  }
2090  Memcpy((void*)pData,(const void*)&(*InputInstance.pUart).Raw[Device][Pointer],(size_t)UART_DATA_LENGTH);
2091 
2092 #else
2093  Memcpy((void*)pData,(const void*)&(*InputInstance.pUart).Raw[Device],UART_DATA_LENGTH);
2094 #endif
2095  Result = OK;
2096  }
2097  else
2098  { // Device is not an UART device
2099 
2100  if (InputInstance.DeviceData[Device].Connection == CONN_NXT_IIC)
2101  { // Device is an IIC device
2102 
2103 #ifndef DISABLE_FAST_DATALOG_BUFFER
2104 
2105  Pointer = (*InputInstance.pIic).Actual[Port];
2106  InPointer = (*InputInstance.pIic).LogIn[Port];
2107  if (pInit != NULL)
2108  {
2109 #ifdef DEBUG_C_INPUT_DATALOG
2110  printf("c_input cInputGetDeviceData Act=%-4d In=%-4d Out=%-4d Time=%-4d ",(*InputInstance.pIic).Actual[Port],(*InputInstance.pIic).LogIn[Port],*pInit,Time);
2111 #endif
2112 
2113  if ((*pInit < 0) || (*pInit > DEVICE_LOGBUF_SIZE))
2114  {
2115  *pInit = Pointer;
2116  (*InputInstance.pIic).Repeat[Port][*pInit] = 0;
2117 #ifdef DEBUG_C_INPUT_FAST_DATALOG
2118  if (Port == 0)
2119  {
2120  Cnt = 0;
2121  }
2122 #endif
2123  }
2124  if (Time != 0)
2125  {
2126  Pointer = *pInit;
2127  while ((Time) && ((*InputInstance.pIic).Repeat[Port][Pointer] > 1))
2128  {
2129  ((*InputInstance.pIic).Repeat[Port][Pointer])--;
2130  Time--;
2131  }
2132  while ((Time) && (Pointer != InPointer))
2133  {
2134 
2135  if (++Pointer >= DEVICE_LOGBUF_SIZE)
2136  {
2137  Pointer = 0;
2138  }
2139 
2140  if (Pointer != InPointer)
2141  {
2142  (*pInit) = Pointer;
2143  }
2144  Time--;
2145  }
2146  Pointer = (*pInit);
2147 #ifdef DEBUG_C_INPUT_FAST_DATALOG
2148  if (Port == 0)
2149  {
2150  Cnt++;
2151  New = (UWORD)(*InputInstance.pIic).Raw[Device][Pointer][0];
2152  New += (UWORD)(*InputInstance.pIic).Raw[Device][Pointer][1] << 8;
2153  printf("C=%-5d A=%-4d I=%-4d P=%-4d O=%-5d N=%-5d R=%-3d\r\n ",Cnt,(*InputInstance.pIic).Actual[Port],InPointer,Pointer,Old,New,(*InputInstance.pIic).Repeat[Port][*pInit]);
2154  Old = New;;
2155  }
2156 #endif
2157  }
2158  }
2159  Memcpy((void*)pData,(const void*)&(*InputInstance.pIic).Raw[Device][Pointer],(size_t)IIC_DATA_LENGTH);
2160 
2161 #else
2162  Memcpy((void*)pData,(const void*)&(*InputInstance.pIic).Raw[Device],IIC_DATA_LENGTH);
2163 #endif
2164  Result = OK;
2165  }
2166  else
2167  { // Device is not an IIC device
2168 
2169  if (InputInstance.DeviceData[Device].Connection == CONN_INPUT_DUMB)
2170  { // Device is a new dumb input device
2171 
2172 #ifndef DISABLE_FAST_DATALOG_BUFFER
2173 
2174  if (pInit != NULL)
2175  {
2176  Pointer = (*InputInstance.pAnalog).Actual[Port];
2177  InPointer = (*InputInstance.pAnalog).LogIn[Port];
2178 #ifdef DEBUG_C_INPUT_DATALOG
2179  printf("c_input cInputGetDeviceData Act=%-4d In=%-4d Out=%-4d Time=%-4d ",(*InputInstance.pAnalog).Actual[Port],(*InputInstance.pAnalog).LogIn[Port],*pInit,Time);
2180 #endif
2181 
2182  if ((*pInit < 0) || (*pInit > DEVICE_LOGBUF_SIZE))
2183  {
2184  *pInit = Pointer;
2185  }
2186  if (Time != 0)
2187  {
2188  Pointer = *pInit;
2189  while ((Time) && (Pointer != InPointer))
2190  {
2191 
2192  if (++Pointer >= DEVICE_LOGBUF_SIZE)
2193  {
2194  Pointer = 0;
2195  }
2196 
2197  if (Pointer != InPointer)
2198  {
2199  (*pInit) = Pointer;
2200  }
2201  Time--;
2202  }
2203  Pointer = (*pInit);
2204  }
2205 #ifdef DEBUG_C_INPUT_DATALOG
2206  printf("Pointer=%-4d\r\n",Pointer);
2207 #endif
2208  if ((InputInstance.DeviceType[Device] == 16) && (InputInstance.DeviceMode[Device] == 1))
2209  {
2210  Memcpy((void*)pData,(const void*)&InputInstance.DeviceData[Device].Changes,(size_t)4);
2211  }
2212  else
2213  {
2214  Memcpy((void*)pData,(const void*)&(*InputInstance.pAnalog).Pin6[Device][Pointer],(size_t)2);
2215  }
2216  }
2217  else
2218  {
2219  if ((InputInstance.DeviceType[Device] == 16) && (InputInstance.DeviceMode[Device] == 1))
2220  {
2221  Memcpy((void*)pData,(const void*)&InputInstance.DeviceData[Device].Changes,(size_t)4);
2222  }
2223  else
2224  {
2225  Memcpy((void*)pData,(const void*)&(*InputInstance.pAnalog).InPin6[Device],(size_t)2);
2226  }
2227  }
2228 #else
2229  if ((InputInstance.DeviceType[Device] == 16) && (InputInstance.DeviceMode[Device] == 1))
2230  {
2231  Memcpy((void*)pData,(const void*)&InputInstance.DeviceData[Device].Changes,(size_t)4);
2232  }
2233  else
2234  {
2235  Memcpy((void*)pData,(const void*)&(*InputInstance.pAnalog).InPin6[Device],(size_t)2);
2236  }
2237 #endif
2238  Result = OK;
2239  }
2240  else
2241  {
2242 #ifndef DISABLE_OLD_COLOR
2243 
2244  if (InputInstance.DeviceData[Device].Connection == CONN_NXT_COLOR)
2245  { // Device is a nxt color sensor
2246 
2247  Result = cInputGetColor(Device,pData);
2248  }
2249  else
2250  { // Device is an old dumb input device
2251 
2252 #ifndef DISABLE_FAST_DATALOG_BUFFER
2253 
2254  if (pInit != NULL)
2255  {
2256  Pointer = (*InputInstance.pAnalog).Actual[Port];
2257  InPointer = (*InputInstance.pAnalog).LogIn[Port];
2258 #ifdef DEBUG_C_INPUT_DATALOG
2259  printf("c_input cInputGetDeviceData Act=%-4d In=%-4d Out=%-4d Time=%-4d ",(*InputInstance.pAnalog).Actual[Port],(*InputInstance.pAnalog).LogIn[Port],*pInit,Time);
2260 #endif
2261 
2262  if ((*pInit < 0) || (*pInit > DEVICE_LOGBUF_SIZE))
2263  {
2264  *pInit = Pointer;
2265  }
2266  if (Time != 0)
2267  {
2268  Pointer = *pInit;
2269  while ((Time) && (Pointer != InPointer))
2270  {
2271 
2272  if (++Pointer >= DEVICE_LOGBUF_SIZE)
2273  {
2274  Pointer = 0;
2275  }
2276 
2277  if (Pointer != InPointer)
2278  {
2279  (*pInit) = Pointer;
2280  }
2281  Time--;
2282  }
2283  Pointer = (*pInit);
2284  }
2285 #ifdef DEBUG_C_INPUT_DATALOG
2286  printf("Pointer=%-4d\r\n",Pointer);
2287 #endif
2288 
2289  if ((InputInstance.DeviceType[Device] == 1) && (InputInstance.DeviceMode[Device] == 1))
2290  {
2291  Memcpy((void*)pData,(const void*)&InputInstance.DeviceData[Device].Bumps,(size_t)4);
2292  }
2293  else
2294  {
2295  Memcpy((void*)pData,(const void*)&(*InputInstance.pAnalog).Pin1[Device][Pointer],(size_t)2);
2296  }
2297  }
2298  else
2299  {
2300  if ((InputInstance.DeviceType[Device] == 1) && (InputInstance.DeviceMode[Device] == 1))
2301  {
2302  Memcpy((void*)pData,(const void*)&InputInstance.DeviceData[Device].Bumps,(size_t)4);
2303  }
2304  else
2305  {
2306  Memcpy((void*)pData,(const void*)&(*InputInstance.pAnalog).InPin1[Device],(size_t)2);
2307  }
2308  }
2309 #else
2310  if ((InputInstance.DeviceType[Device] == 1) && (InputInstance.DeviceMode[Device] == 1))
2311  {
2312  Memcpy((void*)pData,(const void*)&InputInstance.DeviceData[Device].Bumps,(size_t)4);
2313  }
2314  else
2315  {
2316  Memcpy((void*)pData,(const void*)&(*InputInstance.pAnalog).InPin1[Device],(size_t)2);
2317  }
2318 #endif
2319  Result = OK;
2320  }
2321 #else
2322  // Device is an old dumb input device
2323 
2324 #ifndef DISABLE_FAST_DATALOG_BUFFER
2325  if (pInit != NULL)
2326  {
2327  Pointer = (*InputInstance.pAnalog).Actual[Port];
2328  InPointer = (*InputInstance.pAnalog).LogIn[Port];
2329 #ifdef DEBUG_C_INPUT_DATALOG
2330  printf("c_input cInputGetDeviceData Act=%-4d In=%-4d Out=%-4d Time=%-4d ",(*InputInstance.pAnalog).Actual[Port],*pInit,Time);
2331 #endif
2332 
2333  if ((*pInit < 0) || (*pInit > DEVICE_LOGBUF_SIZE))
2334  {
2335  *pInit = Pointer;
2336  }
2337  if (Time != 0)
2338  {
2339  Pointer = *pInit;
2340  while ((Time) && (Pointer != InPointer))
2341  {
2342  (*pInit) = Pointer;
2343 
2344  if (++Pointer >= DEVICE_LOGBUF_SIZE)
2345  {
2346  Pointer = 0;
2347  }
2348  Time--;
2349  }
2350  Pointer = (*pInit);
2351  }
2352 #ifdef DEBUG_C_INPUT_DATALOG
2353  printf("Pointer=%-4d\r\n",Pointer);
2354 #endif
2355  Memcpy((void*)pData,(const void*)&(*InputInstance.pAnalog).Pin1[Device][Pointer],(size_t)2);
2356  }
2357  else
2358  {
2359  Memcpy((void*)pData,(const void*)&(*InputInstance.pAnalog).InPin1[Device],(size_t)2);
2360  }
2361 #else
2362  Memcpy((void*)pData,(const void*)&(*InputInstance.pAnalog).InPin1[Device],(size_t)2);
2363 #endif
2364 
2365  Result = OK;
2366 #endif
2367  }
2368  }
2369  }
2370  }
2371  else
2372  { // Device is output device
2373 
2374  if (InputInstance.DeviceMode[Device] == 2)
2375  {
2376  Memcpy((void*)pData,(const void*)&OutputInstance.pMotor[Device - INPUT_DEVICES].Speed,(size_t)1);
2377  Result = OK;
2378  }
2379  else
2380  {
2381  Memcpy((void*)pData,(const void*)&OutputInstance.pMotor[Device - INPUT_DEVICES].TachoSensor,(size_t)4);
2382  Result = OK;
2383  }
2384 
2385  InputInstance.DeviceData[Device].DevStatus = OK;
2386  }
2387  if (Result == OK)
2388  {
2389  Result = InputInstance.DeviceData[Device].DevStatus;
2390  }
2391 
2392  }
2393  else
2394  { // Device is daisy chained
2395 
2396  Result = cInputComGetDeviceData(Layer,Port,MAX_DEVICE_DATALENGTH,pType,pMode,pData);
2397 
2398  }
2399  }
2400  }
2401  }
2402 
2403  return (Result);
2404 }
2405 
2406 
2408 {
2409  DATAF Result = (DATAF)0;
2410  DATA8 Layer;
2411  DATA8 Port;
2412  DATA8 Output;
2413  DATA8 Type;
2414  DATA8 Mode;
2415  DATA8 DataSets;
2416  UBYTE *pData;
2417 
2418 
2419  Result = DATAF_NAN;
2420 
2421 #ifdef DEBUG_C_INPUT_DAISYCHAIN
2422  printf("c_input cInputReadDeviceRaw: D=%-3d I=%d B=%d\r\n",Device,Index,InputInstance.DeviceData[Device].DevStatus);
2423 #endif
2424 
2425  if (cInputExpandDevice(Device,&Layer,&Port,&Output) == OK)
2426  { // Device valid
2427 
2428  pData = InputInstance.Data;
2429  InputInstance.DeviceData[Device].Raw[Index] = DATAF_NAN;
2430 
2431  if (cInputGetData(Layer,Port,Time,pInit,MAX_DEVICE_DATALENGTH,&Type,&Mode,(DATA8*)pData) == OK)
2432  {
2433 
2434  DataSets = InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].DataSets;
2435  if (Index < DataSets)
2436  {
2437  switch (InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].Format & 0x0F)
2438  {
2439  case DATA_8 :
2440  {
2441  if (((DATA8*)pData)[Index] != DATA8_NAN)
2442  {
2443  InputInstance.DeviceData[Device].Raw[Index] = (DATAF)((DATA8*)pData)[Index];
2444  }
2445  }
2446  break;
2447 
2448  case DATA_16 :
2449  {
2450  if (((DATA16*)pData)[Index] != DATA16_NAN)
2451  {
2452  InputInstance.DeviceData[Device].Raw[Index] = (DATAF)((DATA16*)pData)[Index];
2453 
2455  if ((Type == 99) && (Mode == 7) && (Index == 4))
2456  {
2457  InputInstance.DeviceData[Device].Raw[Index] *= (DATAF)1000.0;
2458  }
2459  }
2460  }
2461  break;
2462 
2463  case DATA_32 :
2464  {
2465  if (((DATA32*)pData)[Index] != DATA32_NAN)
2466  {
2467  InputInstance.DeviceData[Device].Raw[Index] = (DATAF)((DATA32*)pData)[Index];
2468  }
2469  }
2470  break;
2471 
2472  case DATA_F :
2473  {
2474  InputInstance.DeviceData[Device].Raw[Index] = (DATAF)((DATAF*)pData)[Index];
2475  }
2476  break;
2477 
2478  }
2479  }
2480 
2481  Result = InputInstance.DeviceData[Device].Raw[Index];
2482  }
2483  }
2484 
2485  return (Result);
2486 }
2487 
2488 
2489 #else
2490 
2491 
2492 DATAF cInputReadDeviceRaw(DATA8 Device,DATA8 Index,DATA16 Time,DATA16 *pInit)
2493 {
2494  DATAF Result;
2495  DATA8 DataSets;
2496  void *pResult;
2497 
2498  Result = DATAF_NAN;
2499 
2500 #ifdef DEBUG_C_INPUT_DAISYCHAIN
2501  printf("c_input cInputReadDeviceRaw: D=%-3d B=%d\r\n",Device,InputInstance.DeviceData[Device].DevStatus);
2502 #endif
2503  if ((Device >= 0) && (Device < DEVICES) && (Index >= 0) && (Index < MAX_DEVICE_DATASETS))
2504  {
2505  // Parameters are valid
2506 
2507  if (InputInstance.DeviceData[Device].DevStatus == OK)
2508  {
2509 
2510  if (Device < INPUTS)
2511  {
2512  // Device is a local sensor
2513 
2514  if ((InputInstance.DeviceData[Device].Connection != CONN_NONE) && (InputInstance.DeviceData[Device].Connection != CONN_ERROR))
2515  {
2516  // Device is connected right
2517 
2518  if (InputInstance.DeviceData[Device].Connection == CONN_INPUT_UART)
2519  {
2520  // Device is a UART sensor
2521 
2522  pResult = (void*)&(*InputInstance.pUart).Raw[Device];
2523  DataSets = InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].DataSets;
2524 
2525  if (Index < DataSets)
2526  {
2527  switch (InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].Format & 0x0F)
2528  {
2529  case DATA_8 :
2530  {
2531  InputInstance.DeviceData[Device].Raw[Index] = (DATAF)*(((DATA8*)pResult) + Index);
2532  }
2533  break;
2534 
2535  case DATA_16 :
2536  {
2537  InputInstance.DeviceData[Device].Raw[Index] = (DATAF)*(((DATA16*)pResult) + Index);
2538  }
2539  break;
2540 
2541  case DATA_32 :
2542  {
2543  InputInstance.DeviceData[Device].Raw[Index] = (DATAF)*(((DATA32*)pResult) + Index);
2544  }
2545  break;
2546 
2547  case DATA_F :
2548  {
2549  InputInstance.DeviceData[Device].Raw[Index] = (DATAF)*(((DATAF*)pResult) + Index);
2550  }
2551  break;
2552 
2553  default :
2554  {
2555  InputInstance.DeviceData[Device].Raw[Index] = DATAF_NAN;
2556  }
2557  break;
2558 
2559  }
2560  }
2561  else
2562  {
2563  InputInstance.DeviceData[Device].Raw[Index] = DATAF_NAN;
2564  }
2565  }
2566  else
2567  {
2568  // Device is not a UART sensor
2569 
2570  if (InputInstance.DeviceData[Device].Connection == CONN_INPUT_DUMB)
2571  {
2572  // Device is new dumb
2573 
2574  InputInstance.DeviceData[Device].Raw[Index] = (DATAF)(*InputInstance.pAnalog).InPin6[Device];
2575  }
2576  else
2577  {
2578 #ifndef DISABLE_OLD_COLOR
2579 
2580  if (InputInstance.DeviceData[Device].Connection == CONN_NXT_COLOR)
2581  {
2582  // Device is nxt color
2583 
2584  InputInstance.DeviceData[Device].Raw[Index] = (DATAF)cInputGetColor(Device,Index);
2585  }
2586  else
2587  {
2588  // Device is old dumb
2589 
2590  InputInstance.DeviceData[Device].Raw[Index] = (DATAF)(*InputInstance.pAnalog).InPin1[Device];
2591  }
2592 #else
2593  // Device is old dumb
2594 
2595  InputInstance.DeviceData[Device].Raw[Index] = (DATAF)(*InputInstance.pAnalog).InPin1[Device];
2596 
2597 #endif
2598  }
2599  }
2600  }
2601  else
2602  {
2603  InputInstance.DeviceData[Device].Raw[Index] = DATAF_NAN;
2604  }
2605  }
2606  if ((Device >= INPUT_DEVICES) && (Device < (INPUT_DEVICES + OUTPUTS)))
2607  {
2608  // Device is connected on output port
2609 
2610  if ((InputInstance.DeviceData[Device].Connection == CONN_NONE) || (InputInstance.DeviceData[Device].Connection == CONN_ERROR))
2611  {
2612  InputInstance.DeviceData[Device].Raw[Index] = DATAF_NAN;
2613  }
2614  else
2615  {
2616  if (InputInstance.DeviceMode[Device] == 2)
2617  {
2618  InputInstance.DeviceData[Device].Raw[Index] = (DATAF)OutputInstance.pMotor[Device - INPUT_DEVICES].Speed;
2619  }
2620  else
2621  {
2622  InputInstance.DeviceData[Device].Raw[Index] = (DATAF)OutputInstance.pMotor[Device - INPUT_DEVICES].TachoSensor;
2623  }
2624  }
2625 
2626  }
2627  Result = InputInstance.DeviceData[Device].Raw[Index];
2628  }
2629  }
2630 
2631  return (Result);
2632 }
2633 #endif
2634 
2635 
2636 void cInputWriteDeviceRaw(DATA8 Device,DATA8 Connection,DATA8 Type,DATAF DataF)
2637 {
2638  UBYTE Byte;
2639  UWORD Word;
2640 
2641  if (Device < INPUTS)
2642  {
2643  if (Type == TYPE_KEEP)
2644  {
2645  Type = InputInstance.DeviceType[Device];
2646  }
2647  if (InputInstance.DeviceType[Device] != Type)
2648  {
2649  InputInstance.DeviceData[Device].Connection = Connection;
2650  cInputSetDeviceType(Device,Type,0,__LINE__);
2651  }
2652  if (InputInstance.DeviceData[Device].Connection == CONN_INPUT_UART)
2653  {
2654  }
2655  else
2656  {
2657  if (InputInstance.DeviceData[Device].Connection == CONN_NXT_IIC)
2658  {
2659  Byte = (UBYTE)DataF;
2660  Word = (UWORD)DataF;
2661 
2662 #ifndef DISABLE_FAST_DATALOG_BUFFER
2663  (*InputInstance.pAnalog).Actual[Device] = 0;
2664  if ((InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].Format & 0x0F) == 0)
2665  {
2666  (*InputInstance.pIic).Raw[Device][(*InputInstance.pAnalog).Actual[Device]][0] = Byte;
2667  (*InputInstance.pIic).Raw[Device][(*InputInstance.pAnalog).Actual[Device]][1] = 0;
2668  }
2669  else
2670  {
2671  (*InputInstance.pIic).Raw[Device][(*InputInstance.pAnalog).Actual[Device]][0] = (UBYTE)Word;
2672  (*InputInstance.pIic).Raw[Device][(*InputInstance.pAnalog).Actual[Device]][1] = (UBYTE)(Word >> 8);
2673  }
2674  InputInstance.DeviceData[Device].DevStatus = OK;
2675 #else
2676  if ((InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].Format & 0x0F) == 0)
2677  {
2678  (*InputInstance.pIic).Raw[Device][0] = Byte;
2679  (*InputInstance.pIic).Raw[Device][1] = 0;
2680  }
2681  else
2682  {
2683  (*InputInstance.pIic).Raw[Device][0] = (UBYTE)Word;
2684  (*InputInstance.pIic).Raw[Device][1] = (UBYTE)(Word >> 8);
2685  }
2686  InputInstance.DeviceData[Device].DevStatus = OK;
2687 #endif
2688  }
2689  else
2690  {
2691 #ifndef DISABLE_FAST_DATALOG_BUFFER
2692  (*InputInstance.pAnalog).Actual[Device] = 0;
2693  (*InputInstance.pAnalog).LogIn[Device] = 0;
2694 
2695  if (InputInstance.DeviceData[Device].Connection == CONN_INPUT_DUMB)
2696  {
2697  (*InputInstance.pAnalog).Pin6[Device][(*InputInstance.pAnalog).Actual[Device]] = (DATA16)DataF;
2698 
2699  }
2700  else
2701  {
2702  (*InputInstance.pAnalog).Pin1[Device][(*InputInstance.pAnalog).Actual[Device]] = (DATA16)DataF;
2703  }
2704  InputInstance.DeviceData[Device].DevStatus = OK;
2705 #endif
2706  if (InputInstance.DeviceData[Device].Connection == CONN_INPUT_DUMB)
2707  {
2708  (*InputInstance.pAnalog).InPin6[Device] = (DATA16)DataF;
2709  }
2710  else
2711  {
2712  (*InputInstance.pAnalog).InPin1[Device] = (DATA16)DataF;
2713  }
2714  InputInstance.DeviceData[Device].DevStatus = OK;
2715  }
2716  }
2717  }
2718  if ((Device >= INPUT_DEVICES) && (Device < (INPUT_DEVICES + OUTPUTS)))
2719  {
2720  if (Type == TYPE_KEEP)
2721  {
2722  Type = InputInstance.DeviceType[Device];
2723  }
2724  if (InputInstance.DeviceType[Device] != Type)
2725  {
2726  InputInstance.DeviceData[Device].Connection = CONN_UNKNOWN;
2727  cInputSetDeviceType(Device,Type,0,__LINE__);
2728  }
2729  if (InputInstance.DeviceMode[Device] == 2)
2730  {
2731  OutputInstance.pMotor[Device - INPUT_DEVICES].Speed = (DATA32)DataF;
2732  }
2733  else
2734  {
2735  OutputInstance.pMotor[Device - INPUT_DEVICES].TachoSensor = (DATA32)DataF;
2736  }
2737  InputInstance.DeviceData[Device].DevStatus = OK;
2738  }
2739 }
2740 
2741 
2743 {
2744  DATA8 Result = DATA8_NAN;
2745  UWORD TypeIndex;
2746  DATAF Raw;
2747  DATA8 Type;
2748  DATA8 Mode;
2749  DATAF Min;
2750  DATAF Max;
2751  DATAF Pct;
2752 
2753  Raw = cInputReadDeviceRaw(Device,Index,Time,pInit);
2754 
2755  if (!(isnan(Raw)))
2756  {
2757  TypeIndex = InputInstance.DeviceData[Device].TypeIndex;
2758 
2759  Type = InputInstance.TypeData[TypeIndex].Type;
2760  Mode = InputInstance.TypeData[TypeIndex].Mode;
2761  Min = InputInstance.TypeData[TypeIndex].RawMin;
2762  Max = InputInstance.TypeData[TypeIndex].RawMax;
2763 
2764  if ((Type > 0) && (Type < (MAX_DEVICE_TYPE + 1)) && (Mode >= 0) && (Mode < MAX_DEVICE_MODES))
2765  {
2766  if (InputInstance.Calib[Type][Mode].InUse)
2767  {
2768  Min = InputInstance.Calib[Type][Mode].Min;
2769  Max = InputInstance.Calib[Type][Mode].Max;
2770  }
2771  }
2772 
2773  Pct = (((Raw - Min) * (InputInstance.TypeData[TypeIndex].PctMax - InputInstance.TypeData[TypeIndex].PctMin)) / (Max - Min) + InputInstance.TypeData[TypeIndex].PctMin);
2774 
2775  if (Pct > InputInstance.TypeData[TypeIndex].PctMax)
2776  {
2777  Pct = InputInstance.TypeData[TypeIndex].PctMax;
2778  }
2779  if (Pct < InputInstance.TypeData[TypeIndex].PctMin)
2780  {
2781  Pct = InputInstance.TypeData[TypeIndex].PctMin;
2782  }
2783  Result = (DATA8)Pct;
2784  }
2785 
2786  return (Result);
2787 }
2788 
2789 
2791 {
2792  UWORD TypeIndex;
2793  DATAF Raw;
2794  DATA8 Type;
2795  DATA8 Mode;
2796  DATA8 Connection;
2797  DATAF Min;
2798  DATAF Max;
2799 
2800  Raw = cInputReadDeviceRaw(Device,Index,Time,pInit);
2801 
2802  if (!(isnan(Raw)))
2803  {
2804  TypeIndex = InputInstance.DeviceData[Device].TypeIndex;
2805 
2806  Type = InputInstance.TypeData[TypeIndex].Type;
2807  Mode = InputInstance.TypeData[TypeIndex].Mode;
2808  Min = InputInstance.TypeData[TypeIndex].RawMin;
2809  Max = InputInstance.TypeData[TypeIndex].RawMax;
2810 
2811  if ((Type > 0) && (Type < (MAX_DEVICE_TYPE + 1)) && (Mode >= 0) && (Mode < MAX_DEVICE_MODES))
2812  {
2813  if (InputInstance.Calib[Type][Mode].InUse)
2814  {
2815  Min = InputInstance.Calib[Type][Mode].Min;
2816  Max = InputInstance.Calib[Type][Mode].Max;
2817  }
2818  }
2819 
2820  Raw = (((Raw - Min) * (InputInstance.TypeData[TypeIndex].SiMax - InputInstance.TypeData[TypeIndex].SiMin)) / (Max - Min) + InputInstance.TypeData[TypeIndex].SiMin);
2821 
2822  // Limit values on dumb connections if "pct" or "_"
2823  Connection = InputInstance.TypeData[TypeIndex].Connection;
2824  if ((Connection == CONN_NXT_DUMB) || (Connection == CONN_INPUT_DUMB) || (Connection == CONN_OUTPUT_DUMB) || (Connection == CONN_OUTPUT_TACHO))
2825  {
2826  if ((InputInstance.TypeData[TypeIndex].Symbol[0] == 'p') || (InputInstance.TypeData[TypeIndex].Symbol[0] == ' ') || (InputInstance.TypeData[TypeIndex].Symbol[0] == 0))
2827  {
2828  if (Raw > InputInstance.TypeData[TypeIndex].SiMax)
2829  {
2830  Raw = InputInstance.TypeData[TypeIndex].SiMax;
2831  }
2832  if (Raw < InputInstance.TypeData[TypeIndex].SiMin)
2833  {
2834  Raw = InputInstance.TypeData[TypeIndex].SiMin;
2835  }
2836  }
2837  }
2838 
2839  }
2840 
2841  return (Raw);
2842 }
2843 
2844 
2846 {
2847  RESULT Result = BUSY;
2848  TYPES *pTmp;
2849 
2850 
2851  if (InputInstance.UartFile >= MIN_HANDLE)
2852  { // Driver installed
2853 
2854  if (((*InputInstance.pUart).Status[Port] & UART_PORT_CHANGED))
2855  { // something has changed
2856 
2857  if (InputInstance.TmpMode[Port] > 0)
2858  { // check each mode
2859 
2860  InputInstance.TmpMode[Port]--;
2861 
2862  // Get info
2863  InputInstance.UartCtl.Port = Port;
2864  InputInstance.UartCtl.Mode = InputInstance.TmpMode[Port];
2865  ioctl(InputInstance.UartFile,UART_READ_MODE_INFO,&InputInstance.UartCtl);
2866 
2867  if (InputInstance.UartCtl.TypeData.Name[0])
2868  { // Info available
2869 
2870  Result = cInputGetNewTypeDataPointer(InputInstance.UartCtl.TypeData.Name,InputInstance.UartCtl.TypeData.Type,InputInstance.UartCtl.TypeData.Mode,CONN_INPUT_UART,&pTmp);
2871  if (pTmp != NULL)
2872  { // Tabel index found
2873 
2874  if (InputInstance.DeviceType[Port] == TYPE_UNKNOWN)
2875  { // Use first mode info to set type
2876 
2877  InputInstance.DeviceType[Port] = InputInstance.UartCtl.TypeData.Type;
2878  }
2879 
2880  if (Result == OK)
2881  { // New mode
2882 
2883  // Insert in tabel
2884  Memcpy(pTmp,&InputInstance.UartCtl.TypeData,sizeof(TYPES));
2885 
2886  }
2887 #ifndef DISABLE_DAISYCHAIN
2888  if (cInputComSetDeviceInfo(MAX_DEVICE_INFOLENGTH,(UBYTE*)&InputInstance.UartCtl.TypeData) == BUSY)
2889  { // Chain not ready - roll back
2890 
2891  ioctl(InputInstance.UartFile,UART_NACK_MODE_INFO,&InputInstance.UartCtl);
2892  InputInstance.TmpMode[Port]++;
2893  }
2894 #endif
2895 
2896 #ifdef BUFPRINTSIZE
2897  BufPrint('p',"P=%d T=%-3d M=%d N=%s\r\n",Port,InputInstance.UartCtl.TypeData.Type,InputInstance.UartCtl.TypeData.Mode,InputInstance.UartCtl.TypeData.Name);
2898 #endif
2899  }
2900  }
2901  }
2902  else
2903  { // All modes received set device mode 0
2904 
2905  InputInstance.UartCtl.Port = Port;
2906  ioctl(InputInstance.UartFile,UART_CLEAR_CHANGED,&InputInstance.UartCtl);
2907  (*InputInstance.pUart).Status[Port] &= ~UART_PORT_CHANGED;
2908  cInputSetDeviceType(Port,InputInstance.DeviceType[Port],0,__LINE__);
2909  }
2910  }
2911 
2912  if (((*InputInstance.pUart).Status[Port] & UART_DATA_READY))
2913  {
2914  if (!((*InputInstance.pUart).Status[Port] & UART_PORT_CHANGED))
2915  {
2916  Result = OK;
2917  }
2918  }
2919  }
2920 
2921  return (Result);
2922 }
2923 
2924 
2926 {
2927  RESULT Result = BUSY;
2928  DATA8 Type;
2929  DATA8 Mode;
2930  UWORD Index;
2931 
2932  if (InputInstance.IicFile >= MIN_HANDLE)
2933  { // Driver installed
2934 
2935  if ((*InputInstance.pAnalog).InDcm[Port] == TYPE_NXT_IIC)
2936  {
2937 
2938  if ((*InputInstance.pIic).Changed[Port])
2939  { // something has changed
2940 
2941  InputInstance.IicStr.Port = Port;
2942 
2943  ioctl(InputInstance.IicFile,IIC_READ_TYPE_INFO,&InputInstance.IicStr);
2944 
2945  Index = IIC_NAME_LENGTH;
2946  while ((Index) && ((InputInstance.IicStr.Manufacturer[Index] == ' ') || (InputInstance.IicStr.Manufacturer[Index] == 0)))
2947  {
2948  InputInstance.IicStr.Manufacturer[Index] = 0;
2949  Index--;
2950  }
2951  Index = IIC_NAME_LENGTH;
2952  while ((Index) && ((InputInstance.IicStr.SensorType[Index] == ' ') || (InputInstance.IicStr.SensorType[Index] == 0)))
2953  {
2954  InputInstance.IicStr.SensorType[Index] = 0;
2955  Index--;
2956  }
2957 
2958  // Find 3th party type
2959  Type = TYPE_IIC_UNKNOWN;
2960  Mode = 0;
2961  Index = 0;
2962  while ((Index < InputInstance.IicDeviceTypes) && (Type == TYPE_IIC_UNKNOWN))
2963  { // Check list
2964 
2965  if (strcmp((char*)InputInstance.IicStr.Manufacturer,(char*)InputInstance.IicString[Index].Manufacturer) == 0)
2966  { // Manufacturer found
2967 
2968  if (strcmp((char*)InputInstance.IicStr.SensorType,(char*)InputInstance.IicString[Index].SensorType) == 0)
2969  { // Type found
2970 
2971  Type = InputInstance.IicString[Index].Type;
2972  Mode = InputInstance.IicString[Index].Mode;
2973  }
2974  }
2975  Index++;
2976  }
2977  cInputSetDeviceType(Port,Type,Mode,__LINE__);
2978  }
2979  (*InputInstance.pIic).Changed[Port] = 0;
2980  }
2981  if (((*InputInstance.pIic).Status[Port] & IIC_DATA_READY))
2982  {
2983  Result = OK;
2984  }
2985  else
2986  {
2987  Result = BUSY;
2988  }
2989  }
2990 
2991  return (Result);
2992 }
2993 
2994 
3001 {
3002  RESULT Result = BUSY;
3003  DATA8 Device;
3004  DATA8 Port;
3005 #ifndef DISABLE_DAISYCHAIN
3006  TYPES Tmp;
3007  TYPES *pTmp;
3008  DATA16 Index;
3009  DATA8 Layer;
3010  DATA8 Output;
3011  DATA8 Type;
3012  DATA8 Mode;
3013 #endif
3014 
3015  if (InputInstance.DCMUpdate)
3016  {
3017  for (Device = 0;Device < DEVICES;Device++)
3018  {
3019 
3020  if ((Device >= 0) && (Device < INPUTS))
3021  { // Device is local input port
3022 
3023  Port = Device;
3024 
3025  if (InputInstance.DeviceData[Device].Connection != (*InputInstance.pAnalog).InConn[Port])
3026  { // Connection type has changed
3027 
3028  InputInstance.DeviceData[Device].Connection = (*InputInstance.pAnalog).InConn[Port];
3029  cInputSetDeviceType(Device,(*InputInstance.pAnalog).InDcm[Port],0,__LINE__);
3030  InputInstance.DeviceMode[Device] = 0;
3031  InputInstance.TmpMode[Device] = MAX_DEVICE_MODES;
3032  InputInstance.DeviceData[Device].DevStatus = BUSY;
3033  }
3034 
3035  if (InputInstance.DeviceData[Device].Connection == CONN_INPUT_UART)
3036  { // UART device
3037 
3038  Result = cInputCheckUartInfo(Port);
3039 
3040  }
3041  else
3042  {
3043  if (InputInstance.DeviceData[Device].Connection == CONN_NXT_IIC)
3044  { // IIC device
3045 
3046  Result = cInputCheckIicInfo(Port);
3047  }
3048  else
3049  { // Analogue device
3050 
3051  if ((*InputInstance.pAnalog).Updated[Device])
3052  {
3053  Result = OK;
3054  }
3055  }
3056  }
3057  }
3058  else
3059  {
3060  if ((Device >= INPUT_DEVICES) && (Device < (INPUT_DEVICES + OUTPUTS)))
3061  { // Device is local output port
3062 
3063  Port = Device - INPUT_DEVICES;
3064 
3065  if (InputInstance.DeviceData[Device].Connection != (*InputInstance.pAnalog).OutConn[Port])
3066  { // Connection type has changed
3067 
3068  InputInstance.DeviceData[Device].Connection = (*InputInstance.pAnalog).OutConn[Port];
3069  cInputSetDeviceType(Device,(*InputInstance.pAnalog).OutDcm[Port],0,__LINE__);
3070  }
3071 
3072  Result = OK;
3073 
3074  }
3075  else
3076  { // Device is from daisy chain
3077 
3078 #ifndef DISABLE_DAISYCHAIN
3079 
3080  cInputExpandDevice(Device,&Layer,&Port,&Output);
3081 
3082  Result = cInputComGetDeviceType(Layer,Port,MAX_DEVICE_DATALENGTH,&Type,&Mode,(DATA8*)&Tmp);
3083 
3084  if ((Type > 0) && (Type <= MAX_VALID_TYPE) && (Result != FAIL))
3085  {
3086  InputInstance.DeviceData[Device].Connection = CONN_DAISYCHAIN;
3087  }
3088  else
3089  {
3090  Type = TYPE_NONE;
3091  InputInstance.DeviceData[Device].Connection = CONN_NONE;
3092  }
3093 
3094  if (InputInstance.DeviceType[Device] != Type)
3095  {
3096  cInputSetDeviceType(Device,Type,0,__LINE__);
3097  }
3098 #else
3099 
3100  InputInstance.DeviceData[Device].Connection = CONN_NONE;
3101 
3102 #endif
3103 
3104  }
3105  }
3106 
3107  if ((InputInstance.DeviceData[Device].Connection == CONN_NONE) || (InputInstance.DeviceData[Device].Connection == CONN_ERROR))
3108  {
3109  InputInstance.DeviceData[Device].DevStatus = BUSY;
3110  }
3111  else
3112  {
3113  if (InputInstance.DeviceData[Device].InvalidTime >= Time)
3114  {
3115  InputInstance.DeviceData[Device].InvalidTime -= Time;
3116  InputInstance.DeviceData[Device].DevStatus = BUSY;
3117  }
3118  else
3119  {
3120  InputInstance.DeviceData[Device].InvalidTime = 0;
3121  if (Result == OK)
3122  {
3123  InputInstance.DeviceData[Device].DevStatus = OK;
3124 
3125 #ifdef BUFPRINTSIZE
3126  BufPrint('p',"D=%-2d M=%d OK\r\n",(int)Device,InputInstance.DeviceMode[Device]);
3127 #endif
3128  }
3129  else
3130  {
3131  InputInstance.DeviceData[Device].DevStatus = BUSY;
3132  }
3133  }
3134  }
3135  if (InputInstance.DeviceData[Device].TimeoutTimer >= Time)
3136  {
3137  InputInstance.DeviceData[Device].TimeoutTimer -= Time;
3138  }
3139  else
3140  {
3141  InputInstance.DeviceData[Device].TimeoutTimer = 0;
3142  }
3143 
3144 
3145  }
3146  }
3147 
3148 #ifndef DISABLE_DAISYCHAIN
3149 
3151  {
3152  Result = cInputGetNewTypeDataPointer(Tmp.Name,Tmp.Type,Tmp.Mode,Tmp.Connection,&pTmp);
3153  if (pTmp != NULL)
3154  {
3155  if (Result == OK)
3156  {
3157  (*pTmp) = Tmp;
3158 #ifdef DEBUG_TRACE_DAISYCHAIN
3159  printf("c_input cInputDcmUpdate: NEW T=%-3d M=%d C=%-3d N=%s\r\n",Tmp.Type,Tmp.Mode,Tmp.Connection,Tmp.Name);
3160 #endif
3161  }
3162 #ifdef DEBUG_TRACE_DAISYCHAIN
3163  else
3164  {
3165  printf("c_input cInputDcmUpdate: KNOWN T=%-3d M=%d C=%-3d N=%s\r\n",Tmp.Type,Tmp.Mode,Tmp.Connection,Tmp.Name);
3166  }
3167 #endif
3168  }
3169  }
3170 
3171  if (InputInstance.TypeDataIndex < InputInstance.MaxDeviceTypes)
3172  { // Upload TypeData info through daisy chain
3173 
3174  if (InputInstance.TypeDataTimer == 0)
3175  {
3176  Index = InputInstance.TypeDataIndex;
3177  InputInstance.TypeDataIndex++;
3178 
3179  if (InputInstance.TypeData[Index].Name[0] != 0)
3180  {
3181  if (InputInstance.TypeData[Index].Type <= MAX_VALID_TYPE)
3182  { // Entry valid
3183 
3184  if (cInputComSetDeviceInfo(MAX_DEVICE_INFOLENGTH,(UBYTE*)&InputInstance.TypeData[Index]) == BUSY)
3185  { // Roll back
3186 
3187  InputInstance.TypeDataIndex--;
3188 
3189  #ifdef ENABLE_STATUS_TEST
3190  VMInstance.Status |= 0x10;
3191  #endif
3192  }
3193  #ifdef ENABLE_STATUS_TEST
3194  else
3195  {
3196  VMInstance.Status &= ~0x10;
3197  }
3198  #endif
3199 
3200  }
3201  }
3202  }
3203  else
3204  {
3205  if (InputInstance.TypeDataTimer >= Time)
3206  {
3207  InputInstance.TypeDataTimer -= Time;
3208  }
3209  else
3210  {
3211  InputInstance.TypeDataTimer = 0;
3212  }
3213  }
3214  }
3215  else
3216  {
3217  InputInstance.TypeDataIndex = DATA16_MAX;
3218  }
3219 
3220 #endif
3221 #ifdef ENABLE_STATUS_TEST
3222  VMInstance.Status &= ~0x07;
3223 
3224  if (InputInstance.DeviceData[TESTDEVICE].DevStatus == OK)
3225  {
3226  VMInstance.Status |= 0x01;
3227  }
3228  else
3229  {
3230  if (InputInstance.DeviceData[TESTDEVICE].DevStatus == BUSY)
3231  {
3232  VMInstance.Status |= 0x02;
3233  }
3234  else
3235  {
3236  VMInstance.Status |= 0x04;
3237  }
3238  }
3239 #endif
3240 }
3241 
3242 
3244 {
3245  InputInstance.TypeDataIndex = 0;
3246  InputInstance.TypeDataTimer = DELAY_TO_TYPEDATA;
3247 
3248  return (OK);
3249 }
3250 
3251 
3252 #ifdef Linux_X86
3253 void cInputSimulate(UWORD Time,DATA8 Device,DATA8 Connection,DATA8 Type)
3254 {
3255  DATAF Raw;
3256  DATAF Min;
3257  DATAF Max;
3258  DATAF Inc;
3259  UWORD Timeout;
3260 
3261  InputInstance.DeviceData[Device].Timer += Time;
3262 
3263  Timeout = 20;
3264  Raw = cInputReadDeviceRaw(Device,0,0,NULL);
3265  Min = InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].RawMin;
3266  Max = InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].RawMax;
3267  Inc = (Max - Min) / (DATAF)100;
3268  if ((Inc < (DATAF)0) && (Inc > (DATAF)-1))
3269  {
3270  Inc = -1;
3271  Timeout = 200;
3272  }
3273  if ((Inc > (DATAF)0) && (Inc < (DATAF)1))
3274  {
3275  Inc = 1;
3276  Timeout = 200;
3277  }
3278 
3279  if (InputInstance.DeviceData[Device].Timer >= Timeout)
3280  {
3281  InputInstance.DeviceData[Device].Timer = 0;
3282 
3283  if (isnan(Raw))
3284  {
3285  Raw = (DATAF)0;
3286  }
3287 
3288  if (InputInstance.DeviceData[Device].Dir)
3289  { // Down
3290 
3291  Raw -= Inc;
3292 
3293  if (Inc >= (DATAF)0)
3294  {
3295 
3296  if (Raw <= Min)
3297  {
3298  Raw = Min;
3299  InputInstance.DeviceData[Device].Dir = 0;
3300  }
3301  if (Raw >= Max)
3302  {
3303  Raw = Max;
3304  }
3305  }
3306  else
3307  {
3308  if (Raw >= Min)
3309  {
3310  Raw = Min;
3311  InputInstance.DeviceData[Device].Dir = 0;
3312  }
3313  if (Raw <= Max)
3314  {
3315  Raw = Max;
3316  }
3317  }
3318  }
3319  else
3320  { // Up
3321 
3322  Raw += Inc;
3323 
3324  if (Inc >= (DATAF)0)
3325  {
3326  if (Raw <= Min)
3327  {
3328  Raw = Min;
3329  }
3330  if (Raw >= Max)
3331  {
3332  Raw = Max;
3333  InputInstance.DeviceData[Device].Dir = 1;
3334  }
3335  }
3336  else
3337  {
3338  if (Raw >= Min)
3339  {
3340  Raw = Min;
3341  }
3342  if (Raw <= Max)
3343  {
3344  Raw = Max;
3345  InputInstance.DeviceData[Device].Dir = 1;
3346  }
3347  }
3348  }
3349 
3350  cInputWriteDeviceRaw(Device,Connection,Type,Raw);
3351  }
3352 }
3353 #endif
3354 
3355 
3357 {
3358 #ifndef DISABLE_BUMBED
3359  DATA8 Device;
3360  DATAF Value;
3361  DATAF Diff;
3362 #endif
3363 
3364  cInputDcmUpdate(Time);
3365 
3366 #ifndef DISABLE_BUMBED
3367  for (Device = 0;Device < INPUT_PORTS;Device++)
3368  { // check each port for changes
3369 
3370  if ((InputInstance.DeviceType[Device] == 1) || (InputInstance.DeviceType[Device] == 16))
3371  {
3372  if (InputInstance.DeviceType[Device] == 1)
3373  {
3374  Value = (DATAF)(*InputInstance.pAnalog).InPin1[Device];
3375  }
3376  if (InputInstance.DeviceType[Device] == 16)
3377  {
3378  Value = (DATAF)(*InputInstance.pAnalog).InPin6[Device];
3379  }
3380 
3381  Diff = Value - InputInstance.DeviceData[Device].OldRaw;
3382 
3383  if (Diff >= (DATAF)500)
3384  {
3385  InputInstance.DeviceData[Device].Bumps += (DATA32)1;
3386  }
3387  if (Diff <= (DATAF)-500)
3388  {
3389  InputInstance.DeviceData[Device].Changes += (DATA32)1;
3390  }
3391 
3392  InputInstance.DeviceData[Device].OldRaw = Value;
3393  }
3394  else
3395  {
3396  InputInstance.DeviceData[Device].Changes = (DATA32)0;
3397  InputInstance.DeviceData[Device].Bumps = (DATA32)0;
3398  InputInstance.DeviceData[Device].OldRaw = (DATAF)0;
3399  }
3400  }
3401 #endif
3402 #ifdef Linux_X86
3403  cInputSimulate(Time,0,CONN_UNKNOWN,2);
3404  cInputSimulate(Time,1,CONN_UNKNOWN,3);
3405  cInputSimulate(Time,2,CONN_UNKNOWN,1);
3406  cInputSimulate(Time,3,CONN_UNKNOWN,4);
3407  cInputSimulate(Time,16,CONN_UNKNOWN,7);
3408  cInputSimulate(Time,17,CONN_UNKNOWN,7);
3409  cInputSimulate(Time,18,CONN_UNKNOWN,8);
3410  cInputSimulate(Time,19,CONN_UNKNOWN,8);
3411 #endif
3412 }
3413 
3414 
3415 RESULT cInputInit(void)
3416 {
3417  RESULT Result = OK;
3418  ANALOG *pAdcTmp;
3419  UART *pUartTmp;
3420  IIC *pIicTmp;
3421  PRGID TmpPrgId;
3422  UWORD Tmp;
3423  UWORD Set;
3424 
3425  InputInstance.TypeDataIndex = DATA16_MAX;
3426 
3427  InputInstance.MaxDeviceTypes = 3;
3428 
3429  cMemoryMalloc((void*)&InputInstance.TypeData,(DATA32)(sizeof(TYPES) * InputInstance.MaxDeviceTypes));
3430 
3431  InputInstance.IicDeviceTypes = 1;
3432 
3433  cMemoryMalloc((void*)&InputInstance.IicString,(DATA32)(sizeof(IICSTR) * InputInstance.IicDeviceTypes));
3434 
3435  InputInstance.pAnalog = &InputInstance.Analog;
3436 
3437  InputInstance.pUart = &InputInstance.Uart;
3438 
3439  InputInstance.pIic = &InputInstance.Iic;
3440 
3441  InputInstance.AdcFile = open(ANALOG_DEVICE_NAME,O_RDWR | O_SYNC);
3442  InputInstance.UartFile = open(UART_DEVICE_NAME,O_RDWR | O_SYNC);
3443  InputInstance.DcmFile = open(DCM_DEVICE_NAME,O_RDWR | O_SYNC);
3444  InputInstance.IicFile = open(IIC_DEVICE_NAME,O_RDWR | O_SYNC);
3445  InputInstance.DCMUpdate = 1;
3446 
3447  if (InputInstance.AdcFile >= MIN_HANDLE)
3448  {
3449  pAdcTmp = (ANALOG*)mmap(0, sizeof(ANALOG), PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, InputInstance.AdcFile, 0);
3450 
3451  if (pAdcTmp == MAP_FAILED)
3452  {
3453 #ifndef Linux_X86
3455  Result = FAIL;
3456 #endif
3457  InputInstance.DCMUpdate = 0;
3458  }
3459  else
3460  {
3461  InputInstance.pAnalog = pAdcTmp;
3462  }
3463  }
3464  else
3465  {
3466 #ifndef Linux_X86
3468  Result = FAIL;
3469 #endif
3470  InputInstance.DCMUpdate = 0;
3471  }
3472 
3473 
3474  if (InputInstance.UartFile >= MIN_HANDLE)
3475  {
3476  pUartTmp = (UART*)mmap(0, sizeof(UART), PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, InputInstance.UartFile, 0);
3477 
3478  if (pUartTmp == MAP_FAILED)
3479  {
3480 #ifndef Linux_X86
3482  Result = FAIL;
3483 #endif
3484  InputInstance.DCMUpdate = 0;
3485  }
3486  else
3487  {
3488  InputInstance.pUart = pUartTmp;
3489  }
3490  }
3491  else
3492  {
3493 #ifndef Linux_X86
3495  Result = FAIL;
3496 #endif
3497 // InputInstance.DCMUpdate = 0;
3498  }
3499 
3500 
3501  if (InputInstance.IicFile >= MIN_HANDLE)
3502  {
3503  pIicTmp = (IIC*)mmap(0, sizeof(UART), PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, InputInstance.IicFile, 0);
3504 
3505  if (pIicTmp == MAP_FAILED)
3506  {
3507 #ifndef Linux_X86
3509  Result = FAIL;
3510 #endif
3511  InputInstance.DCMUpdate = 0;
3512  }
3513  else
3514  {
3515  InputInstance.pIic = pIicTmp;
3516  }
3517  }
3518  else
3519  {
3520 #ifndef Linux_X86
3522  Result = FAIL;
3523 #endif
3524 // InputInstance.DCMUpdate = 0;
3525  }
3526 
3527 
3530 
3531  for (Tmp = 0;Tmp < DEVICES;Tmp++)
3532  {
3533  for (Set = 0;Set < MAX_DEVICE_DATASETS;Set++)
3534  {
3535  InputInstance.DeviceData[Tmp].Raw[Set] = DATAF_NAN;
3536  }
3537  InputInstance.DeviceData[Tmp].Owner = 0;
3538  InputInstance.DeviceData[Tmp].Busy = 0;
3539  InputInstance.DeviceData[Tmp].Connection = CONN_NONE;
3540  InputInstance.DeviceData[Tmp].DevStatus = BUSY;
3541  InputInstance.DeviceData[Tmp].TypeIndex = InputInstance.NoneIndex;
3542  InputInstance.DeviceType[Tmp] = TYPE_NONE;
3543  InputInstance.DeviceMode[Tmp] = 0;
3544 #ifndef DISABLE_BUMBED
3545  InputInstance.DeviceData[Tmp].Changes = (DATA32)0;
3546  InputInstance.DeviceData[Tmp].Bumps = (DATA32)0;
3547 #endif
3548  }
3549 
3550  for (Tmp = 0;Tmp < INPUT_PORTS;Tmp++)
3551  {
3552  InputInstance.TmpMode[Tmp] = MAX_DEVICE_MODES;
3553  }
3554 
3555  for (TmpPrgId = 0;TmpPrgId < MAX_PROGRAMS;TmpPrgId++)
3556  {
3557  InputInstance.ConfigurationChanged[TmpPrgId] = 0;
3558  }
3559 
3560  return (Result);
3561 }
3562 
3563 
3564 RESULT cInputOpen(void)
3565 {
3566  RESULT Result = FAIL;
3567 
3568  Result = OK;
3569 
3570  return (Result);
3571 }
3572 
3573 
3574 RESULT cInputClose(void)
3575 {
3576  RESULT Result = FAIL;
3577 
3578  Result = OK;
3579 
3580  return (Result);
3581 }
3582 
3583 
3584 RESULT cInputExit(void)
3585 {
3586  RESULT Result = FAIL;
3587 
3589 
3590  if (InputInstance.AdcFile >= MIN_HANDLE)
3591  {
3592  munmap(InputInstance.pAnalog,sizeof(ANALOG));
3593  close(InputInstance.AdcFile);
3594  }
3595 
3596  if (InputInstance.UartFile >= MIN_HANDLE)
3597  {
3598  munmap(InputInstance.pUart,sizeof(UART));
3599  close(InputInstance.UartFile);
3600  }
3601 
3602  if (InputInstance.IicFile >= MIN_HANDLE)
3603  {
3604  munmap(InputInstance.pIic,sizeof(IIC));
3605  close(InputInstance.IicFile);
3606  }
3607 
3608  if (InputInstance.DcmFile >= MIN_HANDLE)
3609  {
3610  close(InputInstance.DcmFile);
3611  }
3612 
3613  if (InputInstance.IicString != NULL)
3614  {
3615  free((void*)InputInstance.IicString);
3616  }
3617  if (InputInstance.TypeData != NULL)
3618  {
3619  free((void*)InputInstance.TypeData);
3620  }
3621 
3622  Result = OK;
3623 
3624  return (Result);
3625 }
3626 
3627 
3629 {
3630  DATA8 Layer;
3631  DATA8 No;
3632 
3633  Layer = *(DATA8*)PrimParPointer();
3634  No = *(DATA8*)PrimParPointer();
3635 
3636  return (No + (Layer * INPUT_PORTS));
3637 }
3638 
3639 
3640 void cInputSetType(DATA8 Device,DATA8 Type,DATA8 Mode,int Line)
3641 {
3642  if (InputInstance.DeviceData[Device].DevStatus == OK)
3643  {
3644  if (Type == TYPE_KEEP)
3645  {
3646  Type = InputInstance.DeviceType[Device];
3647  }
3648  if (Mode == MODE_KEEP)
3649  { // Get actual mode
3650 
3651  Mode = InputInstance.DeviceMode[Device];
3652  }
3653  if ((InputInstance.DeviceType[Device] != Type) || (InputInstance.DeviceMode[Device] != Mode))
3654  {
3655  cInputSetDeviceType(Device,Type,Mode,Line);
3656  }
3657  }
3658 }
3659 
3660 
3661 //******* BYTE CODE SNIPPETS **************************************************
3662 
3663 
3679 {
3680  PRGID TmpPrgId;
3681  DATA8 Length;
3682  DATA8 *pDevices;
3683  DATA8 *pChanged;
3684  DATA8 Count;
3685 
3686  TmpPrgId = CurrentProgramId();
3687  Length = *(DATA8*)PrimParPointer();
3688  pDevices = (DATA8*)PrimParPointer();
3689  pChanged = (DATA8*)PrimParPointer();
3690 
3691  *pChanged = InputInstance.ConfigurationChanged[TmpPrgId];
3692  InputInstance.ConfigurationChanged[TmpPrgId] = 0;
3693  Count = 0;
3694  while ((Count < Length) && (Count < DEVICES))
3695  {
3696  pDevices[Count] = InputInstance.DeviceType[Count];
3697 
3698  Count++;
3699  }
3700  if (Count < Length)
3701  {
3702  pDevices[Count] = 0;
3703  }
3704 }
3705 
3706 
3887 void cInputDevice(void)
3888 {
3889  IP TmpIp;
3890  DATA8 Cmd;
3891  DATA8 Device = 0;
3892  DATA8 Layer;
3893  DATA8 Length;
3894  DATA8 *pDestination;
3895  DATA8 Count = 0;
3896  DATA8 Modes = 0;
3897  DATA8 *TmpName;
3898  DATA8 Data8 = 0;
3899  DATA32 Data32 = 0;
3900  DATA8 Type;
3901  DATA8 Mode;
3902  DATA8 Connection;
3903  DATA8 Values;
3904  DATA8 Value;
3905  DATA8 Views = 0;
3906  UWORD Index = 0;
3907  OBJID Owner;
3908  DATA8 Busy;
3909  DATAF DataF = (DATAF)0;
3910  DATAF Min = (DATAF)0;
3911  DATAF Max = (DATAF)0;
3912  DATA8 Tmp;
3913  DATA8 Repeat;
3914  DATA16 Time;
3915  DATA8 WrLng;
3916  DATA8 RdLng;
3917  DATA8 *pWrData;
3918  DATA8 *pRdData;
3919  UWORD TypeIndex;
3920 #ifndef DISABLE_DAISYCHAIN_COM_CALL
3921  RESULT Result;
3922 #endif
3923 
3924 
3925  TmpIp = GetObjectIp();
3926  Cmd = *(DATA8*)PrimParPointer();
3927  if ((Cmd != CAL_MINMAX) && (Cmd != CAL_MIN) && (Cmd != CAL_MAX) && (Cmd != CAL_DEFAULT) && (Cmd != CLR_ALL) && (Cmd != STOP_ALL))
3928  {
3929  Device = cInputGetDevice();
3930  }
3931 
3932  switch (Cmd)
3933  { // Function
3934 
3935  case CAL_MINMAX :
3936  {
3937  Type = *(DATA8*)PrimParPointer();
3938  Mode = *(DATA8*)PrimParPointer();
3939  Min = (DATAF)*(DATA32*)PrimParPointer();
3940  Max = (DATAF)*(DATA32*)PrimParPointer();
3941 
3942  if ((Type > 0) && (Type < (MAX_DEVICE_TYPE + 1)) && (Mode >= 0) && (Mode < MAX_DEVICE_MODES))
3943  {
3944  InputInstance.Calib[Type][Mode].Min = Min;
3945  InputInstance.Calib[Type][Mode].Max = Max;
3946  }
3947  }
3948  break;
3949 
3950  case CAL_MIN :
3951  {
3952  Type = *(DATA8*)PrimParPointer();
3953  Mode = *(DATA8*)PrimParPointer();
3954  Min = (DATAF)*(DATA32*)PrimParPointer();
3955 
3956  if ((Type > 0) && (Type < (MAX_DEVICE_TYPE + 1)) && (Mode >= 0) && (Mode < MAX_DEVICE_MODES))
3957  {
3958  if (cInputFindDevice(Type,Mode,&TypeIndex) == OK)
3959  {
3960  if (InputInstance.Calib[Type][Mode].InUse == 0)
3961  {
3962  InputInstance.Calib[Type][Mode].InUse = 1;
3963  InputInstance.Calib[Type][Mode].Max = InputInstance.TypeData[TypeIndex].RawMax;
3964  }
3965  else
3966  {
3967  Min = InputInstance.Calib[Type][Mode].Min + (((Min - InputInstance.TypeData[TypeIndex].SiMin) * (InputInstance.Calib[Type][Mode].Max - InputInstance.Calib[Type][Mode].Min)) / (InputInstance.TypeData[TypeIndex].SiMax - InputInstance.TypeData[TypeIndex].SiMin));
3968  }
3969  InputInstance.Calib[Type][Mode].Min = Min;
3970  }
3971  }
3972  }
3973  break;
3974 
3975  case CAL_MAX :
3976  {
3977  Type = *(DATA8*)PrimParPointer();
3978  Mode = *(DATA8*)PrimParPointer();
3979  Max = (DATAF)*(DATA32*)PrimParPointer();
3980 
3981  if ((Type > 0) && (Type < (MAX_DEVICE_TYPE + 1)) && (Mode >= 0) && (Mode < MAX_DEVICE_MODES))
3982  {
3983  if (cInputFindDevice(Type,Mode,&TypeIndex) == OK)
3984  {
3985  if (InputInstance.Calib[Type][Mode].InUse == 0)
3986  {
3987  InputInstance.Calib[Type][Mode].InUse = 1;
3988  InputInstance.Calib[Type][Mode].Min = InputInstance.TypeData[TypeIndex].RawMin;
3989  }
3990  else
3991  {
3992  Max = InputInstance.Calib[Type][Mode].Min + (((Max - InputInstance.TypeData[TypeIndex].SiMin) * (InputInstance.Calib[Type][Mode].Max - InputInstance.Calib[Type][Mode].Min)) / (InputInstance.TypeData[TypeIndex].SiMax - InputInstance.TypeData[TypeIndex].SiMin));
3993  }
3994  InputInstance.Calib[Type][Mode].Max = Max;
3995  }
3996  }
3997  }
3998  break;
3999 
4000  case CAL_DEFAULT :
4001  {
4002  Type = *(DATA8*)PrimParPointer();
4003  Mode = *(DATA8*)PrimParPointer();
4004 
4005  if ((Type > 0) && (Type < (MAX_DEVICE_TYPE + 1)) && (Mode >= 0) && (Mode < MAX_DEVICE_MODES))
4006  {
4007  InputInstance.Calib[Type][Mode].InUse = 0;
4008  }
4009  }
4010  break;
4011 
4012  case GET_TYPEMODE :
4013  {
4014  Type = TYPE_NONE;
4015  Mode = 0;
4016 
4017  if (Device < DEVICES)
4018  {
4019  Type = InputInstance.DeviceType[Device];
4020  Mode = InputInstance.DeviceMode[Device];
4021  }
4022  *(DATA8*)PrimParPointer() = Type;
4023  *(DATA8*)PrimParPointer() = Mode;
4024  }
4025  break;
4026 
4027  case GET_CONNECTION :
4028  {
4029  Connection = TYPE_NONE;
4030 
4031  if (Device < DEVICES)
4032  {
4033  Connection = InputInstance.DeviceData[Device].Connection;
4034  }
4035  *(DATA8*)PrimParPointer() = Connection;
4036  }
4037  break;
4038 
4039  case GET_NAME :
4040  {
4041  Length = *(DATA8*)PrimParPointer();
4042  pDestination = (DATA8*)PrimParPointer();
4043  Count = 0;
4044 
4045  if (Device < DEVICES)
4046  {
4047 
4048  TmpName = InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].Name;
4049 
4050  if (VMInstance.Handle >= 0)
4051  {
4052  Tmp = (DATA8)strlen((char*)TmpName) + 1;
4053 
4054  if (Length == -1)
4055  {
4056  Length = Tmp;
4057  }
4058  pDestination = (DATA8*)VmMemoryResize(VMInstance.Handle,(DATA32)Length);
4059  }
4060  if (pDestination != NULL)
4061  {
4062  while ((Count < (Length - 1)) && (TmpName[Count]))
4063  {
4064  pDestination[Count] = TmpName[Count];
4065 
4066  Count++;
4067  }
4068  while (Count < (Length - 1))
4069  {
4070  pDestination[Count] = ' ';
4071 
4072  Count++;
4073  }
4074  }
4075  }
4076  if (pDestination != NULL)
4077  {
4078  pDestination[Count] = 0;
4079  }
4080  }
4081  break;
4082 
4083  case GET_SYMBOL :
4084  {
4085  Length = *(DATA8*)PrimParPointer();
4086  pDestination = (DATA8*)PrimParPointer();
4087  Count = 0;
4088 
4089  if (Device < DEVICES)
4090  {
4091 
4092  TmpName = InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].Symbol;
4093 
4094  if (VMInstance.Handle >= 0)
4095  {
4096  Tmp = (DATA8)strlen((char*)TmpName) + 1;
4097 
4098  if (Length == -1)
4099  {
4100  Length = Tmp;
4101  }
4102  pDestination = (DATA8*)VmMemoryResize(VMInstance.Handle,(DATA32)Length);
4103  }
4104  if (pDestination != NULL)
4105  {
4106  while ((Count < (Length - 1)) && (TmpName[Count]))
4107  {
4108  pDestination[Count] = TmpName[Count];
4109 
4110  Count++;
4111  }
4112  while (Count < (Length - 1))
4113  {
4114  pDestination[Count] = ' ';
4115 
4116  Count++;
4117  }
4118  }
4119  }
4120  if (pDestination != NULL)
4121  {
4122  pDestination[Count] = 0;
4123  }
4124  }
4125  break;
4126 
4127  case GET_FORMAT :
4128  {
4129  if (Device < DEVICES)
4130  {
4131  Count = (DATA8)InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].DataSets;
4132  Data8 = (DATA8)InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].Format;
4133  Modes = (DATA8)InputInstance.TypeModes[InputInstance.DeviceType[Device]];
4134  Views = (DATA8)InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].Views;
4135  }
4136  *(DATA8*)PrimParPointer() = Count;
4137  *(DATA8*)PrimParPointer() = Data8;
4138  *(DATA8*)PrimParPointer() = Modes;
4139  *(DATA8*)PrimParPointer() = Views;
4140  }
4141  break;
4142 
4143  case GET_RAW :
4144  {
4145  Data32 = DATA32_NAN;
4146  if (Device < DEVICES)
4147  {
4148  DataF = cInputReadDeviceRaw(Device,0,0,NULL);
4149  if (!isnan(DataF))
4150  {
4151  Data32 = (DATA32)DataF;
4152  }
4153  }
4154  *(DATA32*)PrimParPointer() = Data32;
4155  }
4156  break;
4157 
4158  case GET_FIGURES :
4159  {
4160  if (Device < DEVICES)
4161  {
4162  Count = (DATA8)InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].Figures;
4163  Data8 = (DATA8)InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].Decimals;
4164  }
4165  *(DATA8*)PrimParPointer() = Count;
4166  *(DATA8*)PrimParPointer() = Data8;
4167  }
4168  break;
4169 
4170  case GET_MINMAX :
4171  {
4172  if (Device < DEVICES)
4173  {
4174  Min = (DATAF)InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].SiMin;
4175  Max = (DATAF)InputInstance.TypeData[InputInstance.DeviceData[Device].TypeIndex].SiMax;
4176  }
4177  *(DATAF*)PrimParPointer() = Min;
4178  *(DATAF*)PrimParPointer() = Max;
4179  }
4180  break;
4181 
4182  case GET_MODENAME :
4183  {
4184  Mode = *(DATA8*)PrimParPointer();
4185  Length = *(DATA8*)PrimParPointer();
4186  pDestination = (DATA8*)PrimParPointer();
4187  Count = 0;
4188 
4189  if (Device < DEVICES)
4190  {
4191  Type = InputInstance.DeviceType[Device];
4192  if ((Type >= 0) && (Type < (MAX_DEVICE_TYPE + 1)))
4193  {
4194  // try to find device type
4195  Index = 0;
4196  while (Index < InputInstance.MaxDeviceTypes)
4197  {
4198  if (InputInstance.TypeData[Index].Type == Type)
4199  { // match on type
4200 
4201  if (InputInstance.TypeData[Index].Mode == Mode)
4202  { // match on mode
4203 
4204  TmpName = InputInstance.TypeData[Index].Name;
4205 
4206  if (VMInstance.Handle >= 0)
4207  {
4208  Tmp = (DATA8)strlen((char*)TmpName) + 1;
4209 
4210  if (Length == -1)
4211  {
4212  Length = Tmp;
4213  }
4214  pDestination = (DATA8*)VmMemoryResize(VMInstance.Handle,(DATA32)Length);
4215  }
4216  if (pDestination != NULL)
4217  {
4218  while ((Count < (Length - 1)) && (TmpName[Count]))
4219  {
4220  pDestination[Count] = TmpName[Count];
4221 
4222  Count++;
4223  }
4224  while (Count < (Length - 1))
4225  {
4226  pDestination[Count] = ' ';
4227 
4228  Count++;
4229  }
4230  }
4231  Index = InputInstance.MaxDeviceTypes;
4232  }
4233  }
4234  Index++;
4235  }
4236  }
4237  }
4238  if (pDestination != NULL)
4239  {
4240  pDestination[Count] = 0;
4241  }
4242  }
4243  break;
4244 
4245  case SET_RAW :
4246  {
4247  Type = *(DATA8*)PrimParPointer();
4248  Data32 = *(DATA32*)PrimParPointer();
4249 
4250  cInputWriteDeviceRaw(Device,InputInstance.DeviceData[Device].Connection,Type,Data32);
4251  }
4252  break;
4253 
4254 #ifndef DISABLE_BUMBED
4255  case GET_CHANGES :
4256  {
4257  DataF = DATAF_NAN;
4258  DataF = (DATAF)0;
4259  if (Device < DEVICES)
4260  {
4261  if ((InputInstance.DeviceData[Device].Connection != CONN_NONE) && (InputInstance.DeviceData[Device].Connection != CONN_ERROR))
4262  {
4263  DataF = (DATAF)InputInstance.DeviceData[Device].Changes;
4264  }
4265  }
4266  *(DATAF*)PrimParPointer() = DataF;
4267  }
4268  break;
4269 
4270  case GET_BUMPS :
4271  {
4272  DataF = DATAF_NAN;
4273  DataF = (DATAF)0;
4274  if (Device < DEVICES)
4275  {
4276  if ((InputInstance.DeviceData[Device].Connection != CONN_NONE) && (InputInstance.DeviceData[Device].Connection != CONN_ERROR))
4277  {
4278  DataF = (DATAF)InputInstance.DeviceData[Device].Bumps;
4279  }
4280  }
4281  *(DATAF*)PrimParPointer() = DataF;
4282  }
4283  break;
4284 
4285  case CLR_CHANGES :
4286  {
4287  if (Device < DEVICES)
4288  {
4289  InputInstance.DeviceData[Device].Changes = (DATA32)0;
4290  InputInstance.DeviceData[Device].Bumps = (DATA32)0;
4291  }
4292  }
4293  break;
4294 #endif
4295 
4296  case READY_PCT : // Fall through
4297  case READY_RAW : // Fall through
4298  case READY_SI :
4299  {
4300  Type = *(DATA8*)PrimParPointer();
4301  Mode = *(DATA8*)PrimParPointer();
4302  Values = *(DATA8*)PrimParPointer();
4303 
4304  Value = 0;
4305  Busy = 0;
4306  Owner = CallingObjectId();
4307 
4308 #ifdef DEBUG_C_INPUT_DAISYCHAIN
4309  if (Device == TESTDEVICE)
4310  {
4311  printf("c_input opINPUT_DEVICE READY_XX: D=%-3d T=%-3d M=%-3d B=%d C=%-3d\r\n",Device,Type,Mode,InputInstance.DeviceData[Device].DevStatus,InputInstance.DeviceData[Device].Connection);
4312  }
4313 #endif
4314  if (Device < DEVICES)
4315  { // Device valid
4316 
4317  if ((InputInstance.DeviceData[Device].Connection != CONN_NONE) && (InputInstance.DeviceData[Device].Connection != CONN_ERROR))
4318  { // Device present
4319 
4320  if (Type == TYPE_KEEP)
4321  { // Get actual type
4322 
4323  Type = InputInstance.DeviceType[Device];
4324  }
4325  if (Mode == MODE_KEEP)
4326  { // Get actual mode
4327 
4328  Mode = InputInstance.DeviceMode[Device];
4329  }
4330  if (InputInstance.DeviceData[Device].Busy == 0)
4331  {
4332  if ((InputInstance.DeviceType[Device] != Type) || (InputInstance.DeviceMode[Device] != Mode))
4333  { // Must change type or mode so check if owner is OK
4334 
4335  if ((InputInstance.DeviceData[Device].Owner == 0) || (InputInstance.DeviceData[Device].Owner == Owner))
4336  { // Owner is OK
4337 
4338 #ifdef DEBUG_TRACE_MODE_CHANGE
4339  printf("c_input opINPUT_DEVICE READY_XX: D=%-3d Change to type %d mode %d\r\n",Device,Type,Mode);
4340 #endif
4341  InputInstance.DeviceData[Device].Owner = Owner;
4342  cInputSetDeviceType(Device,Type,Mode,__LINE__);
4343  InputInstance.DeviceData[Device].TimeoutTimer = MAX_DEVICE_BUSY_TIME;
4344  InputInstance.DeviceData[Device].Busy = 0;
4345 #ifdef ENABLE_STATUS_TEST
4346  if (Device == TESTDEVICE)
4347  {
4348  VMInstance.Status &= ~0x40;
4349  }
4350 #endif
4351  }
4352  else
4353  { // Another owner
4354 
4355 #ifdef DEBUG_TRACE_MODE_CHANGE
4356  printf("c_input opINPUT_DEVICE READY_XX: D=%-3d Trying to change to type %d mode %d\r\n",Device,Type,Mode);
4357 #endif
4358 #ifdef ENABLE_STATUS_TEST
4359  if (Device == TESTDEVICE)
4360  {
4361  VMInstance.Status |= 0x40;
4362  }
4363 #endif
4364  Busy = 1;
4365  }
4366  }
4367  }
4368  if (Busy == 0)
4369  {
4370  if (InputInstance.DeviceData[Device].DevStatus == BUSY)
4371  {
4372  Busy = 1;
4373 
4374  if (InputInstance.DeviceData[Device].Busy == 0)
4375  {
4376  InputInstance.DeviceData[Device].TimeoutTimer = MAX_DEVICE_BUSY_TIME;
4377  InputInstance.DeviceData[Device].Busy = 1;
4378  }
4379  else
4380  {
4381  if (InputInstance.DeviceData[Device].TimeoutTimer == 0)
4382  {
4383  #ifdef DEBUG_TRACE_MODE_CHANGE
4384  printf("c_input opINPUT_DEVICE READY_XX: D=%-3d Timeout when trying to change to type %d mode %d\r\n",Device,Type,Mode);
4385  #endif
4386  InputInstance.DeviceData[Device].Owner = 0;
4387  InputInstance.DeviceData[Device].Busy = 0;
4388  Busy = 0;
4389  }
4390  }
4391  }
4392  else
4393  {
4394  while ((Value < Values) && (Value < MAX_DEVICE_DATASETS))
4395  {
4396  switch (Cmd)
4397  {
4398  case READY_PCT :
4399  {
4400  *(DATA8*)PrimParPointer() = (DATA8)cInputReadDevicePct(Device,Value,0,NULL);
4401  }
4402  break;
4403 
4404  case READY_RAW :
4405  {
4406  DataF = cInputReadDeviceRaw(Device,Value,0,NULL);
4407  if (isnan(DataF))
4408  {
4410  }
4411  else
4412  {
4413  *(DATA32*)PrimParPointer() = (DATA32)DataF;
4414  }
4415  }
4416  break;
4417 
4418  case READY_SI :
4419  {
4420  DataF = (DATAF)cInputReadDeviceSi(Device,Value,0,NULL);
4421  *(DATAF*)PrimParPointer() = DataF;
4422 
4423  }
4424  break;
4425 
4426  }
4427  Value++;
4428  }
4429  }
4430 
4431  }
4432  }
4433  else
4434  {
4435  SetInstructions(0);
4436  }
4437  if (InputInstance.DeviceData[Device].DevStatus != BUSY)
4438  {
4439  InputInstance.DeviceData[Device].Owner = 0;
4440  InputInstance.DeviceData[Device].Busy = 0;
4441  #ifdef ENABLE_STATUS_TEST
4442  if (Device == TESTDEVICE)
4443  {
4444  VMInstance.Status &= ~0x20;
4445  }
4446  #endif
4447  }
4448  #ifdef ENABLE_STATUS_TEST
4449  else
4450  {
4451  if (Device == TESTDEVICE)
4452  {
4453  VMInstance.Status |= 0x20;
4454  }
4455  }
4456  #endif
4457  }
4458 
4459  if (Busy)
4460  { // Busy -> block VMThread
4461 
4462  SetObjectIp(TmpIp - 1);
4464  }
4465  else
4466  { // Not busy -> be sure to pop all parameters
4467 
4468  while (Value < Values)
4469  {
4470  switch (Cmd)
4471  {
4472  case READY_PCT :
4473  {
4475  }
4476  break;
4477 
4478  case READY_RAW :
4479  {
4481  }
4482  break;
4483 
4484  case READY_SI :
4485  {
4487  }
4488  break;
4489 
4490  }
4491  Value++;
4492  }
4493  }
4494  }
4495  break;
4496 
4497  case SETUP :
4498  { // INPUT_DEVICE(SETUP,LAYER,NO,REPEAT,TIME,WRLNG,WRDATA,RDLNG,RDDATA)
4499 
4500  Repeat = *(DATA8*)PrimParPointer();
4501  Time = *(DATA16*)PrimParPointer();
4502  WrLng = *(DATA8*)PrimParPointer();
4503  pWrData = (DATA8*)PrimParPointer();
4504  RdLng = *(DATA8*)PrimParPointer();
4505  pRdData = (DATA8*)PrimParPointer();
4506 
4507  if (VMInstance.Handle >= 0)
4508  {
4509  pRdData = (DATA8*)VmMemoryResize(VMInstance.Handle,(DATA32)RdLng);
4510  }
4511  if (pRdData != NULL)
4512  {
4513 
4514  if (cInputSetupDevice(Device,Repeat,Time,WrLng,pWrData,RdLng,pRdData) == BUSY)
4515  { // Busy -> block VMThread
4516 
4517  SetObjectIp(TmpIp - 1);
4519  }
4520  }
4521  }
4522  break;
4523 
4524  case CLR_ALL :
4525  {
4526  Layer = *(DATA8*)PrimParPointer();
4527 
4528  if (Layer == 0)
4529  {
4530  ClrLayer();
4531  }
4532 #ifndef DISABLE_DAISYCHAIN_COM_CALL
4533  else
4534  {
4535  Result = OK;
4536 
4537  if (Layer == -1)
4538  {
4539  MoreLayers = 1;
4540  Layer = ActLayer;
4541  }
4542 
4543  if (Layer == 0)
4544  {
4545  ClrLayer();
4546  }
4547  else
4548  {
4549  Result = cDaisyReady();
4550 
4551  if (Result == OK)
4552  { // Ready for command
4553 
4554  DaisyBuf[0] = 0;
4555  DaisyBuf[1] = 0;
4556  DaisyBuf[2] = opINPUT_DEVICE;
4557  DaisyBuf[3] = CLR_ALL;
4558  DaisyBuf[4] = 0;
4559 
4560  Result = cDaisyDownStreamCmd(DaisyBuf, 5, Layer);
4561  }
4562  }
4563 
4564  if (Result != BUSY)
4565  { // If job done
4566 
4567  if (MoreLayers)
4568  { // More layers ?
4569 
4570  ActLayer++;
4571  if (ActLayer >= CHAIN_DEPT)
4572  { // No more layers
4573 
4574  ActLayer = 0;
4575  MoreLayers = 0;
4576  }
4577  else
4578  { // Next layer
4579 
4580  Result = BUSY;
4581  }
4582  }
4583  }
4584 
4585  if (Result == BUSY)
4586  { // Job not done - hold execution
4587 
4588  SetObjectIp(TmpIp - 1);
4590  }
4591  }
4592 #endif
4593  }
4594  break;
4595 
4596  case STOP_ALL :
4597  {
4598  Layer = *(DATA8*)PrimParPointer();
4599 
4600  if (Layer == 0)
4601  {
4602  StopLayer();
4603  }
4604 #ifndef DISABLE_DAISYCHAIN_COM_CALL
4605  else
4606  {
4607  Result = OK;
4608 
4609  if (Layer == -1)
4610  {
4611  MoreLayers = 1;
4612  Layer = ActLayer;
4613  }
4614 
4615  if (Layer == 0)
4616  {
4617  StopLayer();
4618  }
4619  else
4620  {
4621  Result = cDaisyReady();
4622 
4623  if (Result == OK)
4624  { // Ready for command
4625 
4626  DaisyBuf[0] = 0;
4627  DaisyBuf[1] = 0;
4628  DaisyBuf[2] = opINPUT_DEVICE;
4629  DaisyBuf[3] = STOP_ALL;
4630  DaisyBuf[4] = 0;
4631 
4632  Result = cDaisyDownStreamCmd(DaisyBuf, 5, Layer);
4633  }
4634  }
4635 
4636  if (Result != BUSY)
4637  { // If job done
4638 
4639  if (MoreLayers)
4640  { // More layers ?
4641 
4642  ActLayer++;
4643  if (ActLayer >= CHAIN_DEPT)
4644  { // No more layers
4645 
4646  ActLayer = 0;
4647  MoreLayers = 0;
4648  }
4649  else
4650  { // Next layer
4651 
4652  Result = BUSY;
4653  }
4654  }
4655  }
4656 
4657  if (Result == BUSY)
4658  { // Job not done - hold execution
4659 
4660  SetObjectIp(TmpIp - 1);
4662  }
4663  }
4664 #endif
4665  }
4666  break;
4667 
4668  }
4669 }
4670 
4671 
4688 void cInputRead(void)
4689 {
4690  DATA8 Type;
4691  DATA8 Mode;
4692  DATA8 Device;
4693 
4694  Device = cInputGetDevice();
4695  Type = *(DATA8*)PrimParPointer();
4696  Mode = *(DATA8*)PrimParPointer();
4697 
4698  if (Device < DEVICES)
4699  {
4700  cInputSetType(Device,Type,Mode,__LINE__);
4701  }
4702  *(DATA8*)PrimParPointer() = cInputReadDevicePct(Device,0,0,NULL);
4703 }
4704 
4705 
4722 void cInputReadSi(void)
4723 {
4724  DATA8 Type;
4725  DATA8 Mode;
4726  DATA8 Device;
4727 
4728  Device = cInputGetDevice();
4729  Type = *(DATA8*)PrimParPointer();
4730  Mode = *(DATA8*)PrimParPointer();
4731 
4732 #ifdef DEBUG_C_INPUT_DAISYCHAIN
4733  printf("c_input opINPUT_READSI: D=%-3d B=%d\r\n",Device,InputInstance.DeviceData[Device].DevStatus);
4734 #endif
4735  if (Device < DEVICES)
4736  {
4737  cInputSetType(Device,Type,Mode,__LINE__);
4738  }
4739  *(DATAF*)PrimParPointer() = cInputReadDeviceSi(Device,0,0,NULL);
4740 }
4741 
4742 
4758 void cInputTest(void)
4759 {
4760  DATA8 Busy = 1;
4761  DATA8 Device;
4762 
4763  Device = cInputGetDevice();
4764 
4765  if (Device < DEVICES)
4766  {
4767 #ifdef DEBUG_C_INPUT_DAISYCHAIN
4768  printf("c_input opINPUT_TEST: D=%-3d B=%d\r\n",Device,InputInstance.DeviceData[Device].DevStatus);
4769 #endif
4770  if (InputInstance.DeviceData[Device].DevStatus != BUSY)
4771  {
4772  Busy = 0;
4773  }
4774  }
4775  *(DATA8*)PrimParPointer() = Busy;
4776 }
4777 
4778 
4779 
4793 void cInputReady(void)
4794 {
4795  IP TmpIp;
4796  DATA8 Device;
4797 
4798  TmpIp = GetObjectIp();
4799  Device = cInputGetDevice();
4800 
4801  if (Device < DEVICES)
4802  {
4803  if (InputInstance.DeviceData[Device].DevStatus == BUSY)
4804  {
4805  SetObjectIp(TmpIp - 1);
4807  }
4808  }
4809 }
4810 
4811 
4827 void cInputWrite(void)
4828 {
4829  DATA8 Bytes;
4830  DATA8 *Data;
4831  DATA8 Device;
4832  DATA8 Tmp;
4833  IP TmpIp;
4834  DATA8 Buffer[UART_DATA_LENGTH + 1];
4835  DSPSTAT DspStat = FAILBREAK;
4836 
4837  TmpIp = GetObjectIp();
4838 
4839  Device = cInputGetDevice();
4840  Bytes = *(DATA8*)PrimParPointer();
4841  Data = (DATA8*)PrimParPointer();
4842 
4843  if (Device < INPUT_DEVICES)
4844  {
4845  if (InputInstance.DeviceType[Device] != TYPE_TERMINAL)
4846  {
4847  if (InputInstance.DeviceData[Device].Connection == CONN_INPUT_UART)
4848  {
4849  if ((Bytes > 0) && (Bytes <= UART_DATA_LENGTH))
4850  {
4851  if (((*InputInstance.pUart).Status[Device] & UART_WRITE_REQUEST))
4852  {
4853  DspStat = BUSYBREAK;
4854  }
4855  else
4856  {
4857  InputInstance.DeviceData[Device].DevStatus = BUSY;
4858 
4859  (*InputInstance.pUart).Status[Device] &= ~UART_DATA_READY;
4860 
4861  Buffer[0] = Device;
4862  for (Tmp = 0;Tmp < Bytes;Tmp++)
4863  {
4864  Buffer[Tmp + 1] = Data[Tmp];
4865  }
4866 
4867  // write setup string to "UART Device Controller" driver
4868  if (InputInstance.UartFile >= MIN_HANDLE)
4869  {
4870  write(InputInstance.UartFile,Buffer,Bytes + 1);
4871  }
4872  DspStat = NOBREAK;
4873  }
4874  }
4875  }
4876  else
4877  {
4878 
4879  if (InputInstance.DeviceData[Device].Connection == CONN_NXT_IIC)
4880  {
4881  if ((Bytes > 0) && (Bytes <= IIC_DATA_LENGTH))
4882  {
4883  if (((*InputInstance.pIic).Status[Device] & IIC_WRITE_REQUEST))
4884  {
4885  DspStat = BUSYBREAK;
4886  }
4887  else
4888  {
4889  InputInstance.DeviceData[Device].DevStatus = BUSY;
4890 
4891  (*InputInstance.pIic).Status[Device] &= ~IIC_DATA_READY;
4892 
4893  Buffer[0] = Device;
4894  for (Tmp = 0;Tmp < Bytes;Tmp++)
4895  {
4896  Buffer[Tmp + 1] = Data[Tmp];
4897  }
4898 
4899  // write setup string to "IIC Device Controller" driver
4900  if (InputInstance.IicFile >= MIN_HANDLE)
4901  {
4902  write(InputInstance.IicFile,Buffer,Bytes + 1);
4903  }
4904  DspStat = NOBREAK;
4905  }
4906  }
4907  }
4908  else
4909  { // don't bother if not UART or IIC device
4910 
4911  DspStat = NOBREAK;
4912  }
4913  }
4914  }
4915  else
4916  { // don't bother if TERMINAL
4917 
4918  DspStat = NOBREAK;
4919  }
4920  }
4921  if (DspStat == BUSYBREAK)
4922  { // Rewind IP
4923 
4924  SetObjectIp(TmpIp - 1);
4925  }
4926  SetDispatchStatus(DspStat);
4927 }
4928 
4929 
4950 void cInputReadExt(void)
4951 {
4952  DATAF Raw;
4953  DATA8 Type;
4954  DATA8 Mode;
4955  DATA8 Format;
4956  DATA8 Values;
4957  DATA8 Device;
4958  DATA8 Value;
4959 
4960  Device = cInputGetDevice();
4961  Type = *(DATA8*)PrimParPointer();
4962  Mode = *(DATA8*)PrimParPointer();
4963  Format = *(DATA8*)PrimParPointer();
4964  Values = *(DATA8*)PrimParPointer();
4965  Value = 0;
4966 
4967  if (Device < DEVICES)
4968  {
4969  cInputSetType(Device,Type,Mode,__LINE__);
4970 
4971  while ((Value < Values) && (Value < MAX_DEVICE_MODES))
4972  {
4973  switch (Format)
4974  {
4975  case DATA_PCT :
4976  {
4977  *(DATA8*)PrimParPointer() = (DATA8)cInputReadDevicePct(Device,Value,0,NULL);
4978  }
4979  break;
4980 
4981  case DATA_RAW :
4982  {
4983  Raw = cInputReadDeviceRaw(Device,Value,0,NULL);
4984  if (isnan(Raw))
4985  {
4987  }
4988  else
4989  {
4990  *(DATA32*)PrimParPointer() = (DATA32)Raw;
4991  }
4992  }
4993  break;
4994 
4995  case DATA_SI :
4996  {
4997  *(DATAF*)PrimParPointer() = (DATAF)cInputReadDeviceSi(Device,Value,0,NULL);
4998  }
4999  break;
5000 
5001  default :
5002  {
5004  }
5005  break;
5006 
5007  }
5008  Value++;
5009  }
5010  }
5011  while (Value < Values)
5012  {
5013  switch (Format)
5014  {
5015  case DATA_PCT :
5016  {
5018  }
5019  break;
5020 
5021  case DATA_RAW :
5022  {
5024  }
5025  break;
5026 
5027  case DATA_SI :
5028  {
5030  }
5031  break;
5032 
5033  default :
5034  {
5036  }
5037  break;
5038 
5039  }
5040  Value++;
5041  }
5042 }
5043 
5044 
5065 void cInputSample(void)
5066 {
5067  DATA32 SampleTime;
5068  DATA32 Data32;
5069  DATA16 NoOfPorts;
5070  DATA16 *pInits;
5071  DATA8 *pDevices;
5072  DATA8 *pTypes;
5073  DATA8 *pModes;
5074  DATA8 *pDataSets;
5075  DATAF *pValues;
5076  DATA16 Index;
5077  DATA8 Device;
5078  DATA8 Type;
5079  DATA8 Mode;
5080 
5081 
5082  SampleTime = *(DATA32*)PrimParPointer();
5083  NoOfPorts = *(DATA16*)PrimParPointer();
5084  pInits = (DATA16*)PrimParPointer();
5085  pDevices = (DATA8*)PrimParPointer();
5086  pTypes = (DATA8*)PrimParPointer();
5087  pModes = (DATA8*)PrimParPointer();
5088  pDataSets = (DATA8*)PrimParPointer();
5089  pValues = (DATAF*)PrimParPointer();
5090  if (VMInstance.Handle >= 0)
5091  {
5092  Data32 = (DATA32)NoOfPorts;
5093  if (Data32 > MIN_ARRAY_ELEMENTS)
5094  {
5095  pValues = (DATAF*)VmMemoryResize(VMInstance.Handle,Data32);
5096  }
5097  }
5098  if (pValues != NULL)
5099  {
5100  for (Index = 0;Index < NoOfPorts;Index++)
5101  { // Service all devices
5102 
5103  Device = pDevices[Index];
5104 
5105  if (Device >= INPUTS)
5106  {
5107  Device += 12;
5108  }
5109 
5110  pValues[Index] = DATAF_NAN;
5111 
5112  if ((Device >= 0) && (Device < DEVICES))
5113  {
5114  Type = pTypes[Index];
5115  Mode = pModes[Index];
5116 
5117  cInputSetType(Device,Type,Mode,__LINE__);
5118 
5119  pValues[Index] = cInputReadDeviceSi(Device,pDataSets[Index],SampleTime,&pInits[Index]);
5120  }
5121  }
5122  }
5123 }
5124 
5125 
5126 //*****************************************************************************
void ClrLayer(void)
Definition: c_input.c:390
DATAF PctMin
Percent minimum value (e.c. -100.0)
Definition: lms2012.h:787
RESULT cDaisyDownStreamCmd(DATA8 *pData, DATA8 Length, DATA8 Layer)
Definition: c_daisy.c:665
#define DATA32_NAN
Definition: bytecodes.h:1494
DSPSTAT ExecuteByteCode(IP pByteCode, GP pGlobals, LP pLocals)
Execute byte code stream (C-call)
Definition: lms2012.c:535
RESULT cInputGetDeviceData(DATA8 Layer, DATA8 Port, DATA8 Length, DATA8 *pType, DATA8 *pMode, DATA8 *pData)
Definition: c_input.c:1935
void SetDispatchStatus(DSPSTAT DspStat)
Set object (dispatch) status.
Definition: lms2012.c:256
DATA8 cInputReadDevicePct(DATA8 Device, DATA8 Index, DATA16 Time, DATA16 *pInit)
Definition: c_input.c:2742
RESULT Result
Definition: lms2012.h:1137
void cInputDeviceList(void)
opINPUT_DEVICE_LIST byte code
Definition: c_input.c:3678
RESULT cInputComSetDeviceInfo(DATA8 Length, UBYTE *pInfo)
Definition: c_input.c:1784
#define TYPE_NAME_LENGTH
Definition: lms2012.h:768
UART * pUart
Definition: c_input.h:127
DATA8 Changed[INPUTS][MAX_DEVICE_MODES]
Definition: d_uart_mod.c:1953
DATA8 Format
Definition: lms2012.h:781
#define INPUT_DEVICES
Definition: c_input.h:28
#define snprintf
Definition: c_input.c:141
#define UART_DATA_READY
Data is ready.
Definition: lms2012.h:1054
UWORD IdValue
Device id value (e.c. 0 ~ UART)
Definition: lms2012.h:792
DATA8 Type
Device type.
Definition: lms2012.h:777
RESULT cInputFindDevice(DATA8 Type, DATA8 Mode, UWORD *pTypeIndex)
Definition: c_input.c:988
IMGDATA CLR_LAYER_CLR_CHANGES[]
Definition: c_input.c:383
DATA8 InUse
Definition: c_input.h:107
signed char SBYTE
Basic Type used to symbolise 8 bit signed values.
Definition: lmstypes.h:33
#define EXT_CONFIG
rudolf configuration file
Definition: lms2012.h:229
SBYTE Name[TYPE_NAME_LENGTH+1]
Device name.
Definition: lms2012.h:776
DATAF Raw[MAX_DEVICE_DATASETS]
Raw value (only updated when "cInputReadDeviceRaw" function is called)
Definition: c_input.h:91
void cInputWriteDeviceRaw(DATA8 Device, DATA8 Connection, DATA8 Type, DATAF DataF)
Definition: c_input.c:2636
Break because of fail.
Definition: lms2012.h:674
IICDAT IicDat
Definition: c_input.h:140
DATA16 TypeDataIndex
Definition: c_input.h:165
SWORD DATA16
VM Type for 2 byte signed value.
Definition: lmstypes.h:62
DATA8 cInputGetDevice(void)
Definition: c_input.c:3628
void LogErrorNumber(ERR Err)
Definition: lms2012.c:445
DATA8 ConfigurationChanged[MAX_PROGRAMS]
Definition: c_input.h:146
DATA32 Changes
Definition: c_input.h:94
DATA8 Repeat
Definition: lms2012.h:1139
SLONG DATA32
VM Type for 4 byte signed value.
Definition: lmstypes.h:63
DATA16 Time
Definition: lms2012.h:1140
RESULT cInputClose(void)
Definition: c_input.c:3574
DATA8 RdLng
Definition: lms2012.h:1143
#define OUTPUT_PORTS
Definition: c_input.h:30
RESULT cInputComSetDeviceType(DATA8 Layer, DATA8 Port, DATA8 Type, DATA8 Mode)
Definition: c_input.c:1855
void cInputDevice(void)
opINPUT_DEVICE byte code
Definition: c_input.c:3887
TYPES TypeData
Definition: lms2012.h:1069
RESULT cInputStartTypeDataUpload(void)
Definition: c_input.c:3243
DATA8 DCMUpdate
Definition: c_input.h:154
RESULT cInputGetIicString(DATA8 Type, DATA8 Mode, IICSTR *IicStr)
Definition: c_input.c:539
#define IIC_SETUP
Definition: lms2012.h:1168
DATA16 Time
Definition: lms2012.h:1152
UWORD OBJID
Object id type.
Definition: lmstypes.h:73
void cInputSetType(DATA8 Device, DATA8 Type, DATA8 Mode, int Line)
Definition: c_input.c:3640
void SetObjectIp(IP Ip)
Set current instruction pointer.
Definition: lms2012.c:309
#define DATA16_MAX
Definition: bytecodes.h:1500
DATA8 Connection[INPUTS]
Definition: lms2012.h:1060
RESULT DevStatus
Definition: c_input.h:89
Port empty or not available.
Definition: lms2012.h:585
void cInputReadExt(void)
opINPUT_READEXT byte code
Definition: c_input.c:4950
IMGDATA CLR_LAYER_OUTPUT_RESET[]
Definition: c_input.c:385
UWORD TypeIndex
Index to information in "TypeData" table.
Definition: c_input.h:86
DATA8 Port
Definition: lms2012.h:1070
DATA8 Mode
Device mode.
Definition: lms2012.h:779
DATA8 Decimals
Definition: lms2012.h:783
void cInputDcmUpdate(UWORD Time)
Update Device Types.
Definition: c_input.c:3000
RESULT cComSetDeviceType(DATA8 Layer, DATA8 Port, DATA8 Type, DATA8 Mode)
Definition: c_com.c:262
IMGDATA CLR_LAYER_INPUT_WRITE[]
Definition: c_input.c:387
RESULT cInputInsertTypeData(char *pFilename)
Definition: c_input.c:636
void * PrimParPointer(void)
Get next encoded parameter from byte code stream.
Definition: lms2012.c:694
DATA8 Figures
Definition: lms2012.h:782
#define DATA8_NAN
Definition: bytecodes.h:1492
void cInputRead(void)
opINPUT_READ byte code
Definition: c_input.c:4688
DATA8 DeviceType[DEVICES]
Type of all devices - for easy upload.
Definition: c_input.h:148
IP GetObjectIp(void)
Get current instruction pointer.
Definition: lms2012.c:298
#define IIC_READ_TYPE_INFO
Definition: lms2012.h:1167
UWORD MaxDeviceTypes
Number of device type/mode entries in tabel.
Definition: c_input.h:158
void cInputWrite(void)
opINPUT_WRITE byte code
Definition: c_input.c:4827
RESULT cInputExit(void)
Definition: c_input.c:3584
UWORD NoneIndex
Definition: c_input.h:152
RESULT cInputSetupDevice(DATA8 Device, DATA8 Repeat, DATA16 Time, DATA8 WrLng, DATA8 *pWrData, DATA8 RdLng, DATA8 *pRdData)
Definition: c_input.c:821
void SetInstructions(ULONG Instructions)
Set instructions.
Definition: lms2012.c:273
IICSTR * IicString
Definition: c_input.h:161
DATA8 Type[INPUTS]
Definition: lms2012.h:1061
RESULT cMemoryMalloc(void **ppMemory, DATA32 Size)
Definition: c_memory.c:140
void cInputCalDataInit(void)
Definition: c_input.c:1254
RESULT cInputComGetDeviceInfo(DATA8 Length, UBYTE *pInfo)
Definition: c_input.c:1830
INPUT_GLOBALS * getInputInstance()
Definition: c_input.c:152
RESULT cInputGetData(DATA8 Layer, DATA8 Port, DATA16 Time, DATA16 *pInit, DATA8 Length, DATA8 *pType, DATA8 *pMode, DATA8 *pData)
Definition: c_input.c:1963
OBJID CallingObjectId(void)
Get calling object id.
Definition: lms2012.c:196
RESULT cInputOpen(void)
Definition: c_input.c:3564
DATAF SiMin
SI unit minimum value (e.c. -100.0)
Definition: lms2012.h:789
IMGDATA CLR_LAYER_CLR_BUMBED[]
Definition: c_input.c:384
Port not empty but type has not been determined.
Definition: lms2012.h:584
UWORD InvalidTime
mS from type change to valid data
Definition: c_input.h:84
DATA8 Busy
Definition: c_input.h:90
IICSTR IicStr
Definition: c_input.h:162
IMGDATA * IP
Instruction pointer type.
Definition: lmstypes.h:74
Definition: lms2012.h:774
RESULT cMemoryRealloc(void *pOldMemory, void **ppMemory, DATA32 Size)
Definition: c_memory.c:160
SBYTE Symbol[SYMBOL_LENGTH+1]
SI unit symbol.
Definition: lms2012.h:794
ULONG PollString
Definition: lms2012.h:1160
DATAF RawMax
Raw maximum value (e.c. 1023.0)
Definition: lms2012.h:786
DATA8 Pins
Device pin setup.
Definition: lms2012.h:793
DATAF SiMax
SI unit maximum value (e.c. 100.0)
Definition: lms2012.h:790
unsigned int ULONG
Basic Type used to symbolise 32 bit unsigned values.
Definition: lmstypes.h:31
RESULT cComGetDeviceInfo(DATA8 Length, UBYTE *pInfo)
Definition: c_com.c:256
void cInputReady(void)
opINPUT_READY byte code
Definition: c_input.c:4793
CALIB Calib[MAX_DEVICE_TYPE][MAX_DEVICE_MODES]
Definition: c_input.h:168
IMGDATA CLR_LAYER_OUTPUT_CLR_COUNT[]
Definition: c_input.c:386
RESULT cInputCheckIicInfo(UBYTE Port)
Definition: c_input.c:2925
TYPES * TypeData
Type specific data.
Definition: c_input.h:159
UWORD IicDeviceTypes
Number of IIC device type/mode entries in tabel.
Definition: c_input.h:160
TYPES TypeDefault[]
Definition: c_input.c:163
UBYTE Data[MAX_DEVICE_DATALENGTH]
Definition: c_input.h:121
#define MAX_DEVICE_INFOLENGTH
Number of bytes in the structure above (can not be changed)
Definition: lms2012.h:800
void cInputCalibrateColor(COLORSTRUCT *pC, UWORD *pNewVals)
Definition: c_input.c:1344
RESULT cInputExpandDevice(DATA8 Device, DATA8 *pLayer, DATA8 *pPort, DATA8 *pOutput)
Definition: c_input.c:417
#define UART_CLEAR_CHANGED
Definition: lms2012.h:1078
void cInputUpdate(UWORD Time)
Definition: c_input.c:3356
#define SYMBOL_LENGTH
Symbol leng th (not including zero)
Definition: lms2012.h:769
#define COLORSENSORBGMIN
Definition: c_input.c:1315
#define CtoV(C)
Definition: lms2012.h:550
DATAF cInputCalculateColor(COLORSTRUCT *pC)
Definition: c_input.c:1388
#define UART_NACK_MODE_INFO
Definition: lms2012.h:1077
DATA8 Connection
Definition: lms2012.h:778
#define TRUE
Definition: c_input.c:1306
Port not empty and type is invalid.
Definition: lms2012.h:586
void setInputInstance(INPUT_GLOBALS *_Instance)
Definition: c_input.c:147
IMGDATA STOP_LAYER[]
Definition: c_input.c:400
#define DATAF_NAN
Definition: bytecodes.h:1495
UARTCTL UartCtl
Definition: c_input.h:138
DATA8 Mode[INPUTS]
Definition: lms2012.h:1062
RESULT cComSetDeviceInfo(DATA8 Length, UBYTE *pInfo)
Definition: c_com.c:249
DATA8 Mode
Definition: lms2012.h:1154
#define UART_READ_MODE_INFO
Definition: lms2012.h:1076
RESULT cInputGetNewTypeDataPointer(SBYTE *pName, DATA8 Type, DATA8 Mode, DATA8 Connection, TYPES **ppPlace)
Definition: c_input.c:581
#define IIC_DATA_READY
Data is ready.
Definition: lms2012.h:1122
DATA8 Views
Definition: lms2012.h:784
DEVICE DeviceData[DEVICES]
Data for all devices.
Definition: c_input.h:150
Device is NXT IIC sensor.
Definition: lms2012.h:582
#define IIC_WRITE_REQUEST
Write request.
Definition: lms2012.h:1123
void cInputTest(void)
opINPUT_TEST byte code
Definition: c_input.c:4758
UWORD UnknownIndex
Definition: c_input.h:153
DATA8 DataSets
Definition: lms2012.h:780
ANALOG Analog
Definition: c_input.h:123
#define UART_WRITE_REQUEST
Write request.
Definition: lms2012.h:1055
ULONG SetupString
Definition: lms2012.h:1158
DATA8 PollLng
Definition: lms2012.h:1159
DATA8 WrData[IIC_DATA_LENGTH]
Definition: lms2012.h:1142
DATA8 RdData[IIC_DATA_LENGTH]
Definition: lms2012.h:1144
unsigned char UBYTE
Basic Type used to symbolise 8 bit unsigned values.
Definition: lmstypes.h:29
Port is connected to a terminal.
Definition: lms2012.h:583
DATA8 Type
Definition: lms2012.h:1153
RESULT cComGetDeviceData(DATA8 Layer, DATA8 Port, DATA8 Length, DATA8 *pType, DATA8 *pMode, DATA8 *pData)
Definition: c_com.c:268
#define UART_SET_CONN
Definition: lms2012.h:1075
HANDLER Handle
Definition: lms2012.h:1474
DATA8 SensorType[IIC_NAME_LENGTH+1]
Definition: lms2012.h:1156
DATA8 ReadLng
Definition: lms2012.h:1161
FLOAT DATAF
VM Type for 4 byte floating point value.
Definition: lmstypes.h:64
void cInputTypeDataInit(void)
Definition: c_input.c:762
DATA16 TypeDataTimer
Definition: c_input.h:164
unsigned short UWORD
Basic Type used to symbolise 16 bit unsigned values.
Definition: lmstypes.h:30
RESULT cInputFindDumbOutputDevice(DATA8 Device, DATA8 Type, DATA8 Mode, UWORD *pTypeIndex)
Definition: c_input.c:948
#define LFILE
Definition: lmstypes.h:39
UBYTE IMGDATA
Image base type.
Definition: lmstypes.h:69
char Buffer[1024]
Definition: c_wifi.c:102
INPUT_GLOBALS * gInputInstance
Definition: c_input.c:145
DATA8 Port
Definition: lms2012.h:1138
DATA32 Bumps
Definition: c_input.h:95
RESULT cInputCompressDevice(DATA8 *pDevice, UBYTE Layer, UBYTE Port)
Definition: c_input.c:448
#define OUTPUTS
Number of output ports in the system.
Definition: lms2012.h:191
#define DataF
DATAF RawMin
Raw minimum value (e.c. 0.0)
Definition: lms2012.h:785
#define DATA16_NAN
Definition: bytecodes.h:1493
#define FALSE
Definition: c_input.c:1305
OBJID Owner
Definition: c_input.h:88
DATA8 TypeModes[MAX_DEVICE_TYPE+1]
No of modes for specific type.
Definition: c_input.h:156
UWORD InvalidTime
mS from type change to valid data
Definition: lms2012.h:791
PRGID CurrentProgramId(void)
Get current program id.
Definition: lms2012.c:207
#define TESTDEVICE
Definition: lms2012.h:175
RESULT cInputFindDumbInputDevice(DATA8 Device, DATA8 Type, DATA8 Mode, UWORD *pTypeIndex)
Definition: c_input.c:895
DATAF Min
Definition: c_input.h:108
#define SENSOR_RESOLUTION
Definition: c_input.c:1308
DATAF cInputReadDeviceRaw(DATA8 Device, DATA8 Index, DATA16 Time, DATA16 *pInit)
Definition: c_input.c:2407
DEVCON DevCon
Definition: c_input.h:137
void cInputReadSi(void)
opINPUT_READSI byte code
Definition: c_input.c:4722
void cOutputSetTypes(char *pTypes)
Definition: c_output.c:282
void * VmMemoryResize(HANDLER Handle, DATA32 Elements)
Definition: lms2012.c:366
ANALOG * pAnalog
Definition: c_input.h:124
DATAF cInputReadDeviceSi(DATA8 Device, DATA8 Index, DATA16 Time, DATA16 *pInit)
Definition: c_input.c:2790
#define IIC_SET
Definition: lms2012.h:1169
UWORD PRGID
Program id type.
Definition: lmstypes.h:71
DATA8 Port
Definition: lms2012.h:1151
DATA8 Manufacturer[IIC_NAME_LENGTH+1]
Definition: lms2012.h:1155
void cInputSample(void)
opINPUT_SAMPLE byte code
Definition: c_input.c:5065
DATAF OldRaw
Definition: c_input.h:93
#define DEVICES
Definition: c_input.h:33
RESULT cInputGetColor(DATA8 Device, DATA8 *pData)
Definition: c_input.c:1537
void cInputSetDeviceType(DATA8 Device, DATA8 Type, DATA8 Mode, int Line)
Definition: c_input.c:1041
DATA8 Connection
Connection type (from DCM)
Definition: c_input.h:87
RESULT cDaisyReady(void)
Definition: c_daisy.c:531
SBYTE DATA8
VM Type for 1 byte signed value.
Definition: lmstypes.h:61
DATA8 WrLng
Definition: lms2012.h:1141
DATA8 SetupLng
Definition: lms2012.h:1157
#define OutputInstance
Definition: c_output.h:78
int UartFile
Definition: c_input.h:132
#define IIC_SET_CONN
Definition: lms2012.h:1166
DATA8 Mode
Definition: lms2012.h:1071
DATA8 TmpMode[INPUT_PORTS]
Definition: c_input.h:144
#define InputInstance
Definition: c_input.h:174
DATA8 DeviceMode[DEVICES]
Mode of all devices.
Definition: c_input.h:149
#define UART_PORT_CHANGED
Input port changed.
Definition: lms2012.h:1053
DSPSTAT
Definition: lms2012.h:665
#define INPUTS
Number of input ports in the system.
Definition: lms2012.h:192
GLOBALS VMInstance
Definition: lms2012.c:131
DATAF Max
Definition: c_input.h:109
#define TYPE_PARAMETERS
Number of members in the structure above.
Definition: lms2012.h:799
Break because of waiting for completion.
Definition: lms2012.h:671
void StopLayer(void)
Definition: c_input.c:402
RESULT cInputComGetDeviceType(DATA8 Layer, DATA8 Port, DATA8 Length, DATA8 *pType, DATA8 *pMode, DATA8 *pData)
Definition: c_input.c:1920
void cInputCalDataExit(void)
Definition: c_input.c:1288
Dispatcher running (looping)
Definition: lms2012.h:667
#define CHAIN_DEPT
Number of bricks in the USB daisy chain (master + slaves)
Definition: lms2012.h:206
void Memcpy(void *pDest, const void *pSrc, size_t No)
Definition: c_input.c:338
DATAF PctMax
Percent maximum value (e.c. 100.0)
Definition: lms2012.h:788
#define COLORSENSORBGPCTDYN
Definition: c_input.c:1319
RESULT cInputInit(void)
Definition: c_input.c:3415
RESULT cInputComGetDeviceData(DATA8 Layer, DATA8 Port, DATA8 Length, DATA8 *pType, DATA8 *pMode, DATA8 *pData)
Definition: c_input.c:1887
IIC * pIic
Definition: c_input.h:130
RESULT cInputInsertNewIicString(DATA8 Type, DATA8 Mode, DATA8 *pManufacturer, DATA8 *pSensorType, DATA8 SetupLng, ULONG SetupString, DATA8 PollLng, ULONG PollString, DATA8 ReadLng)
Definition: c_input.c:476
RESULT cInputCheckUartInfo(UBYTE Port)
Definition: c_input.c:2845
void cInputCalcFullScale(UWORD *pRawVal, UWORD ZeroPointOffset, UBYTE PctFullScale, UBYTE InvStatus)
Definition: c_input.c:1321
#define INPUT_PORTS
Definition: c_input.h:27
void cInputResetDevice(DATA8 Device, UWORD TypeIndex)
Definition: c_input.c:1017
UWORD TimeoutTimer
mS allowed to be busy timer
Definition: c_input.h:85
RESULT cInputSetChainedDeviceType(DATA8 Layer, DATA8 Port, DATA8 Type, DATA8 Mode)
Definition: c_input.c:1752
#define NULL