Skip to content

Commit

Permalink
Introduced basic joystick function
Browse files Browse the repository at this point in the history
>Added utils library and moved onboard led blink function to it
  • Loading branch information
Abhijith Sriram committed Oct 13, 2024
1 parent 970bddf commit df65558
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 20 deletions.
Binary file not shown.
11 changes: 3 additions & 8 deletions src/appl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 4,9 @@ message("Building application")
add_library(APPL STATIC main.c)
target_include_directories(APPL PRIVATE ${CMAKE_SOURCE_DIR}/src/lib/timer)

# conditionally add application to the library
if (${APPL_TO_BUILD} MATCHES "BLINKLED")
set(DIR_NAME blinkLed)
set(APPL_MACRO APPL_BLINKLED=1)
elseif (${APPL_TO_BUILD} MATCHES "ULTRASONIC")
set(DIR_NAME ultrasonic)
set(APPL_MACRO APPL_ULTRASONIC=1)
endif()
# build the application based on the user input
string(TOLOWER ${APPL_TO_BUILD} DIR_NAME)
string(CONCAT APPL_MACRO "APPL_" ${APPL_TO_BUILD} "=1")

add_subdirectory(${DIR_NAME})
target_include_directories(APPL PRIVATE ${CMAKE_SOURCE_DIR}/src/appl/${DIR_NAME})
Expand Down
12 changes: 12 additions & 0 deletions src/appl/joystick/CMakelists.txt
Original file line number Diff line number Diff line change
@@ -0,0 1,12 @@
cmake_minimum_required(VERSION 3.25.0)

message("Building joystick")
set(SOURCES joystick.c)
set(HEADERS joystick.h)
add_library(joystick joystick.c)
set_target_properties(joystick PROPERTIES FOLDER "appls/joystick")
target_include_directories(joystick PRIVATE
${CMAKE_SOURCE_DIR}/src/lib/timer
${CMAKE_SOURCE_DIR}/src/lib/eeprom
${CMAKE_SOURCE_DIR}/src/lib/utils
${CMAKE_SOURCE_DIR}/src/lib/pins)
24 changes: 24 additions & 0 deletions src/appl/joystick/joystick.c
Original file line number Diff line number Diff line change
@@ -0,0 1,24 @@
#include "joystick.h"
#include "timer.h"
#include "pin.h"
#include "utils.h"

void joystick_setup()
{
/* Configure pin PB5 to output to show sensor has detected something */
Configure_pinPort(PORTB, (uint8_t)PINB5, OUTPUT);
/* Configure PD2 as the trigger for the sensor */
Configure_pinPort(PORTD, (uint8_t)PIND2, INPUT);
}
void joystick_main()
{
if(Read_pin(PORTD, (uint8_t)PIND2) == PIN_LOW)
{
/* The push button is high by default */
Blink_onboard_led(1000U);
}
else
{
Write_pin(PORTB, (uint8_t)PINB5, PIN_LOW);
}
}
7 changes: 7 additions & 0 deletions src/appl/joystick/joystick.h
Original file line number Diff line number Diff line change
@@ -0,0 1,7 @@
#ifndef __JOYSTICK__
#define __JOYSTICK__

void joystick_setup();
void joystick_main();

#endif
9 changes: 9 additions & 0 deletions src/appl/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 6,9 @@
#ifdef APPL_ULTRASONIC
#include "ultrasonic.h"
#endif
#ifdef APPL_JOYSTICK
#include "joystick.h"
#endif

int main()
{
Expand All @@ -18,6 21,9 @@ int main()
#ifdef APPL_ULTRASONIC
ultrasonic_setup();
#endif
#ifdef APPL_JOYSTICK
joystick_setup();
#endif

/* Start the main loop */
while(1U)
Expand All @@ -28,6 34,9 @@ int main()
#ifdef APPL_ULTRASONIC
ultrasonic_main();
#endif
#ifdef APPL_JOYSTICK
joystick_main();
#endif
}
return 1;
}
1 change: 1 addition & 0 deletions src/appl/ultrasonic/CMakelists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 8,5 @@ set_target_properties(ultrasonic PROPERTIES FOLDER "appls/ultrasonic")
target_include_directories(ultrasonic PRIVATE
${CMAKE_SOURCE_DIR}/src/lib/timer
${CMAKE_SOURCE_DIR}/src/lib/eeprom
${CMAKE_SOURCE_DIR}/src/lib/utils
${CMAKE_SOURCE_DIR}/src/lib/pins)
13 changes: 3 additions & 10 deletions src/appl/ultrasonic/ultrasonic.c
Original file line number Diff line number Diff line change
@@ -1,6 1,7 @@
#include "ultrasonic.h"
#include "timer.h"
#include "pin.h"
#include "utils.h"

#define DIST_THRSH (uint16_t)20
#define TRIG_DELAY_US (uint32_t)12
Expand All @@ -23,14 24,6 @@ void ultrasonic_setup()
Configure_pinPort(PORTB, (uint8_t)PINB0, INPUT);
}

void blink_led(uint32_t delay)
{
Write_pin(PORTB, (uint8_t)PINB5, PIN_HIGH);
Delay_ms(delay);
Write_pin(PORTB, (uint8_t)PINB5, PIN_LOW);
Delay_ms(delay);
}

void ultrasonic_main()
{
static ultrasonic_state state = SEND_TRIG;
Expand Down Expand Up @@ -65,15 58,15 @@ void ultrasonic_main()
if(time <= TIME_NO_OBJ_US)
{
/* We have detected something so light up the LED */
blink_led(1000U);
Blink_onboard_led(1000U);

}
else
{
Write_pin(PORTB, (uint8_t)PINB5, PIN_LOW);
}
state = SEND_TRIG;
blink_led(200U);
Blink_onboard_led(200U);
break;

default:
Expand Down
3 changes: 2 additions & 1 deletion src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 4,8 @@ message("Building the lib")
add_subdirectory(timer)
add_subdirectory(pins)
add_subdirectory(eeprom)
add_subdirectory(utils)

file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/lib/dummy_lib.c "")
add_library(LIB STATIC ${CMAKE_CURRENT_BINARY_DIR}/lib/dummy_lib.c)
target_link_libraries(LIB PINS TIMER EEPROM)
target_link_libraries(LIB PINS UTILS TIMER EEPROM)
1 change: 0 additions & 1 deletion src/lib/pins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 5,3 @@ set(SOURCES pin.c)
set(HEADERS pin.h)
add_library(PINS STATIC pin.c)
set_target_properties(PINS PROPERTIES FOLDER "lib/pins")
target_include_directories(PINS PRIVATE ${CMAKE_SOURCE_DIR}/src/lib/utils)
8 changes: 8 additions & 0 deletions src/lib/utils/CMakelists.txt
Original file line number Diff line number Diff line change
@@ -0,0 1,8 @@
cmake_minimum_required(VERSION 3.25.0)

message("Building PINS")
set(SOURCES utils.c)
set(HEADERS utils.h)
add_library(UTILS STATIC utils.c)
set_target_properties(UTILS PROPERTIES FOLDER "lib/utils")
target_include_directories(UTILS PRIVATE ${CMAKE_SOURCE_DIR}/src/lib/pins)
21 changes: 21 additions & 0 deletions src/lib/utils/utils.c
Original file line number Diff line number Diff line change
@@ -0,0 1,21 @@
#include <stdint.h>
#include "utils.h"
#include "pin.h"

/*
* Function to blink the onboard LED. This function assumes that the pin PB5
* is set as output prior to the function call
*
* INPUT
* @param None
*
* OUTPUT
* @param None
*/
void Blink_onboard_led(uint32_t delay_ms)
{
Write_pin(PORTB, (uint8_t)PINB5, PIN_HIGH);
Delay_ms(delay_ms);
Write_pin(PORTB, (uint8_t)PINB5, PIN_LOW);
Delay_ms(delay_ms);
}
7 changes: 7 additions & 0 deletions src/lib/utils/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 1,7 @@
#ifndef __UTILS__
#define __UTILS__
#include <stdint.h>

void Blink_onboard_led(uint32_t delay_ms);

#endif

0 comments on commit df65558

Please sign in to comment.