Функция не возвращается при присваивании массива значений - PullRequest
0 голосов
/ 31 мая 2018

Итак, у меня возникла странная проблема, заключающаяся в том, что в бесконечном цикле вызываемая функция не возвращается и поэтому останавливает цикл.

Каждый раз, когда цикл повторяет цикл, светодиод мигает:

 while (true) {
        loop.set(1); hwlib::wait_ms(100); loop.set(0); hwlib::wait_ms(100); // turns led on and off with 100ms delay
        hwlib::wait_ms(250); // arduino wait's 250ms before continuing 

        ipass.show(); // if this function is commented out the loop continues. If it is called the loop stalls here.
    }

Функция fucntion show () содержит следующий код.Когда я закомментирую все в цикле for, вызов функции возвращается как следует.Когда я сохраняю строки назначения массива, код останавливается.

void layer::show() {
    for (int i = 0; i < 8; i++) {
        pos_data = { 0 };

        pos_data[ i ] = 1;
        neg_data = matrix[ i ];

        for (int j = 0; j < 8; j++) {
            if (neg_data[j] == 0) { neg_data[j] = 1; } 
            else if (neg_data[j] == 1) { neg_data[j] = 0; }
        }

        write_registers();
        hwlib::wait_ms(5);
    }
}

Я думаю, что продолжение переназначения массива pos_data стоит слишком много памяти и поэтому не работает.

Нет ошибок или предупреждений во время компиляции.

Любая помощь приветствуется.

Для полного кода:
main.cpp

#include "hwlib.hpp"
#include "cube.hpp"
#include "layer.hpp"
#include "vector.hpp"


const int ON  = 1;
const int OFF = 0;

int main( void ){   
    // kill the watchdog
    WDT->WDT_MR = WDT_MR_WDDIS;

    auto pos_1 = hwlib::target::pin_out( hwlib::target::pins::d2 );
    auto neg_1 = hwlib::target::pin_out( hwlib::target::pins::d3 );
    auto clock = hwlib::target::pin_out( hwlib::target::pins::d4 );
    auto oe    = hwlib::target::pin_out( hwlib::target::pins::d5 );
    auto loop  = hwlib::target::pin_out( hwlib::target::pins::d6 );

    layer l1 ( 1, pos_1, neg_1, oe);

    cube ipass( clock, l1 );

    ipass.set_coordinate( vector(0, 7, 0), ON ); ipass.set_coordinate( vector(7, 7, 0), ON );  // 1 0 0 0 0 0 0 1
    ipass.set_coordinate( vector(1, 6, 0), ON ); ipass.set_coordinate( vector(6, 6, 0), ON );  // 0 1 0 0 0 0 1 0
    ipass.set_coordinate( vector(2, 5, 0), ON ); ipass.set_coordinate( vector(5, 5, 0), ON );  // 0 0 1 0 0 1 0 0 
    ipass.set_coordinate( vector(3, 4, 0), ON ); ipass.set_coordinate( vector(4, 4, 0), ON );  // 0 0 0 1 1 0 0 0
    ipass.set_coordinate( vector(4, 3, 0), ON ); ipass.set_coordinate( vector(3, 3, 0), ON );  // 0 0 0 1 1 0 0 0 
    ipass.set_coordinate( vector(5, 2, 0), ON ); ipass.set_coordinate( vector(2, 2, 0), ON );  // 0 0 1 0 0 1 0 0 
    ipass.set_coordinate( vector(6, 1, 0), ON ); ipass.set_coordinate( vector(1, 1, 0), ON );  // 0 1 0 0 0 0 1 0
    ipass.set_coordinate( vector(7, 0, 0), ON ); ipass.set_coordinate( vector(7, 0, 0), ON );  // 1 0 0 0 0 0 0 1

     ipass.clock(16);

    while (true) {
        loop.set(1); hwlib::wait_ms(100); loop.set(0); hwlib::wait_ms(100); 
        hwlib::wait_ms(250);

        ipass.show();

    }
}

layer.cpp

#include "hwlib.hpp"
#include "layer.hpp"
#include "vector.hpp"
#include "shift_register.hpp"

layer::layer( int layer_id, hwlib::pin_out &pos_pin, hwlib::pin_out &neg_pin, hwlib::pin_out &output_enable ):
    layer_id( layer_id ),
    pos_reg( shift_register( pos_pin, output_enable )), 
    neg_reg( shift_register( neg_pin, output_enable ))
{
    pos_reg.disable();
    neg_reg.disable();
}

layer::layer(): 
    layer_id( -1 ), pos_reg( shift_register() ), neg_reg( shift_register() )
{}

void layer::show() {
    for (int i = 0; i < 8; i++) {
        pos_data = { 0 };

        pos_data[ i ] = 1;
        neg_data = matrix[ i ];

        for (int j = 0; j < 8; j++) {
            if (neg_data[j] == 0) { neg_data[j] = 1; } 
            else if (neg_data[j] == 1) { neg_data[j] = 0; }
        }

        write_registers();
        hwlib::wait_ms(5);
    }
}

void layer::clock( int amount, int delay ) {
    auto clock = hwlib::target::pin_out( hwlib::target::pins::d4 );
    for ( int i = 0; i < amount; i ++) {
        clock.set( 1 );
        hwlib::wait_ms( delay / 2 );
        clock.set( 0 );
        hwlib::wait_ms( delay / 2 );
    }
}

void layer::set_coordinate( vector v, unsigned int value ) {
    // Transate coordinate system vector to array vector
    matrix[ 7-v.y ][ v.x ] = value;
}

void layer::write_registers() {
    pos_reg.disable(); neg_reg.disable();
    for (int i = 0; i < 8; i++) {
        pos_reg.set( pos_data [ 7-i ] );
        neg_reg.set( neg_data [ 7-i ] );
        clock();
    }
    clock();
    pos_reg.enable(); neg_reg.enable();
}

void layer::offer_data( unsigned int index ) {

}

layer.hpp

#ifndef LAYER_HPP
#define LAYER_HPP

#include "hwlib.hpp"
#include "shift_register.hpp"
#include "vector.hpp"
#include <array>

class layer {
private:
    int layer_id;
    std::array<int, 8> pos_data;
    std::array<int, 8> neg_data;
    shift_register pos_reg;
    shift_register neg_reg;
    std::array< std::array< int, 8>, 8> matrix;
public:
    layer( int layer_id, hwlib::pin_out &pos_pin, hwlib::pin_out &neg_pin, hwlib::pin_out &output_enable );
    layer();
    void set_coordinate( vector v, unsigned int value );
    void offer_data( unsigned int index );
    void show();
    void clock( int amount = 1, int delay = 0 );
    void write_registers();
};

#endif //LAYER_HPP
...