Hi, Welcome, We're going to talk about C++ in the Embedded systems field.
This is sponsored by GearsTech, that is a technology company I personally brand . It specialize in Embedded Systems and Artificial Intelligence in C++, C and Python.
So first slide is, we're going to recapture some concepts that we talked in previous videos
The video is called Embedded Systems Introduction, and those points are: first, the critical readability
It means that is expensive and it's potentially lethal to have any bug in the embedded systems
And the second one is that we have limited resources, that is critical to a computer in an airplane, or it might have limited processing cycle and it can be 60 MHz
It might have memory, really memory limits of 256 Kbytes, we might also be limited by the power supply, we might have power supply interruptions, so we need to handle it in a good way because the  main point is that the system might work uninterruptedly for years so, we need to have different ways to get the power.
And the third point is, that we're talking about the real time response that is essential, please remember that Real Time is the concept of having the output before the deadline, that is even more important that the correctness of the system.
The fourth is that a function must function uninterruptedly for years... and the fifth point is that is infeasible to have maintenance in different field or different environments were the embedded systems was set.
For example, there is a ship in a major storm of the middle of the Pacific, you won't send any technician in there
So, therefore the ES have the correctness concept that is producing  results every time in the right order using acceptable set of resources.
About fault tolerance, we must be careful to specify the set of conditions that the program is suppose to handle.
The third is that there is no downtime ,now defined by the deadlines, we have to run for a long time without changes to the software or intervention by a skilled operator.
The fourth is the real time constrains, that remember the hard real time if a certain response must occur before a deadline.
The fifth is the predictability and that an operation is predictable if it takes the same amount of time to execute every time it is executed on different equipments computers
And sixth is that the Embedded systems typically has to respond to various  events from the external world at the same time.
ok, the concept of predictability there is instructions that have to be compiled or executed in a specific fixed time, so the problem here is that the standard containers are not able to be used here because...
they are internally using the free store, so they're not predictable  either, calling string, vector, map, list, and so forth. They are very restricted in the ES world. We're not using also exceptions
in the ES, we're not able to use dynamic_cast. About memory management,we have three different ways to allocate the memory, the first one is static, that holds global variables, they are allocated by the linker and persistent as long as the program runs
The second one is stack or local variables in the function, they're allocated when we call call a function and freed when we return from the function,Stack memory can be a problem because it is possible to use too much of it,
The third one is dynamic that is Allocated by new and freed for possible reuse by deletion.
The problem with stack and dynamic memory allocation,with the stack we need to be able to demonstrate that a chain of calls will never be too long. Because we have memory constraints therefore  you might want to prefer an iterative implementation instead of a recursive implementation
The second point is that dynamic allocation is usually baned or severely restricted, in this case in many implementations of new, the time needed to allocate a new object will increase dramatically after many objects
had been allocated and deallocated, and we also have the case of fragile or fragment, that after the allocation and deallocation, the remaining of use of memory might be fragment into a lot of little holes of no used space that are useless because each hole is too small to hold another object of the kind used by the application.
You can see in this image that we have two different objects with different sizes, the orange is supposed to be smaller that the green one, so if we deallocated the orange one we're not able to fill it with a green one.
In this approach we're iterating, we might have a lot of waste of memory that in embedded systems is not correct.
We have two alternatives to the free store problems that we saw here, and the first one is a stack (Last In First Out) that is a data structure where you can allocate an arbitrary amount of memory and deallocate the last allocation (only) that in that function in the top, that's it ; that is, a stack can grow and shrink only at the top, right?
There can be no fragmentation, because there is not possibility to have “hole” between two allocations.
And the concept of pool,that is a collection of objects of the same size. There can be no fragmentation because all objects are of the same size, which there can be an smaller and bigger, in the pool they're the same size...
The next is bit, byte and word,and you can remember that a byte is eight bits and that is red by the left to the right
We represent a set of bits  in C++, and in the boolean is only using only one bit, but it takes the whole byte, in the char is a byte or 8 bits, in the short is 16 bits, in the int is typically 32 bits, but in embedded systems it uses 16 bits, and the long is using 32 or maybe have to use 64 bits
in the long long is the same that the long is 32 bits or 64 length. We have operation bit wise operation the OR is setting bits, the AND is testing bits, the XOR is toggling bits; and the left shift is moving the bits, the complement is changing the value of the whole byte  or the whole number
Here you can see the complement of a that is changing the whole byte from 0 to 1 and 1 to 0
in the XOR we have a certain bitset named B, and it's passing, it's toggling the relative of A and the result of the operation
The AND is testing if we have a one it is being passed to the result. The left shift is used to move the bit to the right most significant , and this is the way to have an easier way to access the value of that element,
In C++ we have a container called bitset,a bitset is a container with a fixed size as an array, so I was looking for a container to show bits in a lazy way  and this is the one that is approachable to that goal so.
The use of bitset is to represent and manipulate sets of bits and when writing into a bit set an input stream will look only for ones and zeros, if you have any character different that what is 1 ,0 that those they're going to be ignored.
And the bitset uses only unsigned int, and if you wanted to have any feature or characteristic of the bitset you can search in the documentation
Okay, so signed and unsigned int, obviously the bit representation, if the most significan bit is 1 , then is negative, otherwise it's positive. That's the answer of that question of how we distingush between positive and negative numbers? and
In this way we have the sign, it might be signed... and if we're moving the bytes to the right in the signed number, the one remains instead of using the 0
In unsigned int, if we're using the right shift, we're having zeros inserted, so we talked about two complement representation for negative numbers
and this is the complement of a positive number and then you add an one to that number.
So wants to have the formula of the range, the maximum number that you can represent  in a specific amount of bits  and in this ejemplo we can have 2 powered to 15 minus 1
for intergers, and the result is 32767, that's the largest number that you can represent, as a review the shift moves the bits to the right most; the mask eliminates a bit that we don't want to pass
the set, it sets a new byte and the XOR is toggling the bits. Now we have the concept of bitfields that is a mixture of different types in a chunk of memory, this is represented by a struct
and the two points with a followed number is a limiter, so we're limiting the size of each member in this case we have a 22 bits of an unsigned int CCA with three bits, if we don't have a number like in this part
the identifier of that member is that means that part is skipped so we don't have three bits, this is useful to macros, to frames of messages passed by bit values
so that's it, we were base in the programming, principles and practice using C++ by Bjarne Stroustrup, so I think that you enjoyed so thank you and see you
