Exemplo n.º 1
0
bool TestSuite::TestBTree1()
{
	printf("\n Running TestBTree Case1...") ;

	{
		BTree tree ;
		BTree::DestroyKeyValue d ;
		tree.SetDestoryKeyValueCallBack(&d) ;
		int k[102], v[102] ;

		for(int i = 0; i < 102; i++) 
		{
			TestKey* pKey = new TestKey(i) ;
			TestValue* pValue = new TestValue(i * 10) ;

			k[i] = i ;
			v[i] = i * 10 ;

			ASSERT(tree.Insert(pKey, pValue), ==, true) ;
		}

		int key = 63 ;
		TestValue* pValue = (TestValue*)tree.Find(TestKey(key)) ;
		ASSERT(pValue, !=, NULL) ;
		ASSERT(pValue->m_value, ==, 630) ;

		TestInOrderVisitor t(102, k, v) ;
		tree.InOrderTraverse(t) ;
	}

	return true ;
}
Exemplo n.º 2
0
// ReadKey():
//  Finds and reads a FreeKey license, returning false if the
//   key cannot be found.  Returns a pointer to the matching
//   key if the found; otherwise, it returns NULL.
const char * FreeKeyDongle::ReadKey( const char *path ) {
  char filename[ MAX_PATH_LENGTH ];

  if( path == NULL ) {
    // Get the Plug-in's Path
    #ifdef WIN32
      GetModuleFileName( hinst, filename, MAX_PATH_LENGTH );
    #endif
    DirStrings::ChangeFile( filename, "freekey.key" );
  } else
    strcpy( filename, path );

  if( !DirInfo::Exists( filename ) )
    return NULL;

  pifstream in( filename );
  if( !in )
    return NULL;

  in.GuessEOLType();
  static char buffer[ 64 ];
  do {
    in.getline( buffer, 64 );
    if( TestKey( buffer, GetDongleID() ) )
      return buffer;
  } while( !in.eof() );

  return NULL;
}
Exemplo n.º 3
0
void WaitEnter()
{
	while(1)
	{
		HandlePS2RawCodes();
		if(TestKey(KEY_ENTER)&2)
			return;
	}
}
Exemplo n.º 4
0
bool CKey::EventProcess(const Event &event)
{
    if (m_state & STATE_DEAD)
        return true;

    CControl::EventProcess(event);

    if (event.type == EVENT_MOUSE_BUTTON_DOWN)
    {
        if (event.mouseButton.button == MOUSE_BUTTON_LEFT) // left
            m_catch = Detect(event.mousePos);
    }

    if (event.type == EVENT_KEY_DOWN && m_catch)
    {
        m_catch = false;

        if (TestKey(event.key.key)) // impossible ?
        {
            m_sound->Play(SOUND_TZOING);
        }
        else
        {
            if (event.key.key == m_binding.primary || event.key.key == m_binding.secondary)
            {
                m_binding.secondary = KEY_INVALID;
                m_binding.primary = event.key.key;
            }
            else
            {
                m_binding.secondary = m_binding.primary;
                m_binding.primary = event.key.key;
            }
            m_sound->Play(SOUND_CLICK);

            Event newEvent = event;
            newEvent.type = m_eventType;
            m_event->AddEvent(newEvent);
        }
        return false;
    }

    return true;
}
Exemplo n.º 5
0
int main(void)
{
  uint16_t          U_Bat;         /* voltage of power supply */
  uint8_t           Test;          /* test value */

  /*
   *  init
   */

  /* switch on power to keep me alive */
  CONTROL_DDR = (1 << POWER_CTRL);      /* set pin as output */
  CONTROL_PORT = (1 << POWER_CTRL);     /* set pin to drive power management transistor */

  /* setup MCU */
  MCUCR = (1 << PUD);                        /* disable pull-up resistors globally */
  ADCSRA = (1 << ADEN) | ADC_CLOCK_DIV;      /* enable ADC and set clock divider */

  #ifdef HW_RELAY
  /* init relay (safe mode) */
                                      /* ADC_PORT should be 0 */
  ADC_DDR = (1 << TP_REF);            /* short circuit probes */
  #endif

  /* catch watchdog */  
  Test = (MCUSR & (1 << WDRF));         /* save watchdog flag */
  MCUSR &= ~(1 << WDRF);                /* reset watchdog flag */
  wdt_disable();                        /* disable watchdog */

  /* init LCD module */
  LCD_BusSetup();                       /* setup bus */
  LCD_Init();                           /* initialize LCD */
  LCD_NextLine_Mode(MODE_NONE);         /* reset line mode */


  /*
   *  watchdog was triggered (timeout 2s)
   *  - This is after the MCU done a reset driven by the watchdog.
   *  - Does only work if the capacitor at the base of the power management
   *    transistor is large enough to survive a MCU reset. Otherwise the
   *    tester simply loses power.
   */

  if (Test)
  {
    LCD_Clear();                        /* display was initialized before */
    LCD_EEString(Timeout_str);          /* display: timeout */
    LCD_NextLine_EEString(Error_str);   /* display: error */
    MilliSleep(2000);                   /* give user some time to read */
    CONTROL_PORT = 0;                   /* power off myself */
    return 0;                           /* exit program */
  }


  /*
   *  operation mode selection
   */

  Config.SleepMode = SLEEP_MODE_PWR_SAVE;    /* default: power save */
  UI.TesterMode = MODE_CONTINOUS;            /* set default mode: continous */
  Test = 0;                                  /* key press */

  /* catch long key press */
  if (!(CONTROL_PIN & (1 << TEST_BUTTON)))   /* if test button is pressed */
  {
    RunsMissed = 0;

    while (Test == 0)
    {
      MilliSleep(20);
      if (!(CONTROL_PIN & (1 << TEST_BUTTON)))    /* if button is still pressed */
      {
        RunsMissed++;
        if (RunsMissed > 100) Test = 3;      /* >2000ms */
      }
      else                                        /* button released */
      {
        Test = 1;                            /* <300ms */
        if (RunsMissed > 15) Test = 2;       /* >300ms */
      }
    }
  }

  /* key press >300ms sets autohold mode */
  if (Test > 1) UI.TesterMode = MODE_AUTOHOLD;


  /*
   *  load saved offsets and values
   */

  if (Test == 3)              /* key press >2s resets to defaults */
  {
    SetAdjustDefaults();           /* set default values */
  }
  else                        /* normal mode */
  {
    LoadAdjust();                  /* load adjustment values */
  }

  /* set extra stuff */
  #ifdef SW_CONTRAST
    LCD_Contrast(NV.Contrast);          /* set LCD contrast */
  #endif


  /*
   *  welcome user
   */

  LCD_EEString(Tester_str);             /* display: Component Tester */
  LCD_NextLine_EEString_Space(Edition_str);   /* display firmware edition */
  LCD_EEString(Version_str);            /* display firmware version */
  MilliSleep(1500);                     /* let the user read the display */


  /*
   *  init variables
   */

  /* cycling */
  RunsMissed = 0;
  RunsPassed = 0;

  /* default offsets and values */
  Config.Samples = ADC_SAMPLES;         /* number of ADC samples */
  Config.AutoScale = 1;                 /* enable ADC auto scaling */
  Config.RefFlag = 1;                   /* no ADC reference set yet */
  Config.Vcc = UREF_VCC;                /* voltage of Vcc */
  wdt_enable(WDTO_2S);		        /* enable watchdog (timeout 2s) */


  /*
   *  main processing cycle
   */

start:

  /* reset variabels */
  Check.Found = COMP_NONE;
  Check.Type = 0;
  Check.Done = 0;
  Check.Diodes = 0;
  Check.Resistors = 0;
  Semi.U_1 = 0;
  Semi.I_1 = 0;
  Semi.F_1 = 0;

  /* reset hardware */
  ADC_DDR = 0;                     /* set all pins of ADC port as input */
                                   /* also remove short circuit by relay */
  LCD_NextLine_Mode(MODE_KEEP);    /* line mode: keep first line */
  LCD_Clear();                     /* clear LCD */


  /*
   *  voltage reference
   */

  #ifdef HW_REF25
  /* external 2.5V reference */
  Config.Samples = 200;            /* do a lot of samples for high accuracy */
  U_Bat = ReadU(TP_REF);           /* read voltage of reference (mV) */
  Config.Samples = ADC_SAMPLES;    /* set samples back to default */

  if ((U_Bat > 2250) && (U_Bat < 2750))   /* check for valid reference */
  {
    uint32_t        Temp;

    /* adjust Vcc (assuming 2.495V typically) */
    Temp = ((uint32_t)Config.Vcc * UREF_25) / U_Bat;
    Config.Vcc = (uint16_t)Temp;
  }
  #endif

  /* internal bandgap reference */
  Config.Bandgap = ReadU(0x0e);         /* dummy read for bandgap stabilization */
  Config.Samples = 200;                 /* do a lot of samples for high accuracy */
  Config.Bandgap = ReadU(0x0e);         /* get voltage of bandgap reference (mV) */
  Config.Samples = ADC_SAMPLES;         /* set samples back to default */
  Config.Bandgap += NV.RefOffset;       /* add voltage offset */ 


  /*
   *  battery check
   */

  /*
   *  ADC pin is connected to a voltage divider Rh = 10k and Rl = 3k3.
   *  Ul = (Uin / (Rh + Rl)) * Rl  ->  Uin = (Ul * (Rh + Rl)) / Rl
   *  Uin = (Ul * (10k + 3k3)) / 3k3 = 4 * Ul  
   */

  /* get current voltage */
  U_Bat = ReadU(TP_BAT);                /* read voltage (mV) */
  U_Bat *= 4;                           /* calculate U_bat (mV) */
  U_Bat += BAT_OFFSET;                  /* add offset for voltage drop */

  /* display battery voltage */
  LCD_EEString_Space(Battery_str);      /* display: Bat. */
  DisplayValue(U_Bat / 10, -2, 'V');    /* display battery voltage */
  LCD_Space();

  /* check limits */
  if (U_Bat < BAT_POOR)                 /* low level reached */
  {
    LCD_EEString(Low_str);              /* display: low */
    MilliSleep(2000);                   /* let user read info */
    goto power_off;                     /* power off */
  }
  else if (U_Bat < BAT_POOR + 1000)     /* warning level reached */
  {
    LCD_EEString(Weak_str);             /* display: weak */
  }
  else                                  /* ok */
  {
    LCD_EEString(OK_str);               /* display: ok */
  }


  /*
   *  probing
   */

  /* display start of probing */
  LCD_NextLine_EEString(Running_str);   /* display: probing... */

  /* try to discharge any connected component */
  DischargeProbes();
  if (Check.Found == COMP_ERROR)   /* discharge failed */
  {
    goto result;                   /* skip all other checks */
  }

  /* enter main menu if requested by short-circuiting all probes */
  if (AllProbesShorted() == 3)
  {
    MainMenu();                    /* enter mainmenu */;
    goto end;                      /* new cycle after job is is done */
  }

  /* check all 6 combinations of the 3 probes */ 
  CheckProbes(TP1, TP2, TP3);
  CheckProbes(TP2, TP1, TP3);
  CheckProbes(TP1, TP3, TP2);
  CheckProbes(TP3, TP1, TP2);
  CheckProbes(TP2, TP3, TP1);
  CheckProbes(TP3, TP2, TP1);

  /* if component might be a capacitor */
  if ((Check.Found == COMP_NONE) ||
      (Check.Found == COMP_RESISTOR))
  {
    /* tell user to be patient with large caps :-) */
    LCD_Space();
    LCD_Char('C');    

    /* check all possible combinations */
    MeasureCap(TP3, TP1, 0);
    MeasureCap(TP3, TP2, 1);
    MeasureCap(TP2, TP1, 2);
  }


  /*
   *  output test results
   */

result:

  LCD_Clear();                     /* clear LCD */
  LCD_NextLine_Mode(MODE_KEEP | MODE_KEY);

  /* call output function based on component type */
  switch (Check.Found)
  {
    case COMP_ERROR:
      Show_Error();
      goto end;
      break;

    case COMP_DIODE:
      Show_Diode();
      break;

    case COMP_BJT:
      Show_BJT();
      break;

    case COMP_FET:
      Show_FET();
      break;

    case COMP_IGBT:
      Show_IGBT();
      break;

    case COMP_THYRISTOR:
      Show_Special();
      break;

    case COMP_TRIAC:
      Show_Special();
      break;

    case COMP_RESISTOR:
      Show_Resistor();
      break;

    case COMP_CAPACITOR:
      Show_Capacitor();
      break;

    default:                  /* no component found */
      Show_Fail();
      goto end;
  }

  /* component was found */
  RunsMissed = 0;             /* reset counter */
  RunsPassed++;               /* increase counter */


  /*
   *  take care about cycling and power-off
   */

end:

  #ifdef HW_RELAY
  ADC_DDR = (1<<TP_REF);              /* short circuit probes */
  #endif

  LCD_NextLine_Mode(MODE_NONE);       /* reset next line mode */

  /* get key press or timeout */
  Test = TestKey((uint16_t)CYCLE_DELAY, 12);

  if (Test == 0)              /* timeout (no key press) */
  {
    /* check if we reached the maximum number of rounds (continious mode only) */
    if ((RunsMissed >= CYCLE_MAX) || (RunsPassed >= CYCLE_MAX * 2))
    {
      goto power_off;              /* -> power off */
    }
  }
  else if (Test == 1)         /* short key press */
  {
    /* a second key press triggers extra functions */
    MilliSleep(50);
    Test = TestKey(300, 0);

    if (Test > 0)           /* short or long key press */
    {
      #ifdef HW_RELAY
      ADC_DDR = 0;               /* remove short circuit */
      #endif

      MainMenu();                /* enter main menu */
      goto end;                  /* re-run cycle control */
    }
  }
  else if (Test == 2)         /* long key press */
  {
    goto power_off;              /* -> power off */
  }
  #ifdef HW_ENCODER
  else if (Test == 4)         /* rotary encoder: left turn */
  {
    MainMenu();                  /* enter main menu */
    goto end;                    /* re-run cycle control */
  }
  #endif

  /* default action (also for rotary encoder) */
  goto start;                 /* -> next round */


power_off:

  /* display feedback (otherwise the user will wait :-) */
  LCD_Clear();
  LCD_EEString(Bye_str);

  wdt_disable();                        /* disable watchdog */
  CONTROL_PORT &= ~(1 << POWER_CTRL);   /* power off myself */

  return 0;
}
Exemplo n.º 6
0
void SSection::PaintKeys( const FGeometry& AllottedGeometry, const FSlateRect& MyClippingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle ) const
{
	UMovieSceneSection& SectionObject = *SectionInterface->GetSectionObject();

	FSequencer& Sequencer = ParentSectionArea->GetSequencer();

	static const FName BackgroundBrushName("Sequencer.SectionArea.Background");
	static const FName KeyBrushName("Sequencer.Key");

	const FSlateBrush* BackgroundBrush = FEditorStyle::GetBrush(BackgroundBrushName);

	const FSlateBrush* KeyBrush = FEditorStyle::GetBrush(KeyBrushName);

	static const FName SelectionColorName("SelectionColor");
	static const FName SelectionInactiveColorName("SelectionColorInactive");
	static const FName SelectionColorPressedName("SelectionColor_Pressed");

	const FLinearColor PressedKeyColor = FEditorStyle::GetSlateColor(SelectionColorPressedName).GetColor( InWidgetStyle );
	const FLinearColor SelectedKeyColor = FEditorStyle::GetSlateColor(SelectionColorName).GetColor( InWidgetStyle );
	const FLinearColor SelectedInactiveColor = FEditorStyle::GetSlateColor(SelectionInactiveColorName).GetColor( InWidgetStyle )
		* FLinearColor(.25, .25, .25, 1);  // Make the color a little darker since it's not very visible next to white keyframes.

	// @todo Sequencer temp color, make hovered brighter than selected.
	FLinearColor HoveredKeyColor = SelectedKeyColor * FLinearColor(1.5,1.5,1.5,1.0f);

	// Draw all keys in each key area
	for( int32 KeyAreaIndex = 0; KeyAreaIndex < KeyAreas.Num(); ++KeyAreaIndex )
	{
		const FKeyAreaElement& Element = KeyAreas[KeyAreaIndex];

		// Get the key area at the same index of the section.  Each section in this widget has the same layout and the same number of key areas
		const TSharedRef<IKeyArea>& KeyArea = Element.KeyAreaNode.GetKeyArea( SectionIndex );

		FGeometry KeyAreaGeometry = GetKeyAreaGeometry( Element, AllottedGeometry );

		FTimeToPixel TimeToPixelConverter = SectionObject.IsInfinite() ? 			
			FTimeToPixel( ParentGeometry, GetSequencer().GetViewRange()) : 
			FTimeToPixel( KeyAreaGeometry, TRange<float>( SectionObject.GetStartTime(), SectionObject.GetEndTime() ) );

		// Draw a box for the key area 
		// @todo Sequencer - Allow the IKeyArea to do this
		FSlateDrawElement::MakeBox( 
			OutDrawElements,
			LayerId,
			KeyAreaGeometry.ToPaintGeometry(),
			BackgroundBrush,
			MyClippingRect,
			ESlateDrawEffect::None,
			FLinearColor( .1f, .1f, .1f, 0.7f ) ); 


		int32 KeyLayer = LayerId + 1;

		TArray<FKeyHandle> KeyHandles = KeyArea->GetUnsortedKeyHandles();
		for( int32 KeyIndex = 0; KeyIndex < KeyHandles.Num(); ++KeyIndex )
		{
			FKeyHandle KeyHandle = KeyHandles[KeyIndex];
			float KeyTime = KeyArea->GetKeyTime(KeyHandle);

			// Omit keys which would not be visible
			if( SectionObject.IsTimeWithinSection( KeyTime ) )
			{
				FLinearColor KeyColor( 1.0f, 1.0f, 1.0f, 1.0f );

				// Where to start drawing the key (relative to the section)
				float KeyPosition =  TimeToPixelConverter.TimeToPixel( KeyTime );

				FSelectedKey TestKey( SectionObject, KeyArea, KeyHandle );

				bool bSelected = Sequencer.GetSelection().IsSelected( TestKey );
				bool bActive = Sequencer.GetSelection().GetActiveSelection() == FSequencerSelection::EActiveSelection::KeyAndSection;

				if( TestKey == PressedKey )
				{
					KeyColor = PressedKeyColor;
				}
				else if( TestKey == HoveredKey )
				{
					KeyColor = HoveredKeyColor;
				}
				else if( bSelected )
				{
					if (bActive)
					{
						KeyColor = SelectedKeyColor;
					}
					else
					{
						KeyColor = SelectedInactiveColor;
					}
				}

				// Draw the key
				FSlateDrawElement::MakeBox(
					OutDrawElements,
					// always draw selected keys on top of other keys
					bSelected ? KeyLayer+1 : KeyLayer,
					// Center the key along Y.  Ensure the middle of the key is at the actual key time
					KeyAreaGeometry.ToPaintGeometry( FVector2D( KeyPosition - FMath::CeilToFloat(SequencerSectionConstants::KeySize.X/2.0f), ((KeyAreaGeometry.Size.Y*.5f)-(SequencerSectionConstants::KeySize.Y*.5f)) ), SequencerSectionConstants::KeySize ),
					KeyBrush,
					MyClippingRect,
					ESlateDrawEffect::None,
					KeyColor
					);
			}
		}
	}
}
Exemplo n.º 7
0
uint8_t SelfTest(void)
{
    uint8_t           Flag = 0;           /* return value */
    uint8_t           Test = 1;           /* test counter */
    uint8_t           Counter;            /* loop counter */
    uint8_t           DisplayFlag;        /* display flag */
    uint16_t          Val0;               /* voltage/value */
    int16_t           Val1 = 0, Val2 = 0, Val3 = 0;   /* voltages/values */

    /* make sure all probes are shorted */
    Counter = ShortCircuit(1);
    if (Counter == 0) Test = 10;     /* skip selftest */

    /* loop through all tests */
    while (Test <= 6)
    {
        Counter = 1;

        /* repeat each test 5 times */
        while (Counter <= 5)
        {
            /* display test number */
            LCD_Clear();
            LCD_Char('T');                    /* display: T */
            LCD_Char('0' + Test);             /* display test number */
            LCD_Space();

            DisplayFlag = 1;                  /* display values by default */

            /*
             *  tests
             */

            switch (Test)
            {
            case 1:     /* reference voltage */
                Val0 = ReadU(0x0e);           /* dummy read for bandgap stabilization */
                Val0 = ReadU(0x0e);           /* read bandgap reference voltage */
                LCD_EEString(URef_str);       /* display: Vref */

                LCD_NextLine();
                DisplayValue(Val0, -3, 'V');       /* display voltage in mV */

                DisplayFlag = 0;                   /* reset flag */
                break;

            case 2:     /* compare Rl resistors (probes still shorted) */
                LCD_EEString_Space(Rl_str);     /* display: +Rl- */
                LCD_EEString(ProbeComb_str);    /* display: 12 13 23 */

                /* set up a voltage divider with the Rl's */
                /* substract theoretical voltage of voltage divider */

                /* TP1: Gnd -- Rl -- probe-2 -- probe-1 -- Rl -- Vcc */
                R_PORT = 1 << (TP1 * 2);
                R_DDR = (1 << (TP1 * 2)) | (1 << (TP2 * 2));
                Val1 = ReadU_20ms(TP3);
                Val1 -= ((int32_t)Config.Vcc * (R_MCU_LOW + R_LOW)) / (R_MCU_LOW + R_LOW + R_LOW + R_MCU_HIGH);

                /* TP1: Gnd -- Rl -- probe-3 -- probe-1 -- Rl -- Vcc */
                R_DDR = (1 << (TP1 * 2)) | (1 << (TP3 * 2));
                Val2 = ReadU_20ms(TP2);
                Val2 -= ((int32_t)Config.Vcc * (R_MCU_LOW + R_LOW)) / (R_MCU_LOW + R_LOW + R_LOW + R_MCU_HIGH);

                /* TP1: Gnd -- Rl -- probe-3 -- probe-2 -- Rl -- Vcc */
                R_PORT = 1 << (TP2 * 2);
                R_DDR = (1 << (TP2 * 2)) | (1 << (TP3 * 2));
                Val3 = ReadU_20ms(TP2);
                Val3 -= ((int32_t)Config.Vcc * (R_MCU_LOW + R_LOW)) / (R_MCU_LOW + R_LOW + R_LOW + R_MCU_HIGH);

                break;

            case 3:     /* compare Rh resistors (probes still shorted) */
                LCD_EEString_Space(Rh_str);     /* display: +Rh- */
                LCD_EEString(ProbeComb_str);    /* display: 12 13 23 */

                /* set up a voltage divider with the Rh's */

                /* TP1: Gnd -- Rh -- probe-2 -- probe-1 -- Rh -- Vcc */
                R_PORT = 2 << (TP1 * 2);
                R_DDR = (2 << (TP1 * 2)) | (2 << (TP2 * 2));
                Val1 = ReadU_20ms(TP3);
                Val1 -= (Config.Vcc / 2);

                /* TP1: Gnd -- Rh -- probe-3 -- probe-1 -- Rh -- Vcc */
                R_DDR = (2 << (TP1 * 2)) | (2 << (TP3 * 2));
                Val2 = ReadU_20ms(TP2);
                Val2 -= (Config.Vcc / 2);

                /* TP1: Gnd -- Rh -- probe-3 -- probe-2 -- Rh -- Vcc */
                R_PORT = 2 << (TP2 * 2);
                R_DDR = (2 << (TP2 * 2)) | (2 << (TP3 * 2));
                Val3 = ReadU_20ms(TP1);
                Val3 -= (Config.Vcc / 2);

                break;

            case 4:     /* un-short probes */
                ShortCircuit(0);         /* make sure probes are not shorted */
                Counter = 100;           /* skip test */
                DisplayFlag = 0;         /* reset flag */
                break;

            case 5:     /* Rh resistors pulled down */
                LCD_EEString(RhLow_str);      /* display: Rh- */

                /* TP1: Gnd -- Rh -- probe */
                R_PORT = 0;
                R_DDR = 2 << (TP1 * 2);
                Val1 = ReadU_20ms(TP1);

                /* TP1: Gnd -- Rh -- probe */
                R_DDR = 2 << (TP2 * 2);
                Val2 = ReadU_20ms(TP2);

                /* TP1: Gnd -- Rh -- probe */
                R_DDR = 2 << (TP3 * 2);
                Val3 = ReadU_20ms(TP3);

                break;

            case 6:     /* Rh resistors pulled up */
                LCD_EEString(RhHigh_str);     /* display: Rh+ */

                /* TP1: probe -- Rh -- Vcc */
                R_DDR = 2 << (TP1 * 2);
                R_PORT = 2 << (TP1 * 2);
                Val1 = ReadU_20ms(TP1);

                /* TP1: probe -- Rh -- Vcc */
                R_DDR = 2 << (TP2 * 2);
                R_PORT = 2 << (TP2 * 2);
                Val2 = ReadU_20ms(TP2);

                /* TP1: probe -- Rh -- Vcc */
                R_DDR = 2 << (TP3 * 2);
                R_PORT = 2 << (TP3 * 2);
                Val3 = ReadU_20ms(TP3);

                break;
            }

            /* reset ports to defaults */
            R_DDR = 0;                             /* input mode */
            R_PORT = 0;                            /* all pins low */

            /* display voltages/values of all probes */
            if (DisplayFlag)
            {
                LCD_NextLine();                      /* move to line #2 */
                DisplaySignedValue(Val1, 0 , 0);     /* display TP1 */
                LCD_Space();
                DisplaySignedValue(Val2, 0 , 0);     /* display TP2 */
                LCD_Space();
                DisplaySignedValue(Val3, 0 , 0);     /* display TP3 */
            }

            /* wait and check test push button */
            if (Counter < 100)                     /* when we don't skip this test */
            {
                DisplayFlag = TestKey(1000, 0);      /* catch key press or timeout */

                /* short press -> next test / long press -> end selftest */
                if (DisplayFlag > 0)
                {
                    Counter = 100;                       /* skip current test anyway */
                    if (DisplayFlag == 2) Test = 100;    /* also skip selftest */
                }
            }

            Counter++;                        /* next run */
        }

        Test++;                             /* next one */
    }

    Flag = 1;         /* signal success */
    return Flag;
}
Exemplo n.º 8
0
uint8_t SelfAdjust(void)
{
    uint8_t           Flag = 0;           /* return value */
    uint8_t           Test = 1;           /* test counter */
    uint8_t           Counter;            /* loop counter */
    uint8_t           DisplayFlag;        /* display flag */
    uint16_t          Val1 = 0, Val2 = 0, Val3 = 0;   /* voltages */
    uint8_t           CapCounter = 0;     /* number of C_Zero measurements */
    uint16_t          CapSum = 0;         /* sum of C_Zero values */
    uint8_t           RCounter = 0;       /* number of R_Zero measurements */
    uint16_t          RSum = 0;           /* sum of R_Zero values */
    uint8_t           RiL_Counter = 0;    /* number of U_RiL measurements */
    uint16_t          U_RiL = 0;          /* sum of U_RiL values */
    uint8_t           RiH_Counter = 0;    /* number of U_RiH measurements */
    uint16_t          U_RiH = 0;          /* sum of U_RiL values */
    uint32_t          Val0;               /* temp. value */


    /*
     *  measurements
     */

    /* make sure all probes are shorted */
    Counter = ShortCircuit(1);
    if (Counter == 0) Test = 10;     /* skip adjustment on error */

    while (Test <= 5)      /* loop through tests */
    {
        Counter = 1;

        /* repeat each measurement 5 times */
        while (Counter <= 5)
        {
            /* display test number */
            LCD_Clear();
            LCD_Char('A');                    /* display: a */
            LCD_Char('0' + Test);             /* display number */
            LCD_Space();

            DisplayFlag = 1;        /* display values by default */

            /*
             *  tests
             */

            switch (Test)
            {
            case 1:     /* resistance of probe leads (probes shorted) */
                LCD_EEString_Space(ROffset_str);   /* display: R0 */
                LCD_EEString(ProbeComb_str);       /* display: 12 13 23 */

                /*
                 *  The resistance is for two probes in series and we expect it to be
                 *  smaller than 1.00 Ohms, i.e. 0.50 Ohms for a single probe
                 */

                UpdateProbes(TP2, TP1, 0);
                Val1 = SmallResistor(0);
                if (Val1 < 100)                    /* within limit */
                {
                    RSum += Val1;
                    RCounter++;
                }

                UpdateProbes(TP3, TP1, 0);
                Val2 = SmallResistor(0);
                if (Val2 < 100)                    /* whithin limit */
                {
                    RSum += Val2;
                    RCounter++;
                }

                UpdateProbes(TP3, TP2, 0);
                Val3 = SmallResistor(0);
                if (Val3 < 100)                    /* within limit */
                {
                    RSum += Val3;
                    RCounter++;
                }

                break;

            case 2:     /* un-short probes */
                ShortCircuit(0);              /* make sure probes are not shorted */
                Counter = 100;                /* skip test */
                DisplayFlag = 0;              /* reset display flag */
                break;

            case 3:     /* internal resistance of MCU in pull-down mode */
                LCD_EEString(RiLow_str);      /* display: Ri- */

                /* TP1:  Gnd -- Ri -- probe -- Rl -- Ri -- Vcc */
                ADC_PORT = 0;
                ADC_DDR = 1 << TP1;
                R_PORT = 1 << (TP1 * 2);
                R_DDR = 1 << (TP1 * 2);
                Val1 = ReadU_5ms(TP1);
                U_RiL += Val1;

                /* TP2: Gnd -- Ri -- probe -- Rl -- Ri -- Vcc */
                ADC_DDR = 1 << TP2;
                R_PORT =  1 << (TP2 * 2);
                R_DDR = 1 << (TP2 * 2);
                Val2 = ReadU_5ms(TP2);
                U_RiL += Val2;

                /* TP3: Gnd -- Ri -- probe -- Rl -- Ri -- Vcc */
                ADC_DDR = 1 << TP3;
                R_PORT =  1 << (TP3 * 2);
                R_DDR = 1 << (TP3 * 2);
                Val3 = ReadU_5ms(TP3);
                U_RiL += Val3;

                RiL_Counter += 3;
                break;

            case 4:     /* internal resistance of MCU in pull-up mode */
                LCD_EEString(RiHigh_str);     /* display: Ri+ */

                /* TP1: Gnd -- Ri -- Rl -- probe -- Ri -- Vcc */
                R_PORT = 0;
                ADC_PORT = 1 << TP1;
                ADC_DDR = 1 << TP1;
                R_DDR = 1 << (TP1 * 2);
                Val1 = Config.Vcc - ReadU_5ms(TP1);
                U_RiH += Val1;

                /* TP2: Gnd -- Ri -- Rl -- probe -- Ri -- Vcc */
                ADC_PORT = 1 << TP2;
                ADC_DDR = 1 << TP2;
                R_DDR = 1 << (TP2 * 2);
                Val2 = Config.Vcc - ReadU_5ms(TP2);
                U_RiH += Val2;

                /* TP3: Gnd -- Ri -- Rl -- probe -- Ri -- Vcc */
                ADC_PORT = 1 << TP3;
                ADC_DDR = 1 << TP3;
                R_DDR = 1 << (TP3 * 2);
                Val3 = Config.Vcc - ReadU_5ms(TP3);
                U_RiH += Val3;

                RiH_Counter += 3;
                break;

            case 5:     /* capacitance offset (PCB and probe leads) */
                LCD_EEString_Space(CapOffset_str);   /* display: C0 */
                LCD_EEString(ProbeComb_str);         /* display: 12 13 23 */

                /*
                 *  The capacitance is for two probes and we expect it to be
                 *  less than 100pF.
                 */

                MeasureCap(TP2, TP1, 0);
                Val1 = (uint16_t)Caps[0].Raw;
                /* limit offset to 100pF */
                if ((Caps[0].Scale == -12) && (Caps[0].Raw <= 100))
                {
                    CapSum += Val1;
                    CapCounter++;
                }

                MeasureCap(TP3, TP1, 1);
                Val2 = (uint16_t)Caps[1].Raw;
                /* limit offset to 100pF */
                if ((Caps[1].Scale == -12) && (Caps[1].Raw <= 100))
                {
                    CapSum += Val2;
                    CapCounter++;
                }

                MeasureCap(TP3, TP2, 2);
                Val3 = (uint16_t)Caps[2].Raw;
                /* limit offset to 100pF */
                if ((Caps[2].Scale == -12) && (Caps[2].Raw <= 100))
                {
                    CapSum += Val3;
                    CapCounter++;
                }

                break;
            }

            /* reset ports to defaults */
            ADC_DDR = 0;                      /* input mode */
            ADC_PORT = 0;                     /* all pins low */
            R_DDR = 0;                        /* input mode */
            R_PORT = 0;                       /* all pins low */

            /* display values */
            if (DisplayFlag)
            {
                LCD_NextLine();                 /* move to line #2 */
                DisplayValue(Val1, 0 , 0);      /* display TP1 */
                LCD_Space();
                DisplayValue(Val2, 0 , 0);      /* display TP2 */
                LCD_Space();
                DisplayValue(Val3, 0 , 0);      /* display TP3 */
            }

            /* wait and check test push button */
            if (Counter < 100)                     /* when we don't skip this test */
            {
                DisplayFlag = TestKey(1000, 0);      /* catch key press or timeout */

                /* short press -> next test / long press -> end selftest */
                if (DisplayFlag > 0)
                {
                    Counter = 100;                       /* skip current test anyway */
                    if (DisplayFlag == 2) Test = 100;    /* also skip selftest */
                }
            }

            Counter++;                        /* next run */
        }

        Test++;                             /* next one */
    }


    /*
     *  calculate values and offsets
     */

    /* capacitance auto-zero: calculate average value for all probe pairs */
    if (CapCounter == 15)
    {
        /* calculate average offset (pF) */
        NV.CapZero = CapSum / CapCounter;
        Flag++;
    }

    /* resistance auto-zero: calculate average value for all probes pairs */
    if (RCounter == 15)
    {
        /* calculate average offset (0.01 Ohms) */
        NV.RZero = RSum / RCounter;
        Flag++;
    }

    /* RiL & RiH */
    if ((RiL_Counter == 15) && (RiH_Counter == 15))
    {
        /*
         *  Calculate RiL and RiH using the voltage divider rule:
         *  Ri = Rl * (U_Ri / U_Rl)
         *  - scale up by 100, round up/down and scale down by 10
         */

        /* use values multiplied by 3 to increase accuracy */
        U_RiL /= 5;                         /* average sum of 3 U_RiL */
        U_RiH /= 5;                         /* average sum of 3 U_RiH */
        Val1 = (Config.Vcc * 3) - U_RiL - U_RiH;  /* U_Rl * 3 */

        /* RiL */
        Val0 = ((uint32_t)R_LOW * 100 * U_RiL) / Val1;     /* Rl * U_Ri / U_Rl in 0.01 Ohm */
        Val0 += 5;                                         /* for automagic rounding */
        Val0 /= 10;                                        /* scale down to 0.1 Ohm */
        if (Val0 < 250UL)         /* < 25 Ohms */
        {
            NV.RiL = (uint16_t)Val0;
            Flag++;
        }

        /* RiH */
        Val0 = ((uint32_t)R_LOW * 100 * U_RiH) / Val1;     /* Rl * U_Ri / U_Rl in 0.01 Ohm */
        Val0 += 5;                                         /* for automagic rounding */
        Val0 /= 10;                                        /* scale down to 0.1 Ohm */
        if (Val0 < 280UL)         /* < 29 Ohms */
        {
            NV.RiH = (uint16_t)Val0;
            Flag++;
        }
    }

    /* show values and offsets */
    ShowAdjust();

    if (Flag == 4) Flag = 1;         /* all adjustments done -> success */
    else Flag = 0;                   /* signal error */

    return Flag;
}