Adel Faki
Published © GPL3+

Luminous Dome

Building a magical dome which is controlled by gestures.

IntermediateProtip2 hours469
Luminous Dome

Things used in this project

Story

Read more

Code

IR code

C/C++
/**
  ******************************************************************************
  * File Name          : main.c
  * Description        : Main program body
  ******************************************************************************
  *
  * COPYRIGHT(c) 2015 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
	
/*
		MODIFIED by Hexabitz for BitzOS (BOS) V0.1.4 - Copyright (C) 2017 Hexabitz
    All rights reserved
*/

/* Includes ------------------------------------------------------------------*/
#include "BOS.h"


/* Private variables ---------------------------------------------------------*/
float sensor = 0.0f;
uint32_t t0;

/* Private function prototypes -----------------------------------------------*/
uint8_t AbsDistanceToColor(float distance);
bool DetectedHorizontalSweep(float distance);
uint8_t HorizontalSweepToColor(float distance);
void ChangeModes();
int myMode=0;

TIM_HandleTypeDef htim7;
/* Main functions ------------------------------------------------------------*/

int main(void)
{  
 
  /* MCU Configuration----------------------------------------------------------*/

  /* Reset all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all user peripherals */

	/* Initialize BitzOS */
	BOS_Init();

  /* Call init function for freertos objects (in freertos.c) */
  MX_FREERTOS_Init();

  /* Start scheduler */
  osKernelStart();
  
  /* We should never get here as control is now taken by the scheduler */

  /* Infinite loop */
  while (1)
  {


  }


}

/*-----------------------------------------------------------*/

/* FrontEndTask function */
void FrontEndTask(void * argument)
{ 
	/* Peripheral clock enable */
	 __TIM7_CLK_ENABLE();
	 htim7.Instance = TIM7;
   htim7.Init.Prescaler = 47999; //48MHz/48000 = 1000Hz
   htim7.Init.Period = 2499; //1000HZ / 2500 = 0.4Hz = 2.5s
	 htim7.Init.CounterMode=TIM_COUNTERMODE_UP;
	 htim7.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;
	 HAL_TIM_Base_Init(&htim7); //Configure the timer
	 __HAL_TIM_ENABLE_IT(&htim7, TIM_IT_UPDATE );
	 HAL_NVIC_SetPriority(TIM7_IRQn, 0, 0);
   HAL_NVIC_EnableIRQ(TIM7_IRQn);

	
	 uint8_t color = 0, colorOld = 0, intensity = 0;
	
	
	// Set units to cm
	SetRangeUnit(UNIT_MEASUREMENT_MM);
	
	// Stream to memory
	Stream_ToF_Memory(50, portMAX_DELAY, &sensor);
	
  /* Infinite loop */
  for(;;)
  {
		//changing mode distance;
     if (sensor > 25.0f && sensor< 60.0f)
			{
				ChangeModes();
			}
		else
		{	
			HAL_TIM_Base_Stop_IT(&htim7);
		}
		
		Delay_ms(10);
		
		switch (myMode)
		{

			case 1:								// LEDs off		
				SendMessageToModule(BOS_BROADCAST, 101, 0);		// CODE_H01R0_OFF
							// Reset the mode to execute this only once
				break;
			
			case 2:								// Sensor sweeps LED colors vertically based on absolute distance
			case 3:								// Sensor sweeps LED colors horizontally			
				if (myMode == 2)
					color = AbsDistanceToColor(sensor);
				else
					color = HorizontalSweepToColor(sensor);
			
				if (color != colorOld) {												// Execute only if color changed
					messageParams[0] = 0;													// set color name
					messageParams[1] = color;
					messageParams[2] = 100;
					SendMessageToModule(BOS_BROADCAST, 103, 3);		// CODE_H01R0_COLOR
					colorOld = color;
				}
				break;
				
			case 4:								// Toggle white LEDs with hand sweep
				if (DetectedHorizontalSweep(sensor)) {
					messageParams[0] = 100;
					SendMessageToModule(BOS_BROADCAST, 102, 1);		// CODE_H01R0_TOGGLE
				}
				break;
				
			case 5:								// Sensor controls white LED intensity
				if (sensor < 125.0f)	
					intensity = 0;
				else if (sensor >= h08r6MaxRange)
					;// Do not update intensity
				else
					intensity = (sensor-125)/5;
				if (intensity > 100) intensity = 100;
				messageParams[0] = 0;
				messageParams[1] = 2;													// WHITE
				messageParams[2] = intensity;
				SendMessageToModule(BOS_BROADCAST, 103, 3);		// CODE_H01R0_COLOR				
				break;
			
			default:
				break;

		}
	}
}

//changing between modes by time and distance:
void ChangeModes(){
	HAL_TIM_Base_Start_IT(&htim7);
 }

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
	{
		myMode++;
			SendMessageToModule(BOS_BROADCAST, CODE_ping, 0);
  	if(myMode==6)
	  {myMode=0;
  	}
  }
	//interrupt vector:
    void TIM7_IRQHandler(void)
	{
 
      HAL_TIM_IRQHandler(&htim7);
  }

// Map absolute sensor distance to basic RGB colors
uint8_t AbsDistanceToColor(float distance)
{
//	static float distanceOld;
	
//	// Only update if there's at least 1cm change 
//	if ( distance-distanceOld <= 1.0f || distanceOld-distance <= 1.0f )
//		distance = distanceOld;
//	else
//		distanceOld = distance;

	if (distance < 125.0f)
		return 5;	// YELLOW
	else if (distance < 150.0f)
		return 8;	// GREEN
	else if (distance < 175.0f)
		return 6;	// CYAN
	else if (distance < 200.0f)
		return 4;	// BLUE
	else if (distance < 225.0f)
		return 7;	// MAGENTA
	else if (distance < 250.0f)
		return 3;	// RED
	else
		return 2;	// WHITE
}

bool DetectedHorizontalSweep(float distance)
{
	static float state;
	
	if (distance < h08r6MaxRange && distance > 0.0f)
		state = 1;	// Detected an object
	else if (distance >= h08r6MaxRange && state == 1)
		{
		state = 2;	// The object cleared
		return true;
	  }
		
	return false;
}

uint8_t HorizontalSweepToColor(float distance)
{
	static float colorOld;
	uint8_t temp;
	
	if (DetectedHorizontalSweep(distance))
	{
		if (colorOld == 0 || colorOld == 2)
			temp = 1;	// OFF
		else if (colorOld == 1)
			temp = 5;	// YELLOW
		else if (colorOld == 5)
			temp = 8;	// GREEN
		else if (colorOld == 8)
			temp = 6;	// CYAN
		else if (colorOld == 6)
			temp = 4;	// BLUE
		else if (colorOld == 4)
			temp = 7;	// MAGENTA
		else if (colorOld == 7)
			temp = 3;	// RED
		else if (colorOld == 3)
			temp = 2;	// WHITE
		
		colorOld = temp;
		return temp;
  	}
	else
		return colorOld;
}

/*-----------------------------------------------------------*/

/************************ (C) COPYRIGHT HEXABITZ *****END OF FILE****/

RGB code

C/C++
/**
  ******************************************************************************
  * File Name          : main.c
  * Description        : Main program body
  ******************************************************************************
  *
  * COPYRIGHT(c) 2015 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
	
/*
		MODIFIED by Hexabitz for BitzOS (BOS) V0.1.4 - Copyright (C) 2017 Hexabitz
    All rights reserved
*/

/* Includes ------------------------------------------------------------------*/
#include "BOS.h"


/* Private variables ---------------------------------------------------------*/


/* Private function prototypes -----------------------------------------------*/



/* Main functions ------------------------------------------------------------*/

int main(void)
{


  /* MCU Configuration----------------------------------------------------------*/

  /* Reset all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all user peripherals */

	/* Initialize BitzOS */
	BOS_Init();

  /* Call init function for freertos objects (in freertos.c) */
  MX_FREERTOS_Init();

  /* Start scheduler */
  osKernelStart();
  
  /* We should never get here as control is now taken by the scheduler */

  /* Infinite loop */
  while (1)
  {


  }


}

/*-----------------------------------------------------------*/

/* FrontEndTask function */
void FrontEndTask(void * argument)
{

}

/*-----------------------------------------------------------*/

/************************ (C) COPYRIGHT HEXABITZ *****END OF FILE****/

Credits

Adel Faki

Adel Faki

2 projects • 10 followers

Comments