LPC824専用プログラム

Dependencies:   Ping SDFileSystem mbed-src

format.cpp

Committer:
lilac0112_1
Date:
2016-01-12
Revision:
6:79dc74e4926a
Parent:
5:afba6bf6c408
Child:
7:9b1ac6fbf32c

File content as of revision 6:79dc74e4926a:

#include "mbed.h"
#include "extern.h"

#ifdef SD_CARD
void Sd_System(void){
    
    char buf[BUFSIZE];
    uint16_t FileData[FDATA_NUM]={0}, num, data;
    uint8_t spi_num, h_byte, l_byte, val, packet;
    int i;
    FILE *fp;
    char *fname="/sd/mydir/sdtest.txt";
    char *dname="/sd/mydir";

    mkdir(dname, 0777);

    pc.printf("Hello World!\n");

    fp = fopen(fname, "r");
        if(fp == NULL) {
            error("Could not open file for read\n");
        }
        while(fgets(buf, sizeof(buf), fp) != NULL){
            if(buf[0] == '#') continue;
            
            num = atol(strtok(buf, ","));
            data = atol(strtok(NULL, "\r\n\0"));
            
            if(num<FDATA_NUM){
                FileData[num] = data;
                pc.printf("%d, %ld\n", num, FileData[num]);
            }
            else{
                continue;
            }
        }
        fclose(fp);
        
    while(1){
        
        val = nucleo.receive();
        if(!val) continue;
        
        spi_num = nucleo.read();
        h_byte = nucleo.read();
        l_byte = nucleo.read();
        
        if((spi_num&0x80)>>7 == 1){//writing to sd
            spi_num = spi_num & 0x7F;
            if(spi_num<FDATA_NUM){
                FileData[spi_num] = (h_byte<<8) | l_byte;
                //pc.printf("%d, %ld\n", num, FileData[num]);
                fp = fopen(fname, "w");
                for(i=0; i<FDATA_NUM; i++) fprintf(fp, "%d, %ld\n", i, FileData[i]);
                fclose(fp);
            }
        }
        else{//reading from sd
            spi_num = spi_num & 0x7F;
            if(spi_num<FDATA_NUM){
                h_byte = (FileData[spi_num] & 0xFF00)>>8;
                l_byte = (FileData[spi_num] & 0x00FF);

                packet = h_byte;
                nucleo.reply(packet);

                packet = l_byte;
                nucleo.reply(packet);
            }
        }
        //pc.printf("Goodbye World!\n");
    }
}
#endif /*SD_CARD*/

#ifdef ULTRA_SONIC
void Usw_System(void){
    uint8_t packet, val, order;
    uint16_t front_dis=0, rear_dis=0;
    
    while(1){
        front.Send();
        wait_ms(30);
        front_dis = front.Read_cm();
        
        rear.Send();
        wait_ms(30);
        rear_dis = rear.Read_cm();
        
        while(1){
            val = nucleo.receive();
            if(val){
                order = nucleo.read();
                switch(order){
                    case 0x01:
                        packet = (front_dis&0x00FF);break;
                    case 0x02:
                        packet = (front_dis&0xFF00)>>8;break;
                    case 0x03:
                        packet = (rear_dis&0x00FF);break;
                    case 0x04:
                        packet = (rear_dis&0xFF00)>>8;break;
                    default:
                        packet = 0x00;break;
                }
                nucleo.reply(packet);
            }
            else{
                break;
            }
        }
    }
}
#endif /*ULTRA_SONIC*/

#ifdef COLOR_SENSOR
void Color_System(void){
    uint8_t val, order, packet;
    uint16_t data;

    while(1){
        while(1){
            val = nucleo.receive();
            if(val){
                order = nucleo.read();
                order %= SENSOR_X_COLOR;
                data = ColorSensor[order].read_u16();
                packet = (data & 0xFF00)>>8;
                nucleo.reply(packet);
            }
            else{
                break;
            }
        }
    }
}
#endif /*COLOR_SENSOR*/

#ifdef IR_SENSOR
void Ir_System2(void){
    
    static uint8_t const START_BIT      = 0x04;
    static uint8_t const MODE_SINGLE    = 0x02;    // Single-ended mode
    //static uint8_t const MODE_DIFF      = 0x00;    // Differential mode
    
    int i;//ic
    int j;//ch
    uint8_t ch_num[3]={8, 8, 4};
    int command_high;
    int command_low;
    int high_byte;
    int low_byte;
    
    uint16_t ir_data[3][8]={0};
    //clock_t stime,etime;
    
    supply = 1;
    ir.frequency(1000000);
    
    for(i=0; i<IC_NUM; i++) cs[i]=1;
    while(1) {
        //stime = clock();
        for(i=0; i<IC_NUM; i++){//IC
            for(j=0; j<ch_num[i]; j++){//Ch
                command_high = START_BIT | MODE_SINGLE | ((j & 0x04) >> 2);
                command_low = (j & 0x03) << 6;
                
                cs[i] = 0;
                
                ir.write(command_high);
                high_byte = ir.write(command_low) & 0x0F;
                low_byte = ir.write(0);
                
                cs[i] = 1;
                wait_us(1);
                
                ir_data[i][j] = (high_byte << 8) | low_byte;
            }
        }
        //etime = clock();
        //pc.printf("%.4f\n", (float)(etime-stime)/CLOCKS_PER_SEC);
        pc.printf("%Value is ");
        pc.printf("%d\t", ir_data[1][2]);
        pc.printf("%d\t", ir_data[1][4]);
        pc.printf("%d\t", ir_data[0][0]);
        pc.printf("%d\t", ir_data[0][4]);
        pc.printf("\r\n");
    }
}
void Ir_System(void){
    
    static uint8_t const START_BIT      = 0x04;
    static uint8_t const MODE_SINGLE    = 0x02;    // Single-ended mode
    //static uint8_t const MODE_DIFF      = 0x00;    // Differential mode
    
    static uint8_t const IR_NUM[IC_NUM] = {8, 8, 4};
    
    uint8_t ic, ch;
    unsigned int command_high, command_low;
    unsigned int high_byte, low_byte;
    
    //uint8_t i;
    //uint8_t packet, val, order;
    
    uint16_t ir_data[IC_NUM][CH_NUM];
    
    supply = 1;
    ir.frequency(1000000);
    
    for(ic=0; ic<IC_NUM; ic++) cs[ic]=1;
    while(1) {
        for(ic=0; ic<IC_NUM; ic++){//IC
            for(ch=0; ch<IR_NUM[ic]; ch++){//Ch
                command_high = START_BIT | MODE_SINGLE | ((ch & 0x04) >> 2);
                command_low = (ch & 0x03) << 6;

                cs[ic] = 0;

                ir.write(command_high);
                high_byte = ir.write(command_low) & 0x0F;
                low_byte = ir.write(0);

                cs[ic] = 1;
                wait_us(1);
                ir_data[ic][ch] = ((high_byte << 4) | (low_byte >> 4))&0xFF;

            }
        }
        pc.printf("%d\t%d\t%d\t%d\t\r\n", ir_data[1][0], ir_data[1][1], ir_data[1][2], ir_data[1][3]);
        /*while(1){
            val = nucleo.receive();
            if(val){
                order = nucleo.read();
                packet = ir_data[order/10][order%10] & 0x00FF;
                nucleo.reply(packet);
                wait_us(1);
            }
            else{
                break;
            }
        }*/
        
    }
}
#endif /*IR_SENSOR*/