#include "SevSeg.h"
/*
Instantiate a seven segment controller object
*/
SevSeg sevseg;
/**
* @brief This function initializes the seven segment at driver level
* with necessary argurments.
* @test MISRA-C 2012 Compliant function.
* @return void
*/
void sevenSegmentSetup(void)
{
/*
Number of digits inside the seven segment.
*/
byte numDigits = 4U;
/*
Pin number for the digits.
*/
byte digitPins[] = {2, 3, 4, 5};
/*
Pin number for the segments.
*/
byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
/*
'false' means resistors are on digit pins.
*/
bool resistorsOnSegments = false;
/*
the above pins will be positive pins since it's common anode.
*/
byte hardwareConfig = COMMON_ANODE;
/*
we give some time
*/
bool updateWithDelays = true;
/*
Use 'true' if you'd like to keep the leading zeros
*/
bool leadingZeros = true;
/*
Use 'true' if your decimal point doesn't exist or isn't connected
*/
bool disableDecPoint = true;
/*
Initializing the Seven segment at the driver level.
*/
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
/*
setting the brightness level, but doesn't matter for the simulator.
*/
sevseg.setBrightness(90);
}
/**
* @brief This function calculates fiboncci number and turns ON the LED
* corrasponding to the bit.
* @test MISRA-C 2012 Compliant function.
* @return void
*/
void fibCounter(void)
{
uint8_t firstNum = 0U, secNum = 1U;
uint16_t sum = 1U;
/*
initializing the timer with the millis() values
*/
unsigned long timer = millis();
/*
This is done so that below code matches to the fibonacci
pattern and first number of sequence is 0.
*/
PORTD = firstNum;
sevseg.setNumber(firstNum, 1U);
/*
This while loop will run exactly for 1 seconds,
while loop is used because delay() can not be used,
so every single time when this function is called
seven segment shall show 0 for 1 second, because this
is first number in the sequence.
*/
while(true)
{
/*
This loop will break as soon as 1 second happens.
*/
if( (millis() - timer >= 1000U))
{
break;
}
/*
This API must be called to keep refreshing the
Seven Segment deisplay. If we call delay() we
will not be able to call this function and seven
segment will misbehave.
*/
sevseg.refreshDisplay();
}
/*
We expect this pattern 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377
loop will break once sum == 377, if we take uint8_t for sum variable
buffer overflow will happen.
*/
while(sum < 256U)
{
/*
Updating the seven segment every 1 second.
*/
if( (millis() - timer >= 1000U))
{
/*
Updating the timer value.
*/
timer = millis();
/*
This API is used to set the fibonacci number on the
seven segment diplay.
*/
sevseg.setNumber(sum, 1U);
/*
This is an edge case, as soon as sum is 233, we will show is
on seven segment display and do the summation for next cycle
but since the sum is now 377, while loop will break, and again
the function will be called from super loop, in this way we will
see 233 for very few milliseconds, since we can't use delay(), we
are using this method for 1 second of delay.
*/
if(sum == 233U)
{
unsigned long timer2 = millis();
while(true)
{
/*
This loop will break as soon as 1 second happens.
*/
if( (millis() - timer2 >= 1000U))
{
break;
}
/*
This API must be called to keep refreshing the
Seven Segment deisplay. If we call delay() we
will not be able to call this function and seven
segment will misbehave.
*/
sevseg.refreshDisplay();
}
}
/*
following the formula of fibonacci sequence and
*/
sum = firstNum + secNum;
firstNum = secNum;
secNum = sum;
}
/*
This API must be called to keep refreshing the
Seven Segment deisplay.
*/
sevseg.refreshDisplay();
}
}
/**
* @brief Initialization function for Arduino UNO.
* @test MISRA-C 2012 Compliant function.
* @return void
*/
void setup(void)
{
/*
DDRD stands for The Port D Data Direction Register.
Initializing the PORT D's all 8 bits to 1 it means that
all the pins PD0 - PD7 of arduino will be set as output.
In the model we have connected Seven segment pins to these pins.
*/
DDRD = 255U;
/*
PORTD stands for The Port D Data Register.
Every bit is set to LOW it means the connected
seven segment pins are initialized to low.
*/
PORTD = 0U;
/*
DDRB stands for The Port B Data Direction Register.
Initializing the PORT B's all 8 bits to 1 it means that
all the pins PB0 - PB7 of arduino will be set as output.
In the model we have connected Seven segment pins to these pins.
*/
DDRB = 255U;
/*
PORTB stands for The Port B Data Register.
Every bit is set to LOW it means the connected
seven segment pins are initialized to low.
*/
PORTB = 0U;
sevenSegmentSetup();
/*
This is our super loop since our software
architecture is bare metal we can not use
any OS and hence can't create multiple tasks.
In this super loop we keep calling the fibonacci
counter function.
*/
while(true)
{
fibCounter();
}
}