示例#1
0
/*******************************************************************************
  * @函数名称		log_write
  * @函数说明		write log, log base frame
  * @输入参数		file pointer
  * @输出参数		无
  * @返回参数		是否成功
*******************************************************************************/
uint8_t log_write(FIL *fil, uint8_t warning)
{
    FRESULT rc;

    UINT bw;

    uint8_t src[40] = {0};

    uint8_t time_data[40] = {0};

    get_curr_time(time_data,"分");
    strcat(src,"0:/");
    strcat(src,time_data);
    strcat(src,".txt");

    /* 写入文件 */
    rc = f_open(fil,src,FA_WRITE | FA_READ | FA_OPEN_ALWAYS);
    ERROR_TRACE(rc);
    if(fil->fsize==0){
        rc = f_write(fil, "           时间           总电压 电流 电池温度                        PCB温度 单体电压0.01V                                                   剩余容量 异常\r\n",
                   strlen("           时间           总电压 电流 电池温度                        PCB温度 单体电压0.01V                                                   剩余容量 异常")+2, &bw);
        ERROR_TRACE(rc);
    }

    log_write_content(fil,warning);

    rc = f_close(fil);
    ERROR_TRACE(rc);
    
    return true;
}
示例#2
0
void fatfs_test(void)
{
    FRESULT rc;//error number

    FIL fil;
    
    rc = f_mkdir("0:/一");
    if(rc)  printf("f_mkdir error\n");
    else    printf("f_mkdir Ok!\n");

/******************************************************************************/
    DIR path;

    FILINFO file_info;
#if _USE_LFN
        file_info.lfsize = 20  + 1;
        file_info.lfname = mymalloc(SRAMEX, file_info.lfsize);
#endif
    rc = f_opendir(&path,"/");
    ERROR_TRACE(rc);
    
    printf("共有%d个文件夹\n",dir_totle(&path,&file_info));
    
/******************************************************************************/    
    uint8_t src[30] = {0};
    get_curr_time(src,NULL);
    printf("时间:%s\n",src);

    log_write(&fil,0);

    f_closedir(&path);
#if _USE_LFN
    myfree(SRAMEX, file_info.lfname);
#endif

    //总电压0.01v 充放电电流 电池温度 单体电压 剩余容量 循环次数 日发电量 总发电量 异常记录(异常类型 发生时间 发生时以上各项目参数)
}
示例#3
0
文件: main.c 项目: SunnyQ/cmu
int main(int argc, char **argv) {
  char *strandsFilePath = NULL;
  char *centroidsFilePath = NULL;
  int numStrands = -1;
  int numCentroids = -1;
  int lenStrand = -1;
  int opt, i, numItrs;
  double currTime, ioWallTime, kmeansWallTime;

  int numProcs, rank, namelen;
  char processor_name[MPI_MAX_PROCESSOR_NAME];

  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
  MPI_Get_processor_name(processor_name, &namelen);

  while ((opt = getopt(argc, argv, "f:p:c:l:i:")) != -1) {
    switch (opt) {
      case 'f': 
        strandsFilePath = optarg;
        break;
      case 'i': 
        centroidsFilePath = optarg;
        break;
      case 'c': 
        numCentroids = atoi(optarg);
        break;
      case 'p': 
        numStrands = atoi(optarg);
        break;
      case 'l': 
        lenStrand = atof(optarg);
        break;
      case '?': 
        usage(argv[0]);
        break;
      default: 
        usage(argv[0]);
        break;
    }
  }

  if (strandsFilePath == NULL || 
      numStrands <= 0 ||
      numCentroids <= 0 || 
      lenStrand < 0) {
    usage(argv[0]);
    MPI_Finalize();
    exit(-1);
  }

  ioWallTime = get_curr_time();

  MPI_Barrier(MPI_COMM_WORLD);

  Strand *strands = NULL;
  Strand *centroids = NULL;
  char *strandsMem = NULL;
  char *centroidsMem = NULL;

  if (rank == 0) {

    // load strands 
    init_memory_space(&strands, &strandsMem, numStrands, lenStrand);

    int numStrandsLoaded = load_strands(strands, strandsFilePath, 
        numStrands, lenStrand);
    if (numStrandsLoaded != numStrands) {
      fprintf(stderr, "Error: could only load %d strands from sources\n", 
          numStrandsLoaded);
      MPI_Finalize();
      exit(-1);
    }

    // load or generate initial centroids
    init_memory_space(&centroids, &centroidsMem, numCentroids, lenStrand);
    
    if (centroidsFilePath != NULL) {  // load centroids from given file
      int numCentroidsLoaded = load_strands(centroids, centroidsFilePath, 
          numCentroids, lenStrand);
      if (numCentroidsLoaded != numCentroids) {
        fprintf(stderr, "Error: could only load %d centroids from sources\n",
            numCentroidsLoaded);
        MPI_Finalize();
        exit(-1);
      }
    } else {  // randomly choose centroids
      random_choose_centroids(strands, centroids, 
        numStrands, numCentroids, lenStrand);
    }

    // distributed strands 
    numStrands = distribute_strands(strands, numStrands, lenStrand,
        numProcs, MPI_COMM_WORLD);

    // destroy distributed strands
    strandsMem = realloc(strandsMem, numStrands * lenStrand * sizeof(char));
    assert(strandsMem != NULL);
    strands = realloc(strands, numStrands * sizeof(Strand));
    assert(strands != NULL);

  } else {

    numStrands = get_portion_size(numStrands, numProcs, rank);

    // receive strands 
    init_memory_space(&strands, &strandsMem, numStrands, lenStrand);

    int numStrandsReceived = receive_strands(strands, numStrands, lenStrand,
        rank, MPI_COMM_WORLD);

    if (numStrandsReceived != numStrands) {
      fprintf(stderr, "Error: could only receive %d strands from root\n", 
          numStrandsReceived);
      MPI_Finalize();
      exit(-1);
    }

    // load or generate initial centroids
    init_memory_space(&centroids, &centroidsMem, numCentroids, lenStrand);
  }

  currTime = get_curr_time();
  ioWallTime = currTime - ioWallTime;

  kmeansWallTime = get_curr_time();

  // broadcast centroids
  MPI_Bcast(centroidsMem, numCentroids * lenStrand, MPI_CHAR, 0, MPI_COMM_WORLD);

  numItrs = do_kmeans(strands, centroids, 
      numStrands, numCentroids, lenStrand,
      rank, MPI_COMM_WORLD);

  currTime = get_curr_time();
  kmeansWallTime = currTime - kmeansWallTime;

  if (rank == 0) {
    printf("========================================\n");
    printf("Number of Iterations: %d\n", numItrs);
    printf("========================================\n");
    printf("K-Means Centroids: \n");
    for (i = 0; i < numCentroids; i++) {
      print_strand(centroids[i], lenStrand);
    }
    printf("========================================\n");
    printf("IO wall time: \t\t%10.4f sec\n", ioWallTime);
    printf("KMeans wall time: \t%10.4f sec\n", kmeansWallTime);
    printf("Total wall time: \t%10.4f sec\n", (kmeansWallTime + ioWallTime));
    printf("========================================\n");
  }

  free(centroidsMem);
  free(centroids);
  free(strandsMem);
  free(strands);
  MPI_Finalize();
  return 0;
}
示例#4
0
/*******************************************************************************
  * @函数名称		log_write_content
  * @函数说明		write log content
  * @输入参数		file pointer
  * @输出参数		无
  * @返回参数		是否成功
*******************************************************************************/
static uint8_t log_write_content(FIL *fil,uint8_t warning)
{
    FRESULT rc;
    
    UINT bw;

    uint8_t src[200] = {0};

    uint8_t temp[40] = {0};

    uint8_t time_data[40] = {0};

    uint16_t temp_num = 0;
    uint16_t for_temp = 0;
    uint16_t write_len = 0;
    uint16_t i;

    get_curr_time(time_data,"分");
    strcat(src,"0:/");
    strcat(src,time_data);
    strcat(src,".txt");

    rc = f_lseek(fil, fil->fsize);
    ERROR_TRACE(rc);

    memset(src,0,sizeof(src));
    memset(time_data,0,sizeof(time_data));

    get_curr_time(time_data,"秒");
    uint8_t space;
    space = 26 - strlen(time_data);

    memset(src,0x20,space);
    strcat(src,time_data);
    write_len = 26;

    /* total voltage */
    strcat(src, " ");
    if((bms.Voltage_All/100)==0){
        strcat(src, "000");
    } else {
        itoa((bms.Voltage_All/100), temp);
        strcat(src, temp);
        memset(temp,0,sizeof(temp));
    }
    write_len += 4;
    strcat(src, "  ");write_len += 2;
    
    /* Charge-DisCharge Currents */
    strcat(src, " ");
    temp_num = (30000 - bms.Currents.Currents);

    if (temp_num == 30000) {
        strcat(src, "000");
    } else {
        temp_num /= 100;
        if (temp_num > 0) {
            strcat(src,"+");
            itoa(temp_num,temp);
            strcat(src, temp);
        } else {
            abs(temp_num);
            strcat(src, "-");
            itoa(temp_num, temp);
            strcat(src, temp);
        };
        
        space = 2 - strlen(temp);
        
        for(i=0; i<space; i++){
            strcat(src, " ");
        }
    }
    write_len += 4;
    strcat(src, " ");write_len += 1;
    
    /* TEMP */
    for (; for_temp<10; for_temp++) {
        strcat(src," ");
        if (bms.Temperature[for_temp].Temperature <= 0 || 
            bms.Temperature[for_temp].Temperature >= 150) {
            strcat(src,"000");
        } else {
            temp_num = (bms.Temperature[for_temp].Temperature-50);
            if(temp_num > 0) {
                strcat(src, "+");
                itoa(temp_num, temp);
                strcat(src, temp);
            } else {
                abs(temp_num);
                strcat(src, "-");
                itoa(temp_num, temp);
                strcat(src, temp);
            };
            space = 2 - strlen(temp);
            
            for(i=0; i<space; i++){
                strcat(src, " ");
            }
        }
        write_len += 4;
    }

    /* an Voltage */
    for (for_temp = 0; for_temp<16; for_temp++) {
        strcat(src, " ");
        if (bms.Voltage[for_temp].Voltage/10 == 0) {
            strcat(src, "000");
        } else {
            itoa((bms.Voltage[for_temp].Voltage/10), temp);
            strcat(src, temp);
        }
        write_len += 4;
    }

    /* Capacity */
    strcat(src," ");
    if (bms.Capacity.Capacity == 0) {
        strcat(src, "0000");
    } else {
        itoa(bms.Capacity.Capacity, temp);
        strcat(src, temp);
    }
    write_len += 5;
    
    if (warning) {
        strcat(src, "       1");
        write_len += 8;
    }

    strcat(src,"\r\n");
    write_len += 2;

    rc = f_write(fil, src, write_len, &bw);
    ERROR_TRACE(rc);

    return true;
}
示例#5
0
/**
 * 功能:添加分享资源
 * 参数:resource_share - 分享资源结构体
 *	返回值:添加成功返回0,失败返回1,数据库内部错误返回-1
 */
int add_share_resource(const struct resource_share *res)
{
	
	MYSQL *mysql = NULL;
	char query_str[512];
	int affect_rows;
	if(mysql == NULL) {
		mysql = open();
	}
	
	/* insert into tbl_resoure */
	sprintf(query_str,"INSERT INTO tbl_resource(f_res_name,\
		f_res_size,f_res_md5,f_res_piececount) VALUES('%s',\
		'%s','%s','%d')", res->name,res->size,res->md5,res->piececount);
	if(mysql_query(mysql,query_str)) {
		fprintf(stderr,"Error: %s\n",mysql_error(mysql));
	}
	
	/* get sharetime */
	char *sharetime;
	sharetime = get_curr_time();

	/* insert into tbl_resource_owner */
	if(strlen(res->mac) <= 0) {

		mysql_close(mysql);
		return 0;
	}
	memset(query_str,'\0',sizeof(query_str));
	sprintf(query_str,"INSERT INTO tbl_resource_owner(f_res_md5,\
		f_res_owner,f_res_sharetime) VALUES('%s','%s','%s')",res->md5
		,res->mac,sharetime);
	if(mysql_query(mysql,query_str)) {
		fprintf(stderr,"Error: %s\n",mysql_error(mysql));
	}


	/* insert into tbl_resource_tags */
	if(strlen(res->tag) <= 0) {

		mysql_close(mysql);
		return 0;
	}
	const char *sp = TAG_SEPERATOR;
	char *tag = NULL;
	char key[512];

	int tag_failed = 0;
	strcpy(key,res->tag);
	tag = strtok(key,sp);
	while(tag) {
		memset(query_str,'\0',sizeof(query_str));
		sprintf(query_str,"INSERT INTO tbl_resource_tags(f_res_md5,\
			f_res_tags) VALUES('%s','%s')",res->md5,tag);
		if(mysql_query(mysql,query_str)) {
			fprintf(stderr,"Error: %s\n",mysql_error(mysql));

			mysql_close(mysql);
			return -1;
		}
		affect_rows = mysql_affected_rows(mysql);
		if(affect_rows < 0) {
			tag_failed = 1;
		}

		tag = strtok(NULL,sp);
	}

	mysql_close(mysql);

	return tag_failed == 1 ? -1:0;
}