Demo entry 6554590

testing.c

   

Submitted by AirGuardian on Jun 02, 2017 at 11:03
Language: C. Code size: 15.3 kB.

/*
* Copyright (c) 2017 AirGuardian Team <airguardianteam@gmail.com>
* All Rights Reserved.
*
* This file is part of AirGuardian.
*
* Controller: PIC 16F1847
* Compiler: XC8
* Look at the documentation or in mcc_generated_files/pin_manager.h to see the
* pinout.
*
* FILE:
* Date:			16.03.2017
* Author:		Florian Freitag
*
* This file replaces main.c if you just want to test a part of the project.
* However, this file won't be in the final product and you should only use this
* code to test certain components of the whole project.
*/


/*
* These are all the tests you can do.
* Please uncomment the one you would like to make but recomment it afterwards.
*/
//#define BLINK_TEST
//#define BUTTON_TEST
//#define WS2812_TEST1
//#define WS2812_TEST2
#define WS2812_TEST3
//#define LEDDISPLAY_TEST1
//#define LEDDISPLAY_TEST2
//#define ADC_WITH_LEDDISPLAY_TEST
//#define ADC_WITH_EUSART_TEST
//#define SENSOR_WITH_EUSART_TEST
//#define EEPROM_CLEAR
//#define EEPROM_VALUE
//#define EEPROM_MONITOR
//#define EUSART_TEST1
//#define EUSART_TEST2


/*
* This test is just for testing the major function of the PIC.
* It will let the Test-Led on the Board blink.
*/
#ifdef BLINK_TEST

#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/pin_manager.h"

#define _XTAL_FREQ  32000000

void main()
{
  SYSTEM_Initialize();

  while (1)
  {
    ledTest_Toggle();
    __delay_ms(500);
  }
}
#endif // BLINK_TEST


/*
* With this test the button will light up when it is pressed.
*/
#ifdef BUTTON_TEST

#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/pin_manager.h"

#define _XTAL_FREQ  32000000

void main()
{
  SYSTEM_Initialize();

  while (1)
  {
    if (btnSleep_GetValue() == 0)
    {
      ledStatus_SetHigh();
      ledTest_SetHigh();
    }
    else
    {
      ledStatus_SetLow();
      ledTest_SetLow();
    }
  }
}
#endif // BUTTON_TEST


/*
* This test tests the ws2812b.h file and lets the LEDs blink in different
* colors.
* First in red, than green and last blue.
*/
#ifdef WS2812_TEST1

#include "ws2812b.h"
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/pin_manager.h"

#define _XTAL_FREQ  32000000

int main()
{
  SYSTEM_Initialize(); //Initialize all IOs

  while(1)
  {
    clearLedBuffer();
    updateLeds();
    ledTest_Toggle();
    __delay_ms(300);

    setLedBufferColor(40, 0, 0);
    updateLeds();
    ledTest_Toggle();
    __delay_ms(300);

    clearLedBuffer();
    updateLeds();
    ledTest_Toggle();
    __delay_ms(300);

    setLedBufferColor(0, 40, 0);
    updateLeds();
    ledTest_Toggle();
    __delay_ms(300);

    clearLedBuffer();
    updateLeds();
    ledTest_Toggle();
    __delay_ms(300);

    setLedBufferColor(0, 0, 40);
    updateLeds();
    ledTest_Toggle();
    __delay_ms(300);
  }
}
#endif // WS2812_TEST1


/*
* This test tests the ws2812b.h file.
* It executes the runpoint animation.
*/
#ifdef WS2812_TEST2

#include "ws2812b.h"
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/pin_manager.h"

#define _XTAL_FREQ  32000000

int main()
{
  SYSTEM_Initialize(); // Initialize all IOs

  while(1)
  {
    calcLedRunPointAnimation();
    updateLeds();
    updateLeds();
    ledTest_Toggle();
    __delay_ms(40);
  }
}
#endif // WS2812_TEST2


/*
* This test tests the ws2812b.h file.
* It executes the RainbowAnimation2.
*/
#ifdef WS2812_TEST3

#include "ws2812b.h"
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/pin_manager.h"

#define _XTAL_FREQ  32000000

int main()
{
  SYSTEM_Initialize(); // Initialize all IOs

  while(1)
  {
    calcLedRainbowAnimation2();
    updateLeds();
    ledTest_Toggle();
    __delay_ms(30);
  }
}
#endif // WS2812_TEST3


/*
* This test tests the ledDisplay.h file.
* It executes all types how to display values.
* However, this methode DOESN NOT interpolate the colors so they won't appear
* smooth.
*/
#ifdef LEDDISPLAY_TEST1

#include "ws2812b.h"
#include "ledDisplay.h"
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/pin_manager.h"
#include <stdint.h>

#define _XTAL_FREQ  32000000

int main()
{
  SYSTEM_Initialize(); // Initialize all IOs

  while(1)
  {
    clearLedBuffer();
    updateLeds();
    __delay_ms(500);

    for(uint8_t i=0; i<100; i++)
    {
      displayLedPercent1(i);
      updateLeds();
      __delay_ms(20);
    }

    clearLedBuffer();
    updateLeds();
    __delay_ms(500);


    for(uint8_t i=0; i<100; i++)
    {
      displayLedPercent2(i);
      updateLeds();
      __delay_ms(20);
    }

    clearLedBuffer();
    updateLeds();
    __delay_ms(500);


    for(uint8_t i=0; i<100; i++)
    {
      displayLedPercent3(i);
      updateLeds();
      __delay_ms(20);
    }

    clearLedBuffer();
    updateLeds();
    __delay_ms(500);
  }
}
#endif  // LEDDISPLAY_TEST1


/*
* This test tests the ledDisplay.h file.
* It executes all types how to display values.
* This test interpolates the colors so they appear smooth.
*/
#ifdef LEDDISPLAY_TEST2

#include "ws2812b.h"
#include "ledDisplay.h"
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/pin_manager.h"
#include <stdint.h>

#define _XTAL_FREQ  32000000

int main()
{
  SYSTEM_Initialize(); //Initialize all IOs
  displaySmoothColor = 1;

  while(1)
  {
    clearLedBuffer();
    updateLeds();
    __delay_ms(500);

    for(uint8_t i=0; i<100; i++)
    {
      displayLedPercent1(i);
      updateLeds();
      __delay_ms(20);
    }

    clearLedBuffer();
    updateLeds();
    __delay_ms(500);


    for(uint8_t i=0; i<100; i++)
    {
      displayLedPercent2(i);
      updateLeds();
      __delay_ms(20);
    }

    clearLedBuffer();
    updateLeds();
    __delay_ms(500);


    for(uint8_t i=0; i<100; i++)
    {
      displayLedPercent3(i);
      updateLeds();
      __delay_ms(20);
    }

    clearLedBuffer();
    updateLeds();
    __delay_ms(500);
  }
}
#endif  // LEDDISPLAY_TEST2


/*
* This test reads the analog pin where the sensor is or will be and displays
* the value with the function displayLedPercent2 from the file ledDisplay.h.
*/
#ifdef ADC_WITH_LEDDISPLAY_TEST1

#include "ws2812b.h"
#include "ledDisplay.h"
#include "interpolate.h"
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/pin_manager.h"
#include "mcc_generated_files/adc.h"

int main()
{
  SYSTEM_Initialize(); // Initialize all IOs
  displaySmoothColor = 1;

  while(1)
  {
    // This code displays the value of the analog input with the leds
    displayLedPercent3(toPercent(ADC_GetConversion(0b00000), 0, 4000));
    updateLeds();
  }
}
#endif  // ADC_WITH_LEDDISPLAY_TEST1


/*
* This is not really a test it is more like a setting you can make.
* It will simply wipe of all values from the EEPROM an write zeros in every
* cell.
*/
#ifdef EEPROM_CLEAR

#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/pin_manager.h"
#include "mcc_generated_files/memory.h"
#include <stdint.h>

#define _XTAL_FREQ  32000000

int main()
{
  SYSTEM_Initialize(); // Initialize all IOs

  for(uint16_t i = 0; i < 256; i++)
  {
    DATAEE_WriteByte(i, 0b00000000);
  }

  while(1)
  {
    // Let the LED blink to indicate that the clearing is done.
    ledStatus_Toggle();
    __delay_ms(400);
  }
}
#endif  // EEPROM_CLEAR


/*
* This is not really a test it is more like a setting you can make.
* This code writes the default value of each setting into EEPROM.
*/
#ifdef EEPROM_VALUE

#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/pin_manager.h"
#include "mcc_generated_files/memory.h"
#include "setting.h"
#include <stdint.h>

#define _XTAL_FREQ  32000000

int main()
{
  SYSTEM_Initialize(); // Initialize all IOs

  writeSetting(setting_isStandby.address, 0);
  writeSetting(setting_displayMode.address, 2);
  writeSetting(setting_goodColorRed.address, 0);
  writeSetting(setting_goodColorGreen.address, 255);
  writeSetting(setting_goodColorBlue.address, 0);
  writeSetting(setting_middleColorRed.address, 0);
  writeSetting(setting_middleColorGreen.address, 0);
  writeSetting(setting_middleColorBlue.address, 255);
  writeSetting(setting_badColorRed.address, 255);
  writeSetting(setting_badColorGreen.address, 0);
  writeSetting(setting_badColorBlue.address, 0);
  writeSetting(setting_isSmoothColor.address, 1);
  writeSetting(setting_bootAnimation.address, 1);
  writeSetting(setting_brightness.address, 40);


  while(1)
  {
    // Let the LED blink to indicate that the code if ready.
    ledTest_Toggle();
    __delay_ms(400);
  }
}
#endif  // EEPROM_VALUE


/*
* This code sends the current values from the EEPROM Settings per EUSART,
* without changing them.
*
* To get the data you first have to send anything with an linefeed '\n' at the
* end and the PIC will answer with all settings.
*/
#ifdef EEPROM_MONITOR

#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/pin_manager.h"
#include "mcc_generated_files/memory.h"
#include "mcc_generated_files/eusart.h"
#include "mcc_generated_files/interrupt_manager.h"
#include "setting.h"
#include <stdint.h>
#include <stdio.h>

#define _XTAL_FREQ  32000000


int main()
{
  SYSTEM_Initialize(); // Initialize all IOs

  loadSettings();	// Load Settings from EEPROM

  INTERRUPT_GlobalInterruptEnable();  // Enable interupts
  INTERRUPT_PeripheralInterruptEnable();  // Enable peripheral interupts

  char buffer[32];

  while(1)
  {
    while (EUSART_DataReady)
    {
      if (EUSART_Read() == '\n')
      {
        printf("AIRGUARDIAN EEPROM_MONITOR\n");
        __delay_ms(80);
        printf("--------------------------\n");
        __delay_ms(80);
        printf("Standby: %d\n", setting_isStandby.value);
        __delay_ms(80);
        printf("DisplayMode: %d\n", setting_displayMode.value);
        __delay_ms(80);
        printf("Good Color Red: %d\n", setting_goodColorRed.value);
        __delay_ms(80);
        printf("Good Color Greend: %d\n", setting_goodColorGreen.value);
        __delay_ms(80);
        printf("Good Color Blue: %d\n", setting_goodColorBlue.value);
        __delay_ms(80);
        printf("Middle Color Red: %d\n", setting_middleColorRed.value);
        __delay_ms(80);
        printf("Middle Color Green: %d\n", setting_middleColorGreen.value);
        __delay_ms(80);
        printf("Middle Color Blue: %d\n", setting_middleColorBlue.value);
        __delay_ms(80);
        printf("Bad Color Red: %d\n", setting_badColorRed.value);
        __delay_ms(80);
        printf("Bad Color Green: %d\n", setting_badColorGreen.value);
        __delay_ms(80);
        printf("Bad Color Blue: %d\n", setting_badColorBlue.value);
        __delay_ms(80);
        printf("Smooth Color: %d\n", setting_isSmoothColor.value);
        __delay_ms(80);
        printf("Boot Animation: %d\n", setting_bootAnimation.value);
        __delay_ms(80);
        printf("Brigthness: %d\n", setting_brightness.value);
        __delay_ms(80);
        printf("#####END#####\n\n");

        ledTest_Toggle();
      }
    }
  }
}
#endif  // EEPROM_MONITOR



/*
* This test will only write per bluetooth.
* It will write every second a "Hello World" to the serial connection.
*/
#ifdef EUSART_TEST1

#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/eusart.h"
#include "mcc_generated_files/pin_manager.h"
#include "mcc_generated_files/interrupt_manager.h"
#include <stdint.h>
#include <stdio.h>

#define _XTAL_FREQ 32000000

int main()
{
  SYSTEM_Initialize(); // Initialize all IOs

  INTERRUPT_GlobalInterruptEnable();  // Enable interupts
  INTERRUPT_PeripheralInterruptEnable();  // Enable peripheral interupts


  while (1)
  {
    printf("Hello World\n");

    ledTest_Toggle();
    __delay_ms(1000);
  }
}

#endif


/*
* This test is a little echo programm with EUSART.
*/
#ifdef EUSART_TEST2

#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/eusart.h"
#include "mcc_generated_files/pin_manager.h"
#include "mcc_generated_files/interrupt_manager.h"
#include <stdint.h>
#include <string.h>
#include <stdio.h>

#define _XTAL_FREQ 32000000

int main()
{
  SYSTEM_Initialize(); //Initialize all IOs

  INTERRUPT_GlobalInterruptEnable();  // Enable interupts
  INTERRUPT_PeripheralInterruptEnable();  // Enable peripheral interupts

  char buffer[32];
  while (1)
  {
    while (EUSART_DataReady)
    {
      buffer[strlen(buffer)] = EUSART_Read();

      if (buffer[strlen(buffer) - 1] == '\n')
      {
        buffer[strlen(buffer) - 1] = 0; // Delete the last linefeed;
        printf("Echo: \"%s\"\n", buffer);
        memset(buffer, 0, 32);
        ledTest_Toggle();
      }
    }
  }
}
#endif


/*
* This test reads the ADC from both, the air and the temp sensor and sends the
* values per EUSART.
*/
#ifdef ADC_WITH_EUSART_TEST

#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/eusart.h"
#include "mcc_generated_files/pin_manager.h"
#include "mcc_generated_files/interrupt_manager.h"
#include "mcc_generated_files/adc.h"
#include <stdint.h>
#include <stdio.h>

#define _XTAL_FREQ 32000000

int main()
{
  SYSTEM_Initialize(); // Initialize all IOs

  INTERRUPT_GlobalInterruptEnable();  // Enable interupts
  INTERRUPT_PeripheralInterruptEnable();  // Enable peripheral interupts

  while (1)
  {
    printf("Airsensor: \"%d\"\nTempsensor: \"%d\"\n################\n", ADC_GetConversion(0b00000), ADC_GetConversion(0b00010));
    ledTest_Toggle();
    __delay_ms(1000);
  }
}
#endif


/*
* Like ADC_WITH_EUSART_TEST it reads the values from the ADC. However, it uses
* the sensor.h libary.
* Moreover, you have to send sonmeting with a linefeed to the PIC to get some
* values back.
*/
#ifdef SENSOR_WITH_EUSART_TEST

#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/eusart.h"
#include "mcc_generated_files/pin_manager.h"
#include "mcc_generated_files/interrupt_manager.h"
#include "mcc_generated_files/adc.h"
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include "sensor.h"
#include "interpolate.h"

#define _XTAL_FREQ 32000000

int main()
{
  SYSTEM_Initialize(); // Initialize all IOs

  INTERRUPT_GlobalInterruptEnable();  // Enable interupts
  INTERRUPT_PeripheralInterruptEnable();  // Enable peripheral interupts

  while (1)
  {
    updateSensors();  // Reading the first sensor
    __delay_ms(1000);
    updateSensors();  // Reading the second sensor
    __delay_ms(1000);

    // Sending the values
    printf("airsensorRaw: %d\n",airsensorRaw);
    printf("airsensorPercent: %d%%\n",airsensorPercent);
    printf("airsensorHex: %d\n", airsensorHex);
    printf("tempsensorRaw: %d\n", tempsensorRaw);
    printf("tempsensorCelsius: %dC\n\n", tempsensorCelsius);
    ledTest_Toggle();
  }
}

#endif

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).