Page 2 of 2

Re: Arduino Micro Controllers

Posted: August 6th, 2019, 4:09 am
by albinopapa
You don't need the m_count since the count is part of Array's type, therefore you also don't need the constructor Array(). The size() function can just return the template parameter count or if supported, you can make a static constexpr m_count = count and return the constexpr m_count. I'd also make the size() function constexpr if supported by the Arduino compiler as well and maybe noexcept lol.

Code: Select all

template<typename T, unsigned int count>
class Array
{
   static constexpr unsigned int m_count = count;
public:
   // convenience functions
   constexpr T& operator[]( unsigned int _index )noexcept{
      assert( _index < m_count && "Index out of range" );
      return elements[ _index ];
   }
   constexpr T const& operator[]( unsigned int _index )const noexcept{
      assert( _index < m_count && "Index out of range" );
      return elements[ _index ];
   }
   constexpr unsigned int size()const noexcept {return m_count;}
   T elements[ count ];
};
At least this way you can use the Array class like an array:
Array<int, 10> ints_A;
Array<int, 10> ints_B;

Old way: ints_A.elements[ 0 ] = 3;
New way: ints_B[ 0 ] = 3;
Plus, this way you get some debugging help if you try accessing out of bounds.

Now you just need to make an ArrayIterator, ArrayConstIterator, Array::begin() and Array::end() so you can use range based for loops.

Re: Arduino Micro Controllers

Posted: August 7th, 2019, 1:04 am
by MrGodin
Thanks buddy, I'll use this ;)

Re: Arduino Micro Controllers

Posted: August 7th, 2019, 1:19 am
by MrGodin
So i left everything connected to my ATX power supply then went to work. I came back and the fan was running (as expected) and all is well. Once the code is uploaded to the Arduino It'll run so long as it has power. If the power goes out ... the code still resides in the Arduino chip. Cool or what ;)..

Re: Arduino Micro Controllers

Posted: August 7th, 2019, 1:25 am
by MrGodin
This compiled on the Arduino IDE

Code: Select all

template<typename T, unsigned int count>
class Array
{
   static constexpr unsigned int m_count = count;
public:
   // convenience functions
   constexpr T& operator[]( unsigned int _index )noexcept{
      assert( _index < m_count && "Index out of range" );
      return elements[ _index ];
   }
  
   
   constexpr unsigned int size()const noexcept {return m_count;}
   T elements[ count ];
};

Re: Arduino Micro Controllers

Posted: August 7th, 2019, 6:23 am
by albinopapa
Cool, thanks for sharing.

I like writing library stuff like this, so if you ever want a container with iterators, let me know if you don't feel like writing it out yourself.

Re: Arduino Micro Controllers

Posted: August 28th, 2019, 12:49 am
by MrGodin
so,... I got 3 ATMEGA328p chips and a few 16 mhz crystals, some ceramic capacitors, loaded up the bootloaders and whammo, all ready to have my microchips in my circuits stand alone. getting funner.. errr.. more fun ? haha ;)

Re: Arduino Micro Controllers

Posted: August 28th, 2019, 12:55 am
by MrGodin
Oh and got a couple 16x2 LCD displays to show my photo resistor (light sensor) and temperature sensor data