Solved

Convert C++ to acceptable Arduino C++

Posted on 2014-09-08
1
119 Views
Last Modified: 2014-12-27
I have the following working C++ code in GCC and want to convert it to its Arduino C++ compatible version to produce the output listed just below this code:

#include <vector>
#include <iostream>
#include <algorithm>

void fadeOn(int maxvalue, int maxlength) {
    int counter;
    int maxcounter = 1;
    int vtemp = 0;
    
    std::vector<int> v{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
    
    for (counter=1; counter < maxlength + maxvalue; counter++) {
      // simple rotation to the right
      
      std::cout << "simple rotate right " << counter << ": ";
      for (int n: v)
          std::cout << n << ' ';
      std::cout << '\n';
      
      vtemp = v[0];
      
      std::rotate(v.rbegin(), v.rbegin() + 1, v.rend());
      
      if (maxcounter < maxvalue) {
        v[0] = vtemp + 1;
      } else {
        v[0] = maxvalue;
      }
      
      maxcounter++;
    }
}

int main()
{
    fadeOn(7,13);
}

Open in new window


CURRENT OUTPUT of above code:

simple rotate right 1: 1 0 0 0 0 0 0 0 0 0 0 0 0
simple rotate right 2: 2 1 0 0 0 0 0 0 0 0 0 0 0
simple rotate right 3: 3 2 1 0 0 0 0 0 0 0 0 0 0
simple rotate right 4: 4 3 2 1 0 0 0 0 0 0 0 0 0
simple rotate right 5: 5 4 3 2 1 0 0 0 0 0 0 0 0
simple rotate right 6: 6 5 4 3 2 1 0 0 0 0 0 0 0
simple rotate right 7: 7 6 5 4 3 2 1 0 0 0 0 0 0
simple rotate right 8: 7 7 6 5 4 3 2 1 0 0 0 0 0
simple rotate right 9: 7 7 7 6 5 4 3 2 1 0 0 0 0
simple rotate right 10: 7 7 7 7 6 5 4 3 2 1 0 0 0
simple rotate right 11: 7 7 7 7 7 6 5 4 3 2 1 0 0
simple rotate right 12: 7 7 7 7 7 7 6 5 4 3 2 1 0
simple rotate right 13: 7 7 7 7 7 7 7 6 5 4 3 2 1
simple rotate right 14: 7 7 7 7 7 7 7 7 6 5 4 3 2
simple rotate right 15: 7 7 7 7 7 7 7 7 7 6 5 4 3
simple rotate right 16: 7 7 7 7 7 7 7 7 7 7 6 5 4
simple rotate right 17: 7 7 7 7 7 7 7 7 7 7 7 6 5
simple rotate right 18: 7 7 7 7 7 7 7 7 7 7 7 7 6
simple rotate right 19: 7 7 7 7 7 7 7 7 7 7 7 7 7

Now I would like to have the same outcome with Arduino C++ but I don't want to use STL libraries because I have tried and it simply makes a mess.

Any ideas?  Queues, circle buffers, ring buffers, etc... ?
0
Comment
Question by:wfninpa
1 Comment
 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
ID: 40311476
you may try the following code not using stl.

#include <stdio.h>

void fadeOn(int maxvalue, int maxlength) {
    int counter;
    int maxcounter = 1;
    int vtemp = 0;
    
    int v[] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
    
    for (counter=1; counter < maxlength + maxvalue; counter++) {
      // simple rotation to the right
      
     printf ("simple rotate right %d", counter);
     for (int n = 0; n < 12; ++n)
          printf("%d ", n);
      printf("'\n");

      
      vtemp = v[0];
      for (int r = 11; r > 0; --r)
            v[r] = v[r-1];
      
      if (maxcounter < maxvalue) {
        v[0] = vtemp + 1;
      } else {
        v[0] = maxvalue;
      }
      
      maxcounter++;
    }
}

Open in new window


Sara
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now