//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "threadsearch.h"

extern int bits[17];

unsigned char bus_idents[MAX_SWITCHES + 1][8];	// Identification numbers found on the bus
int max_bus_devices;                            // Maximum number devices found on the bus
int Sequence[MAX_SWITCHES + 1];                 // Who pressed first in one search-cycle
unsigned int nieuw;                             // Are there buttons pressed which where notpressed
unsigned int mask,NewFound;

extern unsigned char switch_id[MAX_SWITCHES + 1][8];
extern int SetUp;
extern int StopThread, ThreadStop;

unsigned int read_switches();
int search_bus();

extern unsigned char FindDevices( void );
extern unsigned char * RomData( void );

#pragma package(smart_init)
__fastcall SearchThread::SearchThread(bool CreateSuspended)
    : TThread(CreateSuspended)
{
    Priority = tpIdle;
}
//---------------------------------------------------------------------------
void __fastcall SearchThread::Execute()
{
    while(!StopThread)
//    while(!Terminated)
    {
        if(!NewFound)
        {
 		    nieuw = read_switches();
            if(nieuw & ~mask)     								/* Read the serial switches */
                NewFound = true;
        }
    }
    ThreadStop = true;
}
//---------------------------------------------------------------------------
unsigned int read_switches()
{
	int i,j;
	unsigned int switch_pressed = 0;

	nieuw = 0;

	if(search_bus())
    {
	    for(i=1;i<=max_bus_devices;i++)
	    {
		    if(bus_idents[i][0] != 0)
		    {
                if(!SetUp)
                {
			        for(j=1;j<=MAX_SWITCHES;j++)
			        {
				        if((bus_idents[i][1] == switch_id[j][1]) &&
					       (bus_idents[i][2] == switch_id[j][2]) &&
					       (bus_idents[i][3] == switch_id[j][3]) &&
					       (bus_idents[i][4] == switch_id[j][4]) &&
					       (bus_idents[i][5] == switch_id[j][5]) &&
					       (bus_idents[i][6] == switch_id[j][6]) &&
					       (bus_idents[i][7] == switch_id[j][7]))
				        {
                            if( !(switch_pressed & bits[j]))
                            {
					            switch_pressed |= bits[j];
                                Sequence[i] = j;
                            }
				        }
                    }
                }
                else
                {
                    NewFound = true;
                }
			}
		}
	}
	return switch_pressed;
}

/* look for a device on the bus */
int search_bus()
{
	int finding = 1,i;
	unsigned char *ROM;

	max_bus_devices = 0; 	   	// Teller for serial numbers

	while(finding)
	{
		if(FindDevices())
		{                       // Another device found
			ROM = RomData();
			if(ROM[0] != 0)
			{
				max_bus_devices++;
				for (i = 0; i <= 7; i++)
				{
					bus_idents[max_bus_devices][i] = ROM[i];
				}
			}
		}
		else
		{
			finding = 0;
		}
	}

	return(max_bus_devices);
}

