AIチップをmbedから駆動するためのプログラムです.

Dependencies:   mbed

matrix_const.hpp

Committer:
toriten1024
Date:
2019-03-16
Revision:
1:18e8ead6f188

File content as of revision 1:18e8ead6f188:

#include "mbed.h"

#ifndef INCLUDE_CONST_MATRIX
#define INCLUDE_CONST_MATRIX

namespace mat
{   
int hogehoge;

class ConstMatrix
{

  public:
    const unsigned int *data;
    int height;//if height == 0 then dimension y is zero
    int width;// 
    int channel;//
    int insert_index;//
    
 
    
    //assert
    void assert(int flag){
        if(! flag){
            printf("error has happend\n\r");
        }    
    }
    /******************************************************************************************
     * Calcrate index from x,y,x axis
     * ***************************************************************************************/
    int calc_pos(int x, int y, int d)
    {
        int pos = 0;
        pos += d * (width * height);
        pos += y * width;
        pos += x;
        return pos;
    }

    // Constructor Create Matrix Instance
    void initialize(int width, int height, int channel)
    {
        assert(channel > 0 && height > 0 && width > 0);
        this->channel = channel;
        this->height = height;
        this->width = width;
        this->insert_index = 0;
    }

    /******************************************************************************************
     * Create 1D Matrix data, like a vector
     *  args
     *  width vector length
     * ***************************************************************************************/
    ConstMatrix(int width)
    {
        assert(width > 0);
        initialize(width, 1, 1);
    }
    /******************************************************************************************
     * Create 2D Matrix data
     *  args
     *  width : matrix width
     *  height : matrix height
     * ***************************************************************************************/
    ConstMatrix(int width, int height)
    {
        assert(width > 0 && height > 0);
        initialize(width, height, 1);
    }

    /******************************************************************************************
     * Create 3D Matrix data
     *  args
     *  width : matrix width
     *  height : matrix height
     *  channel : matrix channel, in other word matrix depth
     * ***************************************************************************************/
    ConstMatrix(int width, int height, int channel)
    {
        assert(width > 0 && height > 0 && channel > 0);
        initialize(width, height, channel);
    }




    /******************************************************************************************
     * Opt out Matrix datas
     * ***************************************************************************************/
    void show(void)
    {
        for (int i = 0; i < this->channel; i++)
        {
            if (i > 1){
                printf("channel %d",i); 
                printf("\n\r");
            }
            printf("[");
            for (int j = 0; j < this->height; j++)
            {
                printf("[");
                for (int k = 0; k < this->width; k++)
                {
                    int ptr = (i * this->width * this->height) + (j * this->width) + k;
                    Fix18 tmp(data[ptr]);
                    printf("%F ",(double)tmp.to_double());
                }
                printf("]");
                if (j < this->height - 1)
                {
                    printf("\n\r");
                }
            }
            printf("]"); 
            printf("\n\r");
        }
    }
    /******************************************************************************************
     * Opt out Matrix datas in 1/0
     * ***************************************************************************************/
    void show_bin(void)
    {
        for (int i = 0; i < this->channel; i++)
        {
            if (i > 1){
                printf("channel %d",i ); 
                printf("\n\r");
            }
            printf("[");
            for (int j = 0; j < this->height; j++)
            {
                printf("[");
                for (int k = 0; k < this->width; k++)
                {
                    int ptr = (i * this->width * this->height) + (j * this->width) + k;
                    printf(" %d",((data[ptr] > 0) ? 1 : 0));
                }
                printf("]");
                if (j < this->height - 1)
                {
                    printf("\n\r");
                }
            }
            printf("]"); 
            printf("\n\r");
        }
    }

    /******************************************************************************************
     * opt out Matrix Dimensions Lengths
     * ***************************************************************************************/
    void shape(void)
    {
        if (this->height == 0 || this->channel == 0)
        {
            printf("%d",width); 
            printf("\n\r");
        }
        else if (this->channel == 0)
        {
            printf("%d,%d",this->width,this->height); 
            printf("\n\r");
        }
        else
        {
            printf("%d,%d,%d",this->width,this->height,this->channel); 
            printf("\n\r");
        }
    }

};



} // namespace mat
#endif