LPC824専用プログラム

Dependencies:   Ping SDFileSystem mbed-src

format.cpp

Committer:
lilac0112_1
Date:
2016-01-24
Revision:
8:a2eadba03040
Parent:
7:9b1ac6fbf32c
Child:
9:7f98d4415425

File content as of revision 8:a2eadba03040:

#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

//InterruptIn vol(P0_15);
void fx(void){
    uint8_t packet, val, order;
    uint8_t front_dis=0, rear_dis=0;
    front_dis = 200;
    rear_dis = 150;
    val = nucleo.receive();
    if(val==1){
        order = nucleo.read();
        if(order == 0xAB){
            packet = 0xAA;nucleo.reply(0xAA);
        }
        else{
            packet = 0x55;nucleo.reply(0x55);
        }
        pc.printf("SSSSSSSSSSSSSSSSS%d\r\n", order);
    }
    else{
        pc.printf("FFFF%d\r\n", 0);
    }
}
void fx2(void){
    uint16_t packet, val, order;
    uint16_t front_dis=0, rear_dis=0;
    front_dis = 200;
    rear_dis = 150;
    val = nucleo.receive();
    if(val==1){
        order = nucleo.read();
        if(order == 0xABCD){
            packet = 0xAAAA;nucleo.reply(0xAAAA);
        }
        else{
            packet = 0x5555;nucleo.reply(0x5555);
        }
        nucleo.reply(0x5555);
        pc.printf("SSSSSSSSSSSSSSSSS%d\r\n", order);
    }
    else{
        pc.printf("FFFF%d\r\n", 0);
    }
}
void Usw_System(void){
    int packet, val, order;
    uint16_t front_dis=0, rear_dis=0;
    
    nucleo.format(8, 3);
    nucleo.frequency(1000000);
    nucleo.reply(0x00);
    /*
    nucleo.format(16, 3);
    nucleo.frequency(1000000);
    nucleo.reply(0x0000);
    */
    vol.fall(&fx);
    //vol.fall(&fx2);
    
    led[0]=led[1]=1;
    while(1){
        /*
        front.Send();
        wait_ms(30);
        front_dis = front.Read_cm();
        if(front_dis>0xFF) front_dis=0xFF;
        
        rear.Send();
        wait_ms(30);
        rear_dis = rear.Read_cm();
        if(rear_dis>0xFF) rear_dis=0xFF;
        */
        
        
        front_dis = 200;
        rear_dis = 150;
        
        //pc.printf("%d\t%d\t\r\n", front_dis, rear_dis);
        //pc.printf("%d\r\n", vol.read());
        //fx();
        //fx2();
        while(0){
            val = nucleo.receive();
            if(val==1){
                order = nucleo.read();
                if(order == 0xAB){
                    packet = 0xAA;nucleo.reply(packet);
                }
                else{
                    packet = 0x55;nucleo.reply(packet);
                }
                
                pc.printf("SSSSSSSSSSSSSSSSS%d\r\n", order);
                break;
            }
            else{
                pc.printf("FFFF%d\r\n", 0);
                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_System(void){
    int i;//ic
    int j;//ch
    uint8_t ch_num[3]={8, 8, 4};
    uint16_t ir_data[IC_NUM][IR_NUM]={0};
    
    int order,packet,val;
    //nucleo...chipselect will be defined interruptpin.
    //nucleo...will be high clock and 16bit
    //nucleo.format(16, 0);
    //nucleo.frequency(5000000);
    nucleo.format(8, 0);
    nucleo.frequency(1000000);
    nucleo.reply(0x00);
    
    ir.format(8, 3);
    ir.frequency(1000000);
    int mod=1;
    
    for(i=0; i<IC_NUM; i++) cs[i]=1;
    while(1) {
        /*cs[1]=1;
        
        mod = ir.write(mod);
        //ir.write(0)&0xFF;
        
        cs[1]=0;
        wait_us(1);
        
        pc.printf("%Value is ");
        pc.printf("%d\t", mod);
        pc.printf("\r\n");
        */
        for(i=0; i<IC_NUM; i++){//IC
            for(j=0; j<ch_num[i]; j++){//Ch
               ir_data[i][j] = read_input(i, j);
            }
        }
        while(0){
            val = nucleo.receive();
            if(val==1){
                //order = nucleo.read();
                order = nucleo.read();
                //if(order != 0xAA) break;
                //packet = ir_data[order/10][order%10] & 0x00FF;
                //nucleo.reply(packet);
                packet = (ir_data[1][0] & 0x0FF0)>>4;nucleo.reply(packet);
                /*packet = (ir_data[1][1] & 0xFF00)>>8;nucleo.reply(packet);
                packet = (ir_data[1][2] & 0xFF00)>>8;nucleo.reply(packet);
                packet = (ir_data[1][3] & 0xFF00)>>8;nucleo.reply(packet);
                packet = (ir_data[1][4] & 0xFF00)>>8;nucleo.reply(packet);
                packet = (ir_data[1][5] & 0xFF00)>>8;nucleo.reply(packet);
                packet = (ir_data[1][6] & 0xFF00)>>8;nucleo.reply(packet);
                packet = (ir_data[1][7] & 0xFF00)>>8;nucleo.reply(packet);wait_us(1);*/
                
                pc.printf("SSSSSSSSSSSSSSSSS\r\n");
                break;
            }
            else{
                pc.printf("FFFFF\r\n");
                break;
            }
        }
        pc.printf("%Value is ");
        pc.printf("%d\t", ir_data[1][0]);
        pc.printf("%d\t", ir_data[1][1]);
        pc.printf("%d\t", ir_data[1][2]);
        pc.printf("%d\t", ir_data[1][3]);
        pc.printf("%d\t", ir_data[1][4]);
        pc.printf("%d\t", ir_data[1][5]);
        pc.printf("%d\t", ir_data[1][6]);
        pc.printf("%d\t", ir_data[1][7]);
        pc.printf("\r\n");
    }
}
int read_input(int ic, int channel)
{
    
    int command_high = START_BIT | MODE_SINGLE | ((channel & 0x04) >> 2);
    int command_low = (channel & 0x03) << 6;
    
    cs[ic] = 0;
    
    ir.write(command_high);
    int high_byte = ir.write(command_low) & 0x0F;
    int low_byte = ir.write(0);
    
    
    wait_us(1);
    cs[ic] = 1;
    
    return (high_byte << 8) | low_byte;//0x0000...0xFFFF
}
#endif /*IR_SENSOR*/