所以我遇到了一个奇怪的问题,即在无限循环中,被调用的函数没有返回,因此停止循环。
每次循环循环LED闪烁:
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