初始版本

This commit is contained in:
yueming
2021-09-01 14:03:46 +08:00
parent 4e0dc1ed9d
commit 975adfb602
123 changed files with 59901 additions and 0 deletions

View File

@@ -0,0 +1,727 @@
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
#include <limits.h>
#include <ctype.h>
#include "cJSON.h"
static const char *ep;
const char *cJSON_GetErrorPtr(void)
{return ep;}
static int cJSON_strcasecmp(const char *s1,const char *s2){
if (!s1) return (s1==s2)?0:1;if (!s2) return 1;
for(; tolower(*s1) == tolower(*s2); ++s1, ++s2) if(*s1 == 0) return 0;
return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2);
}
static void *(*cJSON_malloc)(size_t sz) = malloc;
static void (*cJSON_free)(void *ptr) = free;
static char* cJSON_strdup(const char* str){
size_t len;
char* copy;
len = strlen(str) + 1;
copy = (char *)cJSON_malloc(len);
if (!copy)
return 0;
memcpy(copy,str,len);
return copy;
}
void cJSON_InitHooks(cJSON_Hooks* hooks){
if (!hooks) { /* Reset hooks */
cJSON_malloc = malloc;
cJSON_free = free;
return;
}
cJSON_malloc = (hooks->malloc_fn)?hooks->malloc_fn:malloc;
cJSON_free = (hooks->free_fn)?hooks->free_fn:free;
}
/* Internal constructor. */
static cJSON *cJSON_New_Item(void){
cJSON* node = (cJSON*)cJSON_malloc(sizeof(cJSON));
if (node) memset(node,0,sizeof(cJSON));
return node;
}
/* Delete a cJSON structure. */
void cJSON_Delete(cJSON *c){
cJSON *next;
while (c)
{
next=c->next;
if (!(c->type&cJSON_IsReference) && c->child) cJSON_Delete(c->child);
if (!(c->type&cJSON_IsReference) && c->valuestring) cJSON_free(c->valuestring);
if (!(c->type&cJSON_StringIsConst) && c->string) cJSON_free(c->string);
cJSON_free(c);
c=next;
}
}
/* Parse the input text to generate a number, and populate the result into item. */
static const char *parse_number(cJSON *item,const char *num){
double n=0,sign=1,scale=0;int subscale=0,signsubscale=1;
if (*num=='-') sign=-1,num++; /* Has sign? */
if (*num=='0') num++; /* is zero */
if (*num>='1' && *num<='9') do n=(n*10.0)+(*num++ -'0'); while (*num>='0' && *num<='9'); /* Number? */
if (*num=='.' && num[1]>='0' && num[1]<='9') {num++; do n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');} /* Fractional part? */
if (*num=='e' || *num=='E') /* Exponent? */
{ num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++; /* With sign? */
while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0'); /* Number? */
}
n=sign*n*pow(10.0,(scale+subscale*signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */
item->valuedouble=n;
item->valueint=(int)n;
item->type=cJSON_Number;
return num;
}
static int pow2gt (int x) { --x; x|=x>>1; x|=x>>2; x|=x>>4; x|=x>>8; x|=x>>16; return x+1; }
typedef struct {char *buffer; int length; int offset; } printbuffer;
static char* ensure(printbuffer *p,int needed){
char *newbuffer;int newsize;
if (!p || !p->buffer) return 0;
needed+=p->offset;
if (needed<=p->length) return p->buffer+p->offset;
newsize=pow2gt(needed);
newbuffer=(char*)cJSON_malloc(newsize);
if (!newbuffer) {cJSON_free(p->buffer);p->length=0,p->buffer=0;return 0;}
if (newbuffer) memcpy(newbuffer,p->buffer,p->length);
cJSON_free(p->buffer);
p->length=newsize;
p->buffer=newbuffer;
return newbuffer+p->offset;
}
static int update(printbuffer *p){
char *str;
if (!p || !p->buffer) return 0;
str=p->buffer+p->offset;
return p->offset+strlen(str);
}
/* Render the number nicely from the given item into a string. */
static char *print_number(cJSON *item,printbuffer *p){
char *str=0;
double d=item->valuedouble;
if (d==0)
{
if (p) str=ensure(p,2);
else str=(char*)cJSON_malloc(2); /* special case for 0. */
if (str) strcpy(str,"0");
}
else if (fabs(((double)item->valueint)-d)<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN)
{
if (p) str=ensure(p,21);
else str=(char*)cJSON_malloc(21); /* 2^64+1 can be represented in 21 chars. */
if(str) sprintf(str,"%d",item->valueint);
}
else
{
if (p) str=ensure(p,64);
else str=(char*)cJSON_malloc(64); /* This is a nice tradeoff. */
if (str)
{
if (fabs(floor(d)-d)<=DBL_EPSILON && fabs(d)<1.0e60)sprintf(str,"%.0f",d);
else if (fabs(d)<1.0e-6 || fabs(d)>1.0e9) sprintf(str,"%e",d);
else sprintf(str,"%f",d);
}
}
return str;
}
static unsigned parse_hex4(const char *str){
unsigned h=0;
if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;
h=h<<4;str++;
if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;
h=h<<4;str++;
if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;
h=h<<4;str++;
if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;
return h;
}
/* Parse the input text into an unescaped cstring, and populate item. */
static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
static const char *parse_string(cJSON *item,const char *str){
const char *ptr=str+1;char *ptr2;char *out;int len=0;unsigned uc,uc2;
if (*str!='\"') {ep=str;return 0;} /* not a string! */
while (*ptr!='\"' && *ptr && ++len) if (*ptr++ == '\\') ptr++; /* Skip escaped quotes. */
out=(char*)cJSON_malloc(len+1); /* 这大概是我们需要的长度. */
if (!out) return 0;
ptr=str+1;ptr2=out;
while (*ptr!='\"' && *ptr)
{
if (*ptr!='\\') *ptr2++=*ptr++;
else
{
ptr++;
switch (*ptr)
{
case 'b': *ptr2++='\b'; break;
case 'f': *ptr2++='\f'; break;
case 'n': *ptr2++='\n'; break;
case 'r': *ptr2++='\r'; break;
case 't': *ptr2++='\t'; break;
case 'u': /* transcode utf16 to utf8. */
uc=parse_hex4(ptr+1);ptr+=4; /* get the unicode char. */
if ((uc>=0xDC00 && uc<=0xDFFF) || uc==0) break; /* check for invalid. */
if (uc>=0xD800 && uc<=0xDBFF) /* UTF16 surrogate pairs. */
{
if (ptr[1]!='\\' || ptr[2]!='u') break; /* missing second-half of surrogate. */
uc2=parse_hex4(ptr+3);ptr+=6;
if (uc2<0xDC00 || uc2>0xDFFF) break; /* invalid second-half of surrogate. */
uc=0x10000 + (((uc&0x3FF)<<10) | (uc2&0x3FF));
}
len=4;if (uc<0x80) len=1;else if (uc<0x800) len=2;else if (uc<0x10000) len=3; ptr2+=len;
switch (len) {
case 4: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
case 3: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
case 2: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
case 1: *--ptr2 =(uc | firstByteMark[len]);
}
ptr2+=len;
break;
default: *ptr2++=*ptr; break;
}
ptr++;
}
}
*ptr2=0;
if (*ptr=='\"') ptr++;
item->valuestring=out;
item->type=cJSON_String;
return ptr;
}
/* Render the cstring provided to an escaped version that can be printed. */
static char *print_string_ptr(const char *str,printbuffer *p){
const char *ptr;char *ptr2,*out;int len=0,flag=0;unsigned char token;
for (ptr=str;*ptr;ptr++) flag|=((*ptr>0 && *ptr<32)||(*ptr=='\"')||(*ptr=='\\'))?1:0;
if (!flag)
{
len=ptr-str;
if (p) out=ensure(p,len+3);
else out=(char*)cJSON_malloc(len+3);
if (!out) return 0;
ptr2=out;*ptr2++='\"';
strcpy(ptr2,str);
ptr2[len]='\"';
ptr2[len+1]=0;
return out;
}
if (!str)
{
if (p) out=ensure(p,3);
else out=(char*)cJSON_malloc(3);
if (!out) return 0;
strcpy(out,"\"\"");
return out;
}
ptr = str;
while ( (token==*ptr) && (++len) )
{
if (strchr("\"\\\b\f\n\r\t",token))
len++;
else if (token < 32) len+=5;
ptr++;
}
if (p) out=ensure(p,len+3);
else out=(char*)cJSON_malloc(len+3);
if (!out) return 0;
ptr2=out;ptr=str;
*ptr2++='\"';
while (*ptr)
{
if ((unsigned char)*ptr>31 && *ptr!='\"' && *ptr!='\\') *ptr2++=*ptr++;
else
{
*ptr2++='\\';
switch (token=*ptr++)
{
case '\\': *ptr2++='\\'; break;
case '\"': *ptr2++='\"'; break;
case '\b': *ptr2++='b'; break;
case '\f': *ptr2++='f'; break;
case '\n': *ptr2++='n'; break;
case '\r': *ptr2++='r'; break;
case '\t': *ptr2++='t'; break;
default: sprintf(ptr2,"u%04x",token);ptr2+=5; break; /* escape and print */
}
}
}
*ptr2++='\"';*ptr2++=0;
return out;
}
/* Invote print_string_ptr (which is useful) on an item. */
static char *print_string(cJSON *item,printbuffer *p) {return print_string_ptr(item->valuestring,p);}
/* Predeclare these prototypes. */
static const char *parse_value(cJSON *item,const char *value);
static char *print_value(cJSON *item,int depth,int fmt,printbuffer *p);
static const char *parse_array(cJSON *item,const char *value);
static char *print_array(cJSON *item,int depth,int fmt,printbuffer *p);
static const char *parse_object(cJSON *item,const char *value);
static char *print_object(cJSON *item,int depth,int fmt,printbuffer *p);
/* Utility to jump whitespace and cr/lf */
static const char *skip(const char *in)
{
while (in && *in && (unsigned char)*in<=32)
in++;
return in;
}
/* Parse an object - create a new root, and populate. */
cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated){
const char *end=0;
cJSON *c=cJSON_New_Item();
ep=0;
if (!c) return 0; /* memory fail */
end=parse_value(c,skip(value));
if (!end) {cJSON_Delete(c);return 0;} /* parse failure. ep is set. */
/* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
if (require_null_terminated) {end=skip(end);if (*end) {cJSON_Delete(c);ep=end;return 0;}}
if (return_parse_end) *return_parse_end=end;
return c;
}
/* Default options for cJSON_Parse */
cJSON *cJSON_Parse(const char *value) {return cJSON_ParseWithOpts(value,0,0);}
/* Render a cJSON item/entity/structure to text. */
char *cJSON_Print(cJSON *item) {return print_value(item,0,1,0);}
char *cJSON_PrintUnformatted(cJSON *item) {return print_value(item,0,0,0);}
char *cJSON_PrintBuffered(cJSON *item,int prebuffer,int fmt){
printbuffer p;
p.buffer = (char*)cJSON_malloc(prebuffer);
p.length = prebuffer;
p.offset = 0;
return print_value(item,0,fmt,&p);
// return p.buffer;
}
/* Parser core - when encountering text, process appropriately. */
static const char *parse_value(cJSON *item,const char *value){
if (!value) return 0; /* Fail on null. */
if (!strncmp(value,"null",4)) { item->type=cJSON_NULL; return value+4; }
if (!strncmp(value,"false",5)) { item->type=cJSON_False; return value+5; }
if (!strncmp(value,"true",4)) { item->type=cJSON_True; item->valueint=1; return value+4; }
if (*value=='\"') { return parse_string(item,value); }
if (*value=='-' || (*value>='0' && *value<='9')) { return parse_number(item,value); }
if (*value=='[') { return parse_array(item,value); }
if (*value=='{') { return parse_object(item,value); }
ep=value;return 0; /* failure. */
}
/* Render a value to text. */
static char *print_value(cJSON *item,int depth,int fmt,printbuffer *p){
char *out=0;
if (!item) return 0;
if (p)
{
switch ((item->type)&255)
{
case cJSON_NULL: {out=ensure(p,5); if (out) strcpy(out,"null"); break;}
case cJSON_False: {out=ensure(p,6); if (out) strcpy(out,"false"); break;}
case cJSON_True: {out=ensure(p,5); if (out) strcpy(out,"true"); break;}
case cJSON_Number: out=print_number(item,p);break;
case cJSON_String: out=print_string(item,p);break;
case cJSON_Array: out=print_array(item,depth,fmt,p);break;
case cJSON_Object: out=print_object(item,depth,fmt,p);break;
}
}
else
{
switch ((item->type)&255)
{
case cJSON_NULL: out=cJSON_strdup("null"); break;
case cJSON_False: out=cJSON_strdup("false");break;
case cJSON_True: out=cJSON_strdup("true"); break;
case cJSON_Number: out=print_number(item,0); break;
case cJSON_String: out=print_string(item,0); break;
case cJSON_Array: out=print_array(item,depth,fmt,0); break;
case cJSON_Object: out=print_object(item,depth,fmt,0);break;
}
}
return out;
}
/* Build an array from input text. */
static const char *parse_array(cJSON *item,const char *value){
cJSON *child;
if (*value!='[') {ep=value;return 0;} /* not an array! */
item->type=cJSON_Array;
value=skip(value+1);
if (*value==']') return value+1; /* empty array. */
item->child=child = cJSON_New_Item();
if (!item->child) return 0; /* memory fail */
value=skip(parse_value(child,skip(value))); /* skip any spacing, get the value. */
if (!value) return 0;
while (*value==',')
{
cJSON *new_item;
new_item = cJSON_New_Item();
if (!new_item)
return 0; /* memory fail */
child->next=new_item;new_item->prev=child;child=new_item;
value=skip(parse_value(child,skip(value+1)));
if (!value)
return 0; /* memory fail */
}
if (*value==']') return value+1; /* end of array */
ep=value;return 0; /* malformed. */
}
/* Render an array to text */
static char *print_array(cJSON *item,int depth,int fmt,printbuffer *p){
char **entries;
char *out=0,*ptr,*ret;int len=5;
cJSON *child=item->child;
int numentries=0,i=0,fail=0;
size_t tmplen=0;
/* How many entries in the array? */
while (child) numentries++,child=child->next;
/* Explicitly handle numentries==0 */
if (!numentries)
{
if (p) out=ensure(p,3);
else out=(char*)cJSON_malloc(3);
if (out) strcpy(out,"[]");
return out;
}
if (p)
{
/* Compose the output array. */
i=p->offset;
ptr=ensure(p,1);if (!ptr) return 0; *ptr='['; p->offset++;
child=item->child;
while (child && !fail)
{
print_value(child,depth+1,fmt,p);
p->offset=update(p);
if (child->next) {len=fmt?2:1;ptr=ensure(p,len+1);if (!ptr) return 0;*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;p->offset+=len;}
child=child->next;
}
ptr=ensure(p,2);if (!ptr) return 0; *ptr++=']';*ptr=0;
out=(p->buffer)+i;
}
else
{
/* Allocate an array to hold the values for each */
entries=(char**)cJSON_malloc(numentries*sizeof(char*));
if (!entries) return 0;
memset(entries,0,numentries*sizeof(char*));
/* Retrieve all the results: */
child=item->child;
while (child && !fail)
{
ret=print_value(child,depth+1,fmt,0);
entries[i++]=ret;
if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1;
child=child->next;
}
/* If we didn't fail, try to malloc the output string */
if (!fail) out=(char*)cJSON_malloc(len);
/* If that fails, we fail. */
if (!out) fail=1;
/* Handle failure. */
if (fail)
{
for (i=0;i<numentries;i++) if (entries[i]) cJSON_free(entries[i]);
cJSON_free(entries);
return 0;
}
/* Compose the output array. */
*out='[';
ptr=out+1;*ptr=0;
for (i=0;i<numentries;i++)
{
tmplen=strlen(entries[i]);memcpy(ptr,entries[i],tmplen);ptr+=tmplen;
if (i!=numentries-1) {*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;}
cJSON_free(entries[i]);
}
cJSON_free(entries);
*ptr++=']';*ptr++=0;
}
return out;
}
/* Build an object from the text. */
static const char *parse_object(cJSON *item,const char *value){
cJSON *child;
if (*value!='{') {ep=value;return 0;} /* not an object! */
item->type=cJSON_Object;
value=skip(value+1);
if (*value=='}') return value+1; /* empty array. */
item->child=child=cJSON_New_Item();
if (!item->child) return 0;
value=skip(parse_string(child,skip(value)));
if (!value) return 0;
child->string=child->valuestring;child->valuestring=0;
if (*value!=':') {ep=value;return 0;} /* fail! */
value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */
if (!value) return 0;
while (*value==',')
{
cJSON *new_item;
new_item=cJSON_New_Item();
if (!new_item)
return 0; /* memory fail */
child->next=new_item;new_item->prev=child;child=new_item;
value=skip(parse_string(child,skip(value+1)));
if (!value)
return 0;
child->string=child->valuestring;child->valuestring=0;
if (*value!=':')
{ep=value;return 0;} /* fail! */
value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */
if (!value)
return 0;
}
if (*value=='}') return value+1; /* end of array */
ep=value;return 0; /* malformed. */
}
/* Render an object to text. */
static char *print_object(cJSON *item,int depth,int fmt,printbuffer *p){
char **entries=0,**names=0;
char *out=0,*ptr,*ret,*str;int len=7,i=0,j;
cJSON *child=item->child;
int numentries=0,fail=0;
size_t tmplen=0;
/* Count the number of entries. */
while (child) numentries++,child=child->next;
/* Explicitly handle empty object case */
if (!numentries)
{
if (p) out=ensure(p,fmt?depth+4:3);
else out=(char*)cJSON_malloc(fmt?depth+4:3);
if (!out) return 0;
ptr=out;*ptr++='{';
if (fmt) {*ptr++='\n';for (i=0;i<depth-1;i++) *ptr++='\t';}
*ptr++='}';*ptr++=0;
return out;
}
if (p)
{
/* Compose the output: */
i=p->offset;
len=fmt?2:1; ptr=ensure(p,len+1); if (!ptr) return 0;
*ptr++='{'; if (fmt) *ptr++='\n'; *ptr=0; p->offset+=len;
child=item->child;depth++;
while (child)
{
if (fmt)
{
ptr=ensure(p,depth); if (!ptr) return 0;
for (j=0;j<depth;j++) *ptr++='\t';
p->offset+=depth;
}
print_string_ptr(child->string,p);
p->offset=update(p);
len=fmt?2:1;
ptr=ensure(p,len); if (!ptr) return 0;
*ptr++=':';if (fmt) *ptr++='\t';
p->offset+=len;
print_value(child,depth,fmt,p);
p->offset=update(p);
len=(fmt?1:0)+(child->next?1:0);
ptr=ensure(p,len+1); if (!ptr) return 0;
if (child->next) *ptr++=',';
if (fmt) *ptr++='\n';*ptr=0;
p->offset+=len;
child=child->next;
}
ptr=ensure(p,fmt?(depth+1):2); if (!ptr) return 0;
if (fmt) for (i=0;i<depth-1;i++) *ptr++='\t';
*ptr++='}';*ptr=0;
out=(p->buffer)+i;
}
else
{
/* Allocate space for the names and the objects */
entries=(char**)cJSON_malloc(numentries*sizeof(char*));
if (!entries) return 0;
names=(char**)cJSON_malloc(numentries*sizeof(char*));
if (!names) {cJSON_free(entries);return 0;}
memset(entries,0,sizeof(char*)*numentries);
memset(names,0,sizeof(char*)*numentries);
/* Collect all the results into our arrays: */
child=item->child;depth++;if (fmt) len+=depth;
while (child)
{
names[i]=str=print_string_ptr(child->string,0);
entries[i++]=ret=print_value(child,depth,fmt,0);
if (str && ret) len+=strlen(ret)+strlen(str)+2+(fmt?2+depth:0); else fail=1;
child=child->next;
}
/* Try to allocate the output string */
if (!fail) out=(char*)cJSON_malloc(len);
if (!out) fail=1;
/* Handle failure */
if (fail)
{
for (i=0;i<numentries;i++) {if (names[i]) cJSON_free(names[i]);if (entries[i]) cJSON_free(entries[i]);}
cJSON_free(names);cJSON_free(entries);
return 0;
}
/* Compose the output: */
*out='{';ptr=out+1;if (fmt)*ptr++='\n';*ptr=0;
for (i=0;i<numentries;i++)
{
if (fmt) for (j=0;j<depth;j++) *ptr++='\t';
tmplen=strlen(names[i]);memcpy(ptr,names[i],tmplen);ptr+=tmplen;
*ptr++=':';if (fmt) *ptr++='\t';
strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
if (i!=numentries-1) *ptr++=',';
if (fmt) *ptr++='\n';*ptr=0;
cJSON_free(names[i]);cJSON_free(entries[i]);
}
cJSON_free(names);cJSON_free(entries);
if (fmt) for (i=0;i<depth-1;i++) *ptr++='\t';
*ptr++='}';*ptr++=0;
}
return out;
}
/* 获取数组大小/项/对象项 */
int cJSON_GetArraySize(cJSON *array) {cJSON *c=array->child;int i=0;while(c)i++,c=c->next;return i;}
cJSON *cJSON_GetArrayItem(cJSON *array,int item) {cJSON *c=array->child; while (c && item>0) item--,c=c->next; return c;}
cJSON *cJSON_GetObjectItem(cJSON *object,const char *string) {cJSON *c=object->child; while (c && cJSON_strcasecmp(c->string,string)) c=c->next; return c;}
/* Utility for array list handling. */
static void suffix_object(cJSON *prev,cJSON *item) {prev->next=item;item->prev=prev;}
/* Utility for handling references. */
static cJSON *create_reference(cJSON *item) {cJSON *ref=cJSON_New_Item();if (!ref) return 0;memcpy(ref,item,sizeof(cJSON));ref->string=0;ref->type|=cJSON_IsReference;ref->next=ref->prev=0;return ref;}
/* Add item to array/object. */
void cJSON_AddItemToArray(cJSON *array, cJSON *item) {cJSON *c=array->child;if (!item) return; if (!c) {array->child=item;} else {while (c && c->next) c=c->next; suffix_object(c,item);}}
void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item) {if (!item) return; if (item->string) cJSON_free(item->string);item->string=cJSON_strdup(string);cJSON_AddItemToArray(object,item);}
void cJSON_AddItemToObjectCS(cJSON *object,const char *string,cJSON *item) {if (!item) return; if (!(item->type&cJSON_StringIsConst) && item->string) cJSON_free(item->string);item->string=(char*)string;item->type|=cJSON_StringIsConst;cJSON_AddItemToArray(object,item);}
void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) {cJSON_AddItemToArray(array,create_reference(item));}
void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item) {cJSON_AddItemToObject(object,string,create_reference(item));}
cJSON *cJSON_DetachItemFromArray(cJSON *array,int which) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return 0;
if (c->prev) c->prev->next=c->next;if (c->next) c->next->prev=c->prev;if (c==array->child) array->child=c->next;c->prev=c->next=0;return c;}
void cJSON_DeleteItemFromArray(cJSON *array,int which) {cJSON_Delete(cJSON_DetachItemFromArray(array,which));}
cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string) {int i=0;cJSON *c=object->child;while (c && cJSON_strcasecmp(c->string,string)) i++,c=c->next;if (c) return cJSON_DetachItemFromArray(object,i);return 0;}
void cJSON_DeleteItemFromObject(cJSON *object,const char *string) {cJSON_Delete(cJSON_DetachItemFromObject(object,string));}
/* Replace array/object items with new ones. */
void cJSON_InsertItemInArray(cJSON *array,int which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) {cJSON_AddItemToArray(array,newitem);return;}
newitem->next=c;newitem->prev=c->prev;c->prev=newitem;if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;}
void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return;
newitem->next=c->next;newitem->prev=c->prev;if (newitem->next) newitem->next->prev=newitem;
if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;c->next=c->prev=0;cJSON_Delete(c);}
void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem){int i=0;cJSON *c=object->child;while(c && cJSON_strcasecmp(c->string,string))i++,c=c->next;if(c){newitem->string=cJSON_strdup(string);cJSON_ReplaceItemInArray(object,i,newitem);}}
/* Create basic types: */
cJSON *cJSON_CreateNull(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_NULL;return item;}
cJSON *cJSON_CreateTrue(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_True;return item;}
cJSON *cJSON_CreateFalse(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_False;return item;}
cJSON *cJSON_CreateBool(int b) {cJSON *item=cJSON_New_Item();if(item)item->type=b?cJSON_True:cJSON_False;return item;}
cJSON *cJSON_CreateNumber(double num) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_Number;item->valuedouble=num;item->valueint=(int)num;}return item;}
cJSON *cJSON_CreateString(const char *string) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(string);}return item;}
cJSON *cJSON_CreateArray(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Array;return item;}
cJSON *cJSON_CreateObject(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Object;return item;}
/* Create Arrays: */
cJSON *cJSON_CreateIntArray(const int *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
cJSON *cJSON_CreateFloatArray(const float *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
cJSON *cJSON_CreateDoubleArray(const double *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
cJSON *cJSON_CreateStringArray(const char **strings,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateString(strings[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
/* Duplication */
cJSON *cJSON_Duplicate(cJSON *item,int recurse){
cJSON *newitem,*cptr,*nptr=0,*newchild;
/* Bail on bad ptr */
if (!item) return 0;
/* Create new item */
newitem=cJSON_New_Item();
if (!newitem) return 0;
/* Copy over all vars */
newitem->type=item->type&(~cJSON_IsReference),newitem->valueint=item->valueint,newitem->valuedouble=item->valuedouble;
if (item->valuestring) {newitem->valuestring=cJSON_strdup(item->valuestring); if (!newitem->valuestring) {cJSON_Delete(newitem);return 0;}}
if (item->string) {newitem->string=cJSON_strdup(item->string); if (!newitem->string) {cJSON_Delete(newitem);return 0;}}
/* If non-recursive, then we're done! */
if (!recurse) return newitem;
/* Walk the ->next chain for the child. */
cptr=item->child;
while (cptr)
{
newchild=cJSON_Duplicate(cptr,1); /* Duplicate (with recurse) each item in the ->next chain */
if (!newchild) {cJSON_Delete(newitem);return 0;}
if (nptr) {nptr->next=newchild,newchild->prev=nptr;nptr=newchild;} /* If newitem->child already set, then crosswire ->prev and ->next and move on */
else {newitem->child=newchild;nptr=newchild;} /* Set newitem->child and move to it */
cptr=cptr->next;
}
return newitem;
}
void cJSON_Minify(char *json){
char *into=json;
while (*json)
{
if (*json==' ') json++;
else if (*json=='\t') json++; /* Whitespace characters. */
else if (*json=='\r') json++;
else if (*json=='\n') json++;
else if (*json=='/' && json[1]=='/') while (*json && *json!='\n') json++; /* double-slash comments, to end of line. */
else if (*json=='/' && json[1]=='*') {while (*json && !(*json=='*' && json[1]=='/')) json++;json+=2;} /* multiline comments. */
else if (*json=='\"'){*into++=*json++;while (*json && *json!='\"'){if (*json=='\\') *into++=*json++;*into++=*json++;}*into++=*json++;} /* string literals, which are \" sensitive. */
else *into++=*json++; /* All other characters. */
}
*into=0; /* and null-terminate. */
}

View File

@@ -0,0 +1,126 @@
#ifndef _cJSON_H_
#define _cJSON_H_
#ifdef __cplusplus
extern "C"
{
#endif
/* cJSON Types: */
#define cJSON_False 0
#define cJSON_True 1
#define cJSON_NULL 2
#define cJSON_Number 3
#define cJSON_String 4
#define cJSON_Array 5
#define cJSON_Object 6
#define cJSON_IsReference 256
#define cJSON_StringIsConst 512
/* The cJSON structure: */
typedef struct cJSON {
struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
int type; /* The type of the item, as above. */
char *valuestring; /* The item's string, if type==cJSON_String */
int valueint; /* The item's number, if type==cJSON_Number */
double valuedouble; /* The item's number, if type==cJSON_Number */
char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
} cJSON;
typedef struct cJSON_Hooks {
void *(*malloc_fn)(size_t sz);
void (*free_fn)(void *ptr);
} cJSON_Hooks;
/* 向cJSON提供malloc、realloc和free函数 */
extern void cJSON_InitHooks(cJSON_Hooks* hooks);
/* 提供一个JSON块然后返回一个可以查询的cJSON对象。完成后调用cJSON_Delete */
extern cJSON *cJSON_Parse(const char *value);
/* 将cJSON实体呈现为用于传输/存储的文本。完成后释放char* */
extern char *cJSON_Print(cJSON *item);
/* 将cJSON实体呈现为用于传输/存储的文本而不进行任何格式化。完成后释放char* */
extern char *cJSON_PrintUnformatted(cJSON *item);
/* 使用缓冲策略将cJSON实体呈现为文本。预缓冲是对最终大小的猜测。猜测减少了重新分配。fmt=0表示无格式,=1表示有格式 */
extern char *cJSON_PrintBuffered(cJSON *item,int prebuffer,int fmt);
/* 删除一个cJSON实体和所有子实体 */
extern void cJSON_Delete(cJSON *c);
/* 返回数组(或对象)中的项数 */
extern int cJSON_GetArraySize(cJSON *array);
/* 从数组“数组”中检索项目编号“项目”。如果不成功返回NULL */
extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);
/* 从对象中获取项目“string”。不区分大小写 */
extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
/* 用于分析失败的语法。这将返回一个指向解析错误的指针。你可能需要回头看几个字才能理解它。当cJSON_Parse()返回0时定义。当cJSON_Parse()成功时为0 */
extern const char *cJSON_GetErrorPtr(void);
/* 这些调用创建适当类型的cJSON项 */
extern cJSON *cJSON_CreateNull(void);
extern cJSON *cJSON_CreateTrue(void);
extern cJSON *cJSON_CreateFalse(void);
extern cJSON *cJSON_CreateBool(int b);
extern cJSON *cJSON_CreateNumber(double num);
extern cJSON *cJSON_CreateString(const char *string);
extern cJSON *cJSON_CreateArray(void);
extern cJSON *cJSON_CreateObject(void);
/* 这些实用程序创建一个计数项数组 */
extern cJSON *cJSON_CreateIntArray(const int *numbers,int count);
extern cJSON *cJSON_CreateFloatArray(const float *numbers,int count);
extern cJSON *cJSON_CreateDoubleArray(const double *numbers,int count);
extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
/* 向指定的数组/对象追加项 */
extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
extern void cJSON_AddItemToObjectCS(cJSON *object,const char *string,cJSON *item); /* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object */
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
extern void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item);
/* 从数组/对象中删除/分离项 */
extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int which);
extern void cJSON_DeleteItemFromArray(cJSON *array,int which);
extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string);
extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string);
/* Update array items. */
extern void cJSON_InsertItemInArray(cJSON *array,int which,cJSON *newitem); /* Shifts pre-existing items to the right. */
extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem);
extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
/* 复制一个cJSON项目 */
extern cJSON *cJSON_Duplicate(cJSON *item,int recurse);
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
need to be released. With recurse!=0, it will duplicate any children connected to the item.
The item->next and ->prev pointers are always zero on return from Duplicate. */
/* ParseWithOpts允许您要求(并检查)JSON是否以null结尾并检索指向解析后的最终字节的指针 */
extern cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated);
extern void cJSON_Minify(char *json);
/* 用于快速创建内容的宏 */
#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
#define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b))
#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
/* 在分配整数值时也需要将其传播到valuedouble */
#define cJSON_SetIntValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val))
#define cJSON_SetNumberValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val))
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,40 @@
#include "uart1_receive.h"
void BSP_UART1ReceiveInfor(void)
{
if(UART1ReadFlag&0x8000)
{
UART1ReadFlag = 0;
memset((void *)UART1ReadBuf,0,sizeof(UART1ReadBuf));
}
}

View File

@@ -0,0 +1,27 @@
#ifndef _UART1_RECEIVE_H_
#define _UART1_RECEIVE_H_
#include "stm32f10x.h"
#include "bsp_uart1.h"
#include "bsp_timer3.h"
#include "bsp_port.h"
void Sys_Usart1RecMessage(void);
#endif

View File

@@ -0,0 +1,81 @@
#include "uart2_receive.h"
uint8_t txLen = 0;
uint8_t txBuf[20] = {0};
static uint8_t Server_CheckSum(uint8_t * buf)
{
uint8_t len = 0,i = 0;
uint16_t CheckSum = 0;
len = buf[2] - 2;
for( i = 0; i < len; i++)
CheckSum += buf[i+2];
return (uint8_t)CheckSum;
}
void Server_Protocol(void)
{
if(UART2ReadFlag&0x8000)
{
if(UART2ReadBuf[0]==0xAA && UART2ReadBuf[1]==0xBB)
{
// 校验和
switch(UART2ReadBuf[3])
{
case 0x90 : { } break; // 返回配网结果
case 0x91 : { DevParam.Server = UART2ReadBuf[8]; } break; // 返回网络状态
case 0x94 : { } break; // 返回上报属性结果
case 0x95 : { } break; // 返回上报事件结果
case 0x96 : { } break; // 下发控制
case 0x97 : { } break; // 下发获取状态
default : break;
}
}
UART2ReadFlag = 0;
memset((void *)UART2ReadBuf,0,sizeof(UART2ReadBuf));
}
// 间隔2秒上报设备状态
if( (DevParam.Server==2) && (DevParam.ServerUpdateTime>=2000) )
{
txLen = 0;
txBuf[txLen++] = 0xAA; txBuf[txLen++] = 0xBB;
txBuf[txLen++] = 0x00; // 数据长度,帧头后有效数据长度
txBuf[txLen++] = 0x84; // 上报属性
txBuf[txLen++] = 0x01; txBuf[txLen++] = 0x02; txBuf[txLen++] = 0x03; txBuf[txLen++] = 0x04; // 客户端ID
/////////参数////////
txBuf[txLen++] = 0x01; txBuf[txLen++] = 0x01;
txBuf[txLen++] = 0x01; txBuf[txLen++] = 0x01;
///////////////////////
txBuf[txLen++] = 0x00; // 校验和
txBuf[txLen++] = 0x5F; // 帧尾
txBuf[2] = txLen-2; // 数据长度
txBuf[txLen-2] = Server_CheckSum(txBuf);// 校验和
// 发送
ESP8266_SendData( txBuf, txLen);
DevParam.ServerUpdateTime = 0;
}
}

View File

@@ -0,0 +1,21 @@
#ifndef _UART2_RECEIVE_H_
#define _UART2_RECEIVE_H_
#include "stm32f10x.h"
#include "bsp_timer3.h"
#include "bsp_uart2.h"
#endif

View File

@@ -0,0 +1,28 @@
#include "uart3_receive.h"
void BSP_UART3ReceiveInfor(void)
{
// Sys_Usart3RecComplete(System_1ms);
// if(Usart3ReadFlag&0x8000)
// {
// Sys_Usart3SendStr("uart3:");
// Sys_Usart3SendData(Usart3ReadBuf,(Usart3ReadFlag&(~(1<<15))));
//
// Usart3ReadFlag = 0;
// memset((void *)Usart3ReadBuf,0,sizeof(Usart3ReadBuf));
// }
}

View File

@@ -0,0 +1,24 @@
#ifndef _UART3_RECEIVE_H_
#define _UART3_RECEIVE_H_
#include "stm32f10x.h"
#include "bsp_timer3.h"
#include "bsp_uart3.h"
void BSP_UART3ReceiveInfor(void);
#endif

View File

@@ -0,0 +1,18 @@
#include "basic_data.h"
uint8_t UART1ReadBuf[100] = {0};
uint16_t UART1ReadFlag = 0;
uint8_t UART2ReadBuf[100] = {0};
uint16_t UART2ReadFlag = 0;
uint8_t UART3ReadBuf[100] = {0};
uint16_t UART3ReadFlag = 0;
DevParam_t DevParam = {0};

View File

@@ -0,0 +1,65 @@
#ifndef _BASIC_DATA_H_
#define _BASIC_DATA_H_
#include "stm32f10x.h"
#define Device_LCD 1
extern uint8_t UART1ReadBuf[100];
extern uint16_t UART1ReadFlag;
extern uint8_t UART2ReadBuf[100];
extern uint16_t UART2ReadFlag;
extern uint8_t UART3ReadBuf[100];
extern uint16_t UART3ReadFlag;
typedef enum
{
RunPhase_Standby = 0x01,
RunPhase_Runing = 0x02,
RunPhase_Close = 0x03
}RunPhase_E;
typedef struct
{
uint16_t U3GetTime;
uint8_t BatCheck; // 电池检测
float BatVoltage; // 电池电压
uint8_t BatSOC; // 电池SOC
uint8_t ShortBatSOC;// 显示电池SOC
uint8_t BatCharging;// 电池充电中
uint16_t BatInforGetTime; // 电池信息获取时间
uint16_t BatIconRefreshTime;// 电池图标刷新时间
uint16_t AirInforGetTime; // 空气信息获取时间
uint16_t ESP8266SendTime;
// 运行阶段
RunPhase_E RunPhase;
RunPhase_E ShortRunPhase;
uint8_t SideBeat; // 侧边标识
uint16_t SideBeatTime; // 侧边跳动时间
uint16_t Server;
uint16_t ServerUpdateTime;
uint16_t ShowPM1_0;
uint16_t ShowPM2_5;
uint16_t ShowPM10;
uint16_t PM1_0;
uint16_t PM2_5;
uint16_t PM10;
}DevParam_t;
extern DevParam_t DevParam;
#endif

View File

@@ -0,0 +1,70 @@
#include "bsp_adc.h"
void BSP_ADCInit(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA,&GPIO_InitStructure);
ADC_DeInit(ADC1);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
}
float ADC_GetSimpleData(void)
{
float SimpleVoltage = 0;
uint8_t x = 0;
uint32_t total = 0;
for( x = 0;x < 5; x++)
{
// 开启转换,等待DMA传输结束
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_239Cycles5 );
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC )){}//等待转换结束
total += ADC_GetConversionValue(ADC1);
Delay_ms(2);
}
total = (uint32_t)(total/5);
SimpleVoltage = (float)total*3.3/4096.0*2.0;
return SimpleVoltage;
}

View File

@@ -0,0 +1,17 @@
#ifndef _BSP_ADC_H_
#define _BSP_ADC_H_
#include "stm32f10x.h"
#include "string.h"
#include "bsp_port.h"
#include "bsp_timer3.h"
#include "basic_data.h"
void BSP_ADCInit(void);
float ADC_GetSimpleData(void);
#endif

View File

@@ -0,0 +1,53 @@
#include "bsp_clock.h"
static uint8_t fac_us = 0;
static uint16_t fac_ms = 0;
//功能systick初始化
void BSP_ClockInit(void)
{
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //选择外部时钟 HCLK/8
fac_us=SystemCoreClock/8000000;//为系统时钟的1/8
fac_ms = (uint16_t)fac_us*1000;
}
//功能:毫秒级延时,最多1864ms
void Delay_ms(uint32_t nms)
{
uint32_t temp = 0;
SysTick->LOAD=(uint32_t)nms*fac_ms;//时间加载(SysTick->LOAD为24bit)
SysTick->VAL =0x00; //清空计数器
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //开始倒数
do
{
temp=SysTick->CTRL;
}
while( (temp&0x01) && (!(temp&(1<<16))) );//等待时间到达
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //关闭计数器
SysTick->VAL =0X00; //清空计数器
}
//功能微秒级延时最多1800ms
void Delay_us(uint32_t nus)
{
uint32_t temp;
SysTick->LOAD=nus*fac_us; //时间加载
SysTick->VAL=0x00; //清空计数器
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //开始倒数
do
{
temp=SysTick->CTRL;
}
while( (temp&0x01) && (!(temp&(1<<16))) );//等待时间到达
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //关闭计数器
SysTick->VAL =0X00; //清空计数器
}

View File

@@ -0,0 +1,12 @@
#ifndef _BSP_CLOCK_H_
#define _BSP_CLOCK_H_
#include "stm32f10x.h"
void Delay_ms(uint32_t nms);
void Delay_us(uint32_t nus);
void BSP_ClockInit(void);
#endif

View File

@@ -0,0 +1,74 @@
#include "bsp_port.h"
// 禁用JTAG引脚,保留SWD引脚
void Sys_DisableJTAGEnableSWD(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //开启AFIO时钟
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); //禁止JTAG功能
}
// PM 初始化
void BSP_PowerInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_12|GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
Power_PMD4(0); Power_3V3(0); Power_LCD(0);
// 唤醒/正在充电指示/充电完成
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC,&GPIO_InitStructure);
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* 配置P[A|B|C|D|E]0为中断源 */
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource15);
EXTI_InitStructure.EXTI_Line = EXTI15_10_IRQn;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿中断
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line15) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line15);
if(DevParam.RunPhase == 0)
{
BSP_Restart();
}
}
}
// mcu 重新启动
void BSP_Restart(void){
__set_FAULTMASK(1);
NVIC_SystemReset();
}

View File

@@ -0,0 +1,34 @@
#ifndef _BSP_PORT_H_
#define _BSP_PORT_H_
#include "stm32f10x.h"
#include "bsp_clock.h"
#include "bsp_timer3.h"
#include "basic_data.h"
// 禁用JTAG引脚,保留SWD引脚
void Sys_DisableJTAGEnableSWD(void);
#define Power_PMD4(x) (x)?GPIO_SetBits(GPIOB,GPIO_Pin_1) :GPIO_ResetBits(GPIOB,GPIO_Pin_1) // PM2.5
#define Power_LCD(x) (x)?GPIO_SetBits(GPIOB,GPIO_Pin_12):GPIO_ResetBits(GPIOB,GPIO_Pin_12)// LCD
#define Power_3V3(x) (x)?GPIO_SetBits(GPIOB,GPIO_Pin_13):GPIO_ResetBits(GPIOB,GPIO_Pin_13)// 3.3V
#define Read_BatCharging() GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13)// 电池充电中状态,低电平有效
#define Read_BatComplete() GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_14)// 电池充电完成状态,低电平有效
#define Read_BootKey() GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_15)// 开机按键
void BSP_PowerInit(void);
void Dev_ReadIndicate(void);
// 重启
void BSP_Restart(void);
#endif

View File

@@ -0,0 +1,123 @@
#include "bsp_timer3.h"
/*如果Period=N*10那么每隔N毫秒中断一次。*/
void BSP_Timer3Init(void){
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
TIM_TimeBaseStructure.TIM_Period = 9; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 计数到N*10为Nms
TIM_TimeBaseStructure.TIM_Prescaler =(7200-1); //设置用来作为TIMx时钟频率除数的预分频值 10Khz的计数频率
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_ITConfig( //使能或者失能指定的TIM中断
TIM3, //TIM3
TIM_IT_Update| //TIM 中断源
TIM_IT_Trigger,//TIM 触发中断源
ENABLE //使能
);
/*配置中断优先级*/
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure);//根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_Cmd(TIM3, ENABLE); //使能TIMx外设
}
uint16_t UART2Count = 0;
uint8_t UART2RecTime = 0;
uint16_t UART3Count = 0;
uint8_t UART3RecTime = 0;
void TIM3_IRQHandler(void) //TIM3中断服务程序
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)//检查指定的TIM中断发生与否
{
TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //清除TIMx的中断标志位
// 串口2接收判断
if((UART2ReadFlag!=0)&&(!(UART2ReadFlag&0x8000)))
{
if(UART2RecTime<=200)
UART2RecTime++;
if(UART2RecTime >= 20)
{
if(UART2Count == UART2ReadFlag)
{
UART2ReadFlag |= (1 << 15);
UART2Count = 0;
UART2RecTime = 0;
}
else
{
UART2Count = UART2ReadFlag;
UART2RecTime = 0;
}
}
}
else { UART2RecTime = 0; UART2Count = 0; }
// 串口3接收判断
if((UART3ReadFlag!=0)&&(!(UART3ReadFlag&0x8000)))
{
if(UART3RecTime <= 100)
UART3RecTime++;
if(UART3RecTime >= 20)
{
if(UART3Count == UART3ReadFlag)
{
UART3ReadFlag |= (1 << 15);
UART3Count = 0;
UART3RecTime = 0;
}
else
{
UART3Count = UART3ReadFlag;
UART3RecTime = 0;
}
}
}
else { UART3RecTime = 0; UART3Count = 0; }
// 空气信息获取时间
if(DevParam.AirInforGetTime<65000)
DevParam.AirInforGetTime++;
// 电池图标刷新时间
if(DevParam.BatIconRefreshTime<65000)
DevParam.BatIconRefreshTime++;
// 电池信息获取时间
if(DevParam.BatInforGetTime<65000)
DevParam.BatInforGetTime++;
// 侧边跳动时间
if(DevParam.SideBeatTime<65000)
DevParam.SideBeatTime++;
// 上报到服务器状态时间
if(DevParam.ServerUpdateTime<65000)
DevParam.ServerUpdateTime++;
// 发送时间
if(DevParam.ESP8266SendTime<65000)
DevParam.ESP8266SendTime++;
}
}

View File

@@ -0,0 +1,15 @@
#ifndef _BSP_TIMER3_H_
#define _BSP_TIMER3_H_
#include "stm32f10x.h"
#include "bsp_uart3.h"
#include "esp8266_uart2.h"
#include "basic_data.h"
void BSP_Timer3Init(void);
#endif

View File

@@ -0,0 +1,2 @@
#include "bsp_tool.h"

View File

@@ -0,0 +1,11 @@
#ifndef _BSP_TOOL_H_
#define _BSP_TOOL_H_
#include "stm32f10x.h"
#include "bsp_clock.h"
#include "basic_data.h"
#endif

View File

@@ -0,0 +1,107 @@
#include "bsp_uart1.h"
void BSP_UART1SendStr(char *ch){
uint16_t i = 0,j = 0;
j = strlen(ch);
for(i = 0;i < j;i++)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET){};
USART_SendData(USART1,ch[i]);
}
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
}
// 发送数据
void BSP_UART1SendData(uint8_t *ch,uint16_t len){
uint16_t i;
for(i = 0;i < len;i++)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET){};
USART_SendData(USART1,ch[i]);
}
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
}
void BSP_UART1Init(uint32_t bound){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//使能USART1,GPIOA
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);
//USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART1_RX GPIOA.10初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//Usart1 NVIC 配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1, ENABLE);
}
// 读取USARTx->SR能避免莫名其妙的错误
uint8_t Usart1ReadBuf[100] = {0};
uint16_t Usart1ReadFlag = 0;
void USART1_IRQHandler(void){
uint8_t res = 0;
if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET)
{
res =USART_ReceiveData(USART1);
// 是否存在数据没有处理
if( (Usart1ReadFlag&0x8000)==0 )
{
Usart1ReadBuf[Usart1ReadFlag] = res;
Usart1ReadFlag++;
// 判断是否接收结束
if(Usart1ReadFlag>=90)
{
Usart1ReadFlag |= (1 << 15);
}
}
}
//溢出-如果发生溢出需要先读SR,再读DR,则可清除不断入中断的问题
if( USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET )
{
USART_ReceiveData(USART1);
USART_ClearFlag(USART1,USART_FLAG_ORE);
}
USART_ClearFlag(USART1,USART_IT_RXNE);
}

View File

@@ -0,0 +1,22 @@
#ifndef _BSP_UART1_H_
#define _BSP_UART1_H_
#include "stm32f10x.h"
#include "bsp_clock.h"
#include "stdio.h"
#include "string.h"
#include "stdarg.h"
#include "bsp_timer3.h"
void Sys_SendLog(const char *fmt, ...);
// 判断串口01接收完成
void BSP_UART1RecComplete(uint64_t Std_1ms);
void BSP_UART1SendStr(char *ch);
void BSP_UART1SendData(uint8_t *ch,uint16_t len);
void BSP_UART1Init(uint32_t bound);
#endif

View File

@@ -0,0 +1,111 @@
#include "bsp_uart2.h"
// 串口2 PA2 TX PA3 RX
void Dev_UART2SendStr(char* fmt,...){
uint8_t tbuf[200] = {0};
uint16_t i = 0,j = 0;
va_list ap;
va_start(ap,fmt);
vsprintf((char*)tbuf, fmt, ap);
va_end(ap);
j = strlen((const char*)tbuf);
for( i = 0; i < j; i++)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET){};
USART_SendData(USART2, tbuf[i]);
}
while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET);
}
// 发送数据
void Dev_UART2SendData(uint8_t *ch,uint16_t len){
uint16_t i = 0;
for(i = 0;i < len;i++)
{
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET){};
USART_SendData(USART2,ch[i]);
}
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
}
void BSP_UART2Init(uint32_t bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//使能USART,GPIO
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// NVIC 配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// USART 配置
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启串口接受中断
USART_Cmd(USART2, ENABLE); //使能串口
}
/*读取USARTx->SR能避免莫名其妙的错误*/
void USART2_IRQHandler(void)
{
uint8_t res = 0;
if(USART_GetITStatus(USART2, USART_IT_RXNE)!=RESET)
{
res = USART_ReceiveData(USART2);
// 是否存在数据没有处理
if( (UART2ReadFlag&0x8000)==0 )
{
UART2ReadBuf[UART2ReadFlag++] = res;
if(UART2ReadFlag>=90)
{
UART2ReadFlag |= (1 << 15);
}
}
}
/*溢出-如果发生溢出需要先读SR,再读DR寄存器则可清除不断入中断的问题*/
if( USART_GetFlagStatus(USART2, USART_FLAG_ORE)==SET )
{
USART_ReceiveData(USART2);
USART_ClearFlag(USART2, USART_FLAG_ORE);
}
USART_ClearFlag(USART2, USART_IT_RXNE);
}

View File

@@ -0,0 +1,19 @@
#ifndef _BSP_UART2_H_
#define _BSP_UART2_H_
#include "stm32f10x.h"
#include "bsp_clock.h"
#include "stdio.h"
#include "string.h"
#include "stdarg.h"
#include "bsp_timer3.h"
#include "basic_data.h"
void Dev_UART2SendStr(char* fmt,...);
void Dev_UART2SendData(uint8_t *ch, uint16_t len);
void BSP_UART2Init(uint32_t bound);
#endif

View File

@@ -0,0 +1,109 @@
#include "bsp_uart3.h"
// 串口3 PB10 TX PB11 RX
void BSP_UART3SendStr(char *ch){
uint16_t i = 0,j = 0;
j = strlen(ch);
for(i = 0;i < j;i++)
{
while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET){};
USART_SendData(USART3,ch[i]);
}
while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET);
}
// 发送数据
void BSP_UART3SendData(uint8_t *ch,uint16_t len){
uint16_t i = 0;
for(i = 0;i < len;i++)
{
while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET){};
USART_SendData(USART3,ch[i]);
}
while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET);
}
void BSP_UART3Init(uint32_t bound){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//使能USART3,GPIOB
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//USART3_TX GPIOB10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//USART3_RX GPIOB11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//Usart3 NVIC 配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART3, &USART_InitStructure);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启串口接受中断
USART_Cmd(USART3, ENABLE); //使能串口
}
// 读取USARTx->SR能避免莫名其妙的错误
uint8_t Usart3ReadBuf[60] = {0};
uint16_t Usart3ReadFlag = 0;
void USART3_IRQHandler(void)
{
uint8_t res = 0;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
res = USART_ReceiveData(USART3);
// 是否存在数据没有处理
if( (Usart3ReadFlag&0x8000)==0 )
{
Usart3ReadBuf[Usart3ReadFlag] = res;
Usart3ReadFlag++;
if(Usart3ReadFlag>=59)
{
Usart3ReadFlag |= (1 << 15);
}
}
}
//溢出-如果发生溢出需要先读SR,再读DR寄存器则可清除不断入中断的问题
if( USART_GetFlagStatus(USART3,USART_FLAG_ORE)==SET )
{
USART_ReceiveData(USART3);
USART_ClearFlag(USART3,USART_FLAG_ORE);
}
USART_ClearFlag(USART3,USART_IT_RXNE);
}

View File

@@ -0,0 +1,18 @@
#ifndef _BSP_UART3_H_
#define _BSP_UART3_H_
#include "stm32f10x.h"
#include "bsp_clock.h"
#include "stdio.h"
#include "string.h"
#include "basic_data.h"
// 判断串口03接收完成
void BSP_UART3SendStr(char *ch);
void BSP_UART3SendData(uint8_t *ch,uint16_t len);
void BSP_UART3Init(uint32_t bound);
#endif

View File

@@ -0,0 +1,67 @@
#include "main.h"
/************************
ESP8266 UART2
PMD4 UART3
************************/
int main()
{
BSP_ClockInit();
BSP_Timer3Init();
// 电源控制部分
BSP_PowerInit();
Delay_ms(200);
// 电池电压ADC
BSP_ADCInit();
// LCD
#ifdef Device_LCD
Delay_ms(500);
SimLCD_Init();
DevParam.AirInforGetTime = 3000;
DevParam.BatIconRefreshTime= 3000;
DevParam.BatInforGetTime = 3000;
DevParam.SideBeatTime = 3000;
DevParam.ShowPM1_0 = 200;
DevParam.ShowPM2_5 = 200;
DevParam.ShowPM10 = 200;
#endif
while(1)
{
// 网络配网部分
ESP8266_NetWorkFlow();
// 网络数据接收处理
ESP8266_NetReceiveInfor();
// 电池,获取电池信息
Bat_GetWorkInfor();
// 读取电路板状态
Dev_ScanExtIndicate();
// 切换工作模式
Dev_SwitchWorkMode();
// 系统正常运行处理流程
if(DevParam.RunPhase == RunPhase_Runing)
{
// 显示部分,侧边跳动
Show_BroadSideBeat();
// PMD4获取空气信息
PMD4_GetAirInfor();
}
// 显示部分,电池图标刷新
Show_BatIconRefresh();
}
}

View File

@@ -0,0 +1,28 @@
#ifndef _MAIN_H_
#define _MAIN_H_
#include "stm32f10x.h"
#include "bsp_clock.h"
#include "bsp_timer3.h"
#include "bsp_port.h"
#include "bsp_tool.h"
#include "bsp_adc.h"
#include "pm_uart3.h"
#include "uart2_receive.h"
#include "simulation_lcd.h"
#include "esp8266_work.h"
#include "dev_work.h"
#include "battery.h"
#endif

View File

@@ -0,0 +1,126 @@
#ifndef _MQTTCONNECT_H_
#define _MQTTCONNECT_H_
#include "MQTTPacket.h"
enum connack_return_codes
{
MQTT_CONNECTION_ACCEPTED = 0,
MQTT_UNNACCEPTABLE_PROTOCOL = 1,
MQTT_CLIENTID_REJECTED = 2,
MQTT_SERVER_UNAVAILABLE = 3,
MQTT_BAD_USERNAME_OR_PASSWORD = 4,
MQTT_NOT_AUTHORIZED = 5,
};
#if !defined(DLLImport)
#define DLLImport
#endif
#if !defined(DLLExport)
#define DLLExport
#endif
typedef union
{
unsigned char all; /**< all connect flags */
#if defined(REVERSED)
struct
{
unsigned int username : 1; /**< 3.1 user name */
unsigned int password : 1; /**< 3.1 password */
unsigned int willRetain : 1; /**< will retain setting */
unsigned int willQoS : 2; /**< will QoS value */
unsigned int will : 1; /**< will flag */
unsigned int cleansession : 1; /**< clean session flag */
unsigned int : 1; /**< unused */
} bits;
#else
struct
{
unsigned int : 1; /**< unused */
unsigned int cleansession : 1; /**< cleansession flag */
unsigned int will : 1; /**< will flag */
unsigned int willQoS : 2; /**< will QoS value */
unsigned int willRetain : 1; /**< will retain setting */
unsigned int password : 1; /**< 3.1 password */
unsigned int username : 1; /**< 3.1 user name */
} bits;
#endif
} MQTTConnectFlags; /**< connect flags byte */
/**
* Defines the MQTT "Last Will and Testament" (LWT) settings for
* the connect packet.
*/
typedef struct
{
/** The eyecatcher for this structure. must be MQTW. */
char struct_id[4];
/** The version number of this structure. Must be 0 */
int struct_version;
/** The LWT topic to which the LWT message will be published. */
MQTTString topicName;
/** The LWT payload. */
MQTTString message;
/**
* The retained flag for the LWT message (see MQTTAsync_message.retained).
*/
unsigned char retained;
/**
* The quality of service setting for the LWT message (see
* MQTTAsync_message.qos and @ref qos).
*/
char qos;
} MQTTPacket_willOptions;
#define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 0, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0 }
typedef struct
{
/** The eyecatcher for this structure. must be MQTC. */
char struct_id[4];
/** The version number of this structure. Must be 0 */
int struct_version;
/** Version of MQTT to be used. 3 = 3.1 4 = 3.1.1
*/
unsigned char MQTTVersion;
MQTTString clientID;
unsigned short keepAliveInterval;
unsigned char cleansession;
unsigned char willFlag;
MQTTPacket_willOptions will;
MQTTString username;
MQTTString password;
} MQTTPacket_connectData;
typedef union
{
unsigned char all; /**< all connack flags */
#if defined(REVERSED)
struct
{
unsigned int reserved : 7; /**< unused */
unsigned int sessionpresent : 1; /**< session present flag */
} bits;
#else
struct
{
unsigned int sessionpresent : 1; /**< session present flag */
unsigned int reserved: 7; /**< unused */
} bits;
#endif
} MQTTConnackFlags; /**< connack flags byte */
#define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 0, 4, {NULL, {0, NULL}}, 60, 1, 0, \
MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} }
DLLExport int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options);
DLLExport int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len);
DLLExport int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent);
DLLExport int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen);
DLLExport int MQTTSerialize_disconnect(unsigned char* buf, int buflen);
DLLExport int MQTTSerialize_pingreq(unsigned char* buf, int buflen);
#endif /* MQTTCONNECT_H_ */

View File

@@ -0,0 +1,181 @@
#include "MQTTPacket.h"
#include "StackTrace.h"
#include <string.h>
/**
* Determines the length of the MQTT connect packet that would be produced using the supplied connect options.
* @param options the options to be used to build the connect packet
* @return the length of buffer needed to contain the serialized version of the packet
*/
int MQTTSerialize_connectLength(MQTTPacket_connectData* options){
int len = 0;
FUNC_ENTRY;
if (options->MQTTVersion == 3)
len = 12; /* variable depending on MQTT or MQIsdp */
else if (options->MQTTVersion == 4)
len = 10;
len += MQTTstrlen(options->clientID)+2;
if (options->willFlag)
len += MQTTstrlen(options->will.topicName)+2 + MQTTstrlen(options->will.message)+2;
if (options->username.cstring || options->username.lenstring.data)
len += MQTTstrlen(options->username)+2;
if (options->password.cstring || options->password.lenstring.data)
len += MQTTstrlen(options->password)+2;
FUNC_EXIT_RC(len);
return len;
}
/**
* Serializes the connect options into the buffer.
* @param buf the buffer into which the packet will be serialized
* @param len the length in bytes of the supplied buffer
* @param options the options to be used to build the connect packet
* @return serialized length, or error if 0
*/
int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options){
unsigned char *ptr = buf;
MQTTHeader header = {0};
MQTTConnectFlags flags = {0};
int len = 0;
int rc = -1;
FUNC_ENTRY;
if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen)
{
rc = MQTTPACKET_BUFFER_TOO_SHORT;
goto exit;
}
header.byte = 0;
header.bits.type = CONNECT;
writeChar(&ptr, header.byte); /* write header */
ptr += MQTTPacket_encode(ptr, len); /* write remaining length */
if (options->MQTTVersion == 4)
{
writeCString(&ptr, "MQTT");
writeChar(&ptr, (char) 4);
}
else
{
writeCString(&ptr, "MQIsdp");
writeChar(&ptr, (char) 3);
}
flags.all = 0;
flags.bits.cleansession = options->cleansession;
flags.bits.will = (options->willFlag) ? 1 : 0;
if (flags.bits.will)
{
flags.bits.willQoS = options->will.qos;
flags.bits.willRetain = options->will.retained;
}
if (options->username.cstring || options->username.lenstring.data)
flags.bits.username = 1;
if (options->password.cstring || options->password.lenstring.data)
flags.bits.password = 1;
writeChar(&ptr, flags.all);
writeInt(&ptr, options->keepAliveInterval);
writeMQTTString(&ptr, options->clientID);
if (options->willFlag)
{
writeMQTTString(&ptr, options->will.topicName);
writeMQTTString(&ptr, options->will.message);
}
if (flags.bits.username)
writeMQTTString(&ptr, options->username);
if (flags.bits.password)
writeMQTTString(&ptr, options->password);
rc = ptr - buf;
exit: FUNC_EXIT_RC(rc);
return rc;
}
/**
* Deserializes the supplied (wire) buffer into connack data - return code
* @param sessionPresent the session present flag returned (only for MQTT 3.1.1)
* @param connack_rc returned integer value of the connack return code
* @param buf the raw buffer data, of the correct length determined by the remaining length field
* @param len the length in bytes of the data in the supplied buffer
* @return error code. 1 is success, 0 is failure
*/
int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen){
MQTTHeader header = {0};
unsigned char* curdata = buf;
unsigned char* enddata = NULL;
int rc = 0;
int mylen;
MQTTConnackFlags flags = {0};
FUNC_ENTRY;
header.byte = readChar(&curdata);
if (header.bits.type != CONNACK)
goto exit;
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
enddata = curdata + mylen;
if (enddata - curdata < 2)
goto exit;
flags.all = readChar(&curdata);
*sessionPresent = flags.bits.sessionpresent;
*connack_rc = readChar(&curdata);
rc = 1;
exit:
FUNC_EXIT_RC(rc);
return rc;
}
/**
* Serializes a 0-length packet into the supplied buffer, ready for writing to a socket
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer, to avoid overruns
* @param packettype the message type
* @return serialized length, or error if 0
*/
int MQTTSerialize_zero(unsigned char* buf, int buflen, unsigned char packettype){
MQTTHeader header = {0};
int rc = -1;
unsigned char *ptr = buf;
FUNC_ENTRY;
if (buflen < 2)
{
rc = MQTTPACKET_BUFFER_TOO_SHORT;
goto exit;
}
header.byte = 0;
header.bits.type = packettype;
writeChar(&ptr, header.byte); /* write header */
ptr += MQTTPacket_encode(ptr, 0); /* write remaining length */
rc = ptr - buf;
exit:
FUNC_EXIT_RC(rc);
return rc;
}
/**
* Serializes a disconnect packet into the supplied buffer, ready for writing to a socket
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer, to avoid overruns
* @return serialized length, or error if 0
*/
int MQTTSerialize_disconnect(unsigned char* buf, int buflen){
return MQTTSerialize_zero(buf, buflen, DISCONNECT);
}
/**
* Serializes a disconnect packet into the supplied buffer, ready for writing to a socket
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer, to avoid overruns
* @return serialized length, or error if 0
*/
int MQTTSerialize_pingreq(unsigned char* buf, int buflen){
return MQTTSerialize_zero(buf, buflen, PINGREQ);
}

View File

@@ -0,0 +1,125 @@
#include "StackTrace.h"
#include "MQTTPacket.h"
#include <string.h>
#define min(a, b) ((a < b) ? a : b)
/**
* Validates MQTT protocol name and version combinations
* @param protocol the MQTT protocol name as an MQTTString
* @param version the MQTT protocol version number, as in the connect packet
* @return correct MQTT combination? 1 is true, 0 is false
*/
int MQTTPacket_checkVersion(MQTTString* protocol, int version){
int rc = 0;
if (version == 3 && memcmp(protocol->lenstring.data, "MQIsdp",
min(6, protocol->lenstring.len)) == 0)
rc = 1;
else if (version == 4 && memcmp(protocol->lenstring.data, "MQTT",
min(4, protocol->lenstring.len)) == 0)
rc = 1;
return rc;
}
/**
* Deserializes the supplied (wire) buffer into connect data structure
* @param data the connect data structure to be filled out
* @param buf the raw buffer data, of the correct length determined by the remaining length field
* @param len the length in bytes of the data in the supplied buffer
* @return error code. 1 is success, 0 is failure
*/
int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len){
MQTTHeader header = {0};
MQTTConnectFlags flags = {0};
unsigned char* curdata = buf;
unsigned char* enddata = &buf[len];
int rc = 0;
MQTTString Protocol;
int version;
int mylen = 0;
FUNC_ENTRY;
header.byte = readChar(&curdata);
if (header.bits.type != CONNECT)
goto exit;
curdata += MQTTPacket_decodeBuf(curdata, &mylen); /* read remaining length */
if (!readMQTTLenString(&Protocol, &curdata, enddata) ||
enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */
goto exit;
version = (int)readChar(&curdata); /* Protocol version */
/* If we don't recognize the protocol version, we don't parse the connect packet on the
* basis that we don't know what the format will be.
*/
if (MQTTPacket_checkVersion(&Protocol, version))
{
flags.all = readChar(&curdata);
data->cleansession = flags.bits.cleansession;
data->keepAliveInterval = readInt(&curdata);
if (!readMQTTLenString(&data->clientID, &curdata, enddata))
goto exit;
data->willFlag = flags.bits.will;
if (flags.bits.will)
{
data->will.qos = flags.bits.willQoS;
data->will.retained = flags.bits.willRetain;
if (!readMQTTLenString(&data->will.topicName, &curdata, enddata) ||
!readMQTTLenString(&data->will.message, &curdata, enddata))
goto exit;
}
if (flags.bits.username)
{
if (enddata - curdata < 3 || !readMQTTLenString(&data->username, &curdata, enddata))
goto exit; /* username flag set, but no username supplied - invalid */
if (flags.bits.password &&
(enddata - curdata < 3 || !readMQTTLenString(&data->password, &curdata, enddata)))
goto exit; /* password flag set, but no password supplied - invalid */
}
else if (flags.bits.password)
goto exit; /* password flag set without username - invalid */
rc = 1;
}
exit:
FUNC_EXIT_RC(rc);
return rc;
}
/**
* Serializes the connack packet into the supplied buffer.
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer
* @param connack_rc the integer connack return code to be used
* @param sessionPresent the MQTT 3.1.1 sessionPresent flag
* @return serialized length, or error if 0
*/
int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent){
MQTTHeader header = {0};
int rc = 0;
unsigned char *ptr = buf;
MQTTConnackFlags flags = {0};
FUNC_ENTRY;
if (buflen < 2)
{
rc = MQTTPACKET_BUFFER_TOO_SHORT;
goto exit;
}
header.byte = 0;
header.bits.type = CONNACK;
writeChar(&ptr, header.byte); /* write header */
ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
flags.all = 0;
flags.bits.sessionpresent = sessionPresent;
writeChar(&ptr, flags.all);
writeChar(&ptr, connack_rc);
rc = ptr - buf;
exit:
FUNC_EXIT_RC(rc);
return rc;
}

View File

@@ -0,0 +1,105 @@
/*******************************************************************************
* Copyright (c) 2014 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
*******************************************************************************/
#include "StackTrace.h"
#include "MQTTPacket.h"
#include <string.h>
#define min(a, b) ((a < b) ? 1 : 0)
/**
* Deserializes the supplied (wire) buffer into publish data
* @param dup returned integer - the MQTT dup flag
* @param qos returned integer - the MQTT QoS value
* @param retained returned integer - the MQTT retained flag
* @param packetid returned integer - the MQTT packet identifier
* @param topicName returned MQTTString - the MQTT topic in the publish
* @param payload returned byte buffer - the MQTT publish payload
* @param payloadlen returned integer - the length of the MQTT payload
* @param buf the raw buffer data, of the correct length determined by the remaining length field
* @param buflen the length in bytes of the data in the supplied buffer
* @return error code. 1 is success
*/
int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName,
unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen){
MQTTHeader header = {0};
unsigned char* curdata = buf;
unsigned char* enddata = NULL;
int rc = 0;
int mylen = 0;
FUNC_ENTRY;
header.byte = readChar(&curdata);
if (header.bits.type != PUBLISH)
goto exit;
*dup = header.bits.dup;
*qos = header.bits.qos;
*retained = header.bits.retain;
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
enddata = curdata + mylen;
if (!readMQTTLenString(topicName, &curdata, enddata) ||
enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */
goto exit;
if (*qos > 0)
*packetid = readInt(&curdata);
*payloadlen = enddata - curdata;
*payload = curdata;
rc = 1;
exit:
FUNC_EXIT_RC(rc);
return rc;
}
/**
* Deserializes the supplied (wire) buffer into an ack
* @param packettype returned integer - the MQTT packet type
* @param dup returned integer - the MQTT dup flag
* @param packetid returned integer - the MQTT packet identifier
* @param buf the raw buffer data, of the correct length determined by the remaining length field
* @param buflen the length in bytes of the data in the supplied buffer
* @return error code. 1 is success, 0 is failure
*/
int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen){
MQTTHeader header = {0};
unsigned char* curdata = buf;
unsigned char* enddata = NULL;
int rc = 0;
int mylen;
FUNC_ENTRY;
header.byte = readChar(&curdata);
*dup = header.bits.dup;
*packettype = header.bits.type;
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
enddata = curdata + mylen;
if (enddata - curdata < 2)
goto exit;
*packetid = readInt(&curdata);
rc = 1;
exit:
FUNC_EXIT_RC(rc);
return rc;
}

View File

@@ -0,0 +1,233 @@
#include "StackTrace.h"
#include "MQTTPacket.h"
#include "string.h"
const char* MQTTPacket_names[] =
{
"RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL",
"PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK",
"PINGREQ", "PINGRESP", "DISCONNECT"
};
const char* MQTTPacket_getName(unsigned short packetid) {
return MQTTPacket_names[packetid];
}
int MQTTStringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data){
int strindex = 0;
strindex = snprintf(strbuf, strbuflen,
"CONNECT MQTT version %d, client id %.*s, clean session %d, keep alive %d",
(int)data->MQTTVersion, data->clientID.lenstring.len, data->clientID.lenstring.data,
(int)data->cleansession, data->keepAliveInterval);
if (data->willFlag)
strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
", will QoS %d, will retain %d, will topic %.*s, will message %.*s",
data->will.qos, data->will.retained,
data->will.topicName.lenstring.len, data->will.topicName.lenstring.data,
data->will.message.lenstring.len, data->will.message.lenstring.data);
if (data->username.lenstring.data && data->username.lenstring.len > 0)
strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
", user name %.*s", data->username.lenstring.len, data->username.lenstring.data);
if (data->password.lenstring.data && data->password.lenstring.len > 0)
strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
", password %.*s", data->password.lenstring.len, data->password.lenstring.data);
return strindex;
}
int MQTTStringFormat_connack(char* strbuf, int strbuflen, unsigned char connack_rc, unsigned char sessionPresent){
int strindex = snprintf(strbuf, strbuflen, "CONNACK session present %d, rc %d", sessionPresent, connack_rc);
return strindex;
}
int MQTTStringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int qos, unsigned char retained,
unsigned short packetid, MQTTString topicName, unsigned char* payload, int payloadlen){
int strindex = snprintf(strbuf, strbuflen,
"PUBLISH dup %d, QoS %d, retained %d, packet id %d, topic %.*s, payload length %d, payload %.*s",
dup, qos, retained, packetid,
(topicName.lenstring.len < 20) ? topicName.lenstring.len : 20, topicName.lenstring.data,
payloadlen, (payloadlen < 20) ? payloadlen : 20, payload);
return strindex;
}
int MQTTStringFormat_ack(char* strbuf, int strbuflen, unsigned char packettype, unsigned char dup, unsigned short packetid){
int strindex = snprintf(strbuf, strbuflen, "%s, packet id %d", MQTTPacket_names[packettype], packetid);
if (dup)
strindex += snprintf(strbuf + strindex, strbuflen - strindex, ", dup %d", dup);
return strindex;
}
int MQTTStringFormat_subscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, int count,
MQTTString topicFilters[], int requestedQoSs[]){
return snprintf(strbuf, strbuflen,
"SUBSCRIBE dup %d, packet id %d count %d topic %.*s qos %d",
dup, packetid, count,
topicFilters[0].lenstring.len, topicFilters[0].lenstring.data,
requestedQoSs[0]);
}
int MQTTStringFormat_suback(char* strbuf, int strbuflen, unsigned short packetid, int count, int* grantedQoSs){
return snprintf(strbuf, strbuflen,
"SUBACK packet id %d count %d granted qos %d", packetid, count, grantedQoSs[0]);
}
int MQTTStringFormat_unsubscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid,
int count, MQTTString topicFilters[]){
return snprintf(strbuf, strbuflen,
"UNSUBSCRIBE dup %d, packet id %d count %d topic %.*s",
dup, packetid, count,
topicFilters[0].lenstring.len, topicFilters[0].lenstring.data);
}
#if defined(MQTT_CLIENT)
char* MQTTFormat_toClientString(char* strbuf, int strbuflen, unsigned char* buf, int buflen){
int index = 0;
int rem_length = 0;
MQTTHeader header = {0};
int strindex = 0;
header.byte = buf[index++];
index += MQTTPacket_decodeBuf(&buf[index], &rem_length);
switch (header.bits.type)
{
case CONNACK:
{
unsigned char sessionPresent, connack_rc;
if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) == 1)
strindex = MQTTStringFormat_connack(strbuf, strbuflen, connack_rc, sessionPresent);
}
break;
case PUBLISH:
{
unsigned char dup, retained, *payload;
unsigned short packetid;
int qos, payloadlen;
MQTTString topicName = MQTTString_initializer;
if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName,
&payload, &payloadlen, buf, buflen) == 1)
strindex = MQTTStringFormat_publish(strbuf, strbuflen, dup, qos, retained, packetid,
topicName, payload, payloadlen);
}
break;
case PUBACK:
case PUBREC:
case PUBREL:
case PUBCOMP:
{
unsigned char packettype, dup;
unsigned short packetid;
if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1)
strindex = MQTTStringFormat_ack(strbuf, strbuflen, packettype, dup, packetid);
}
break;
case SUBACK:
{
unsigned short packetid;
int maxcount = 1, count = 0;
int grantedQoSs[1];
if (MQTTDeserialize_suback(&packetid, maxcount, &count, grantedQoSs, buf, buflen) == 1)
strindex = MQTTStringFormat_suback(strbuf, strbuflen, packetid, count, grantedQoSs);
}
break;
case UNSUBACK:
{
unsigned short packetid;
if (MQTTDeserialize_unsuback(&packetid, buf, buflen) == 1)
strindex = MQTTStringFormat_ack(strbuf, strbuflen, UNSUBACK, 0, packetid);
}
break;
case PINGREQ:
case PINGRESP:
case DISCONNECT:
strindex = snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]);
break;
}
return strbuf;
}
#endif
#if defined(MQTT_SERVER)
char* MQTTFormat_toServerString(char* strbuf, int strbuflen, unsigned char* buf, int buflen){
int index = 0;
int rem_length = 0;
MQTTHeader header = {0};
int strindex = 0;
header.byte = buf[index++];
index += MQTTPacket_decodeBuf(&buf[index], &rem_length);
switch (header.bits.type)
{
case CONNECT:
{
MQTTPacket_connectData data;
int rc;
if ((rc = MQTTDeserialize_connect(&data, buf, buflen)) == 1)
strindex = MQTTStringFormat_connect(strbuf, strbuflen, &data);
}
break;
case PUBLISH:
{
unsigned char dup, retained, *payload;
unsigned short packetid;
int qos, payloadlen;
MQTTString topicName = MQTTString_initializer;
if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName,
&payload, &payloadlen, buf, buflen) == 1)
strindex = MQTTStringFormat_publish(strbuf, strbuflen, dup, qos, retained, packetid,
topicName, payload, payloadlen);
}
break;
case PUBACK:
case PUBREC:
case PUBREL:
case PUBCOMP:
{
unsigned char packettype, dup;
unsigned short packetid;
if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1)
strindex = MQTTStringFormat_ack(strbuf, strbuflen, packettype, dup, packetid);
}
break;
case SUBSCRIBE:
{
unsigned char dup;
unsigned short packetid;
int maxcount = 1, count = 0;
MQTTString topicFilters[1];
int requestedQoSs[1];
if (MQTTDeserialize_subscribe(&dup, &packetid, maxcount, &count,
topicFilters, requestedQoSs, buf, buflen) == 1)
strindex = MQTTStringFormat_subscribe(strbuf, strbuflen, dup, packetid, count, topicFilters, requestedQoSs);;
}
break;
case UNSUBSCRIBE:
{
unsigned char dup;
unsigned short packetid;
int maxcount = 1, count = 0;
MQTTString topicFilters[1];
if (MQTTDeserialize_unsubscribe(&dup, &packetid, maxcount, &count, topicFilters, buf, buflen) == 1)
strindex = MQTTStringFormat_unsubscribe(strbuf, strbuflen, dup, packetid, count, topicFilters);
}
break;
case PINGREQ:
case PINGRESP:
case DISCONNECT:
strindex = snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]);
break;
}
strbuf[strbuflen] = '\0';
return strbuf;
}
#endif

View File

@@ -0,0 +1,37 @@
/*******************************************************************************
* Copyright (c) 2014 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
*******************************************************************************/
#if !defined(_MQTTFORMAT_H_)
#define _MQTTFORMAT_H_
#include "StackTrace.h"
#include "MQTTPacket.h"
const char* MQTTPacket_getName(unsigned short packetid);
int MQTTStringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data);
int MQTTStringFormat_connack(char* strbuf, int strbuflen, unsigned char connack_rc, unsigned char sessionPresent);
int MQTTStringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int qos, unsigned char retained,
unsigned short packetid, MQTTString topicName, unsigned char* payload, int payloadlen);
int MQTTStringFormat_ack(char* strbuf, int strbuflen, unsigned char packettype, unsigned char dup, unsigned short packetid);
int MQTTStringFormat_subscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, int count,
MQTTString topicFilters[], int requestedQoSs[]);
int MQTTStringFormat_suback(char* strbuf, int strbuflen, unsigned short packetid, int count, int* grantedQoSs);
int MQTTStringFormat_unsubscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid,
int count, MQTTString topicFilters[]);
char* MQTTFormat_toClientString(char* strbuf, int strbuflen, unsigned char* buf, int buflen);
char* MQTTFormat_toServerString(char* strbuf, int strbuflen, unsigned char* buf, int buflen);
#endif

View File

@@ -0,0 +1,365 @@
#include "StackTrace.h"
#include "MQTTPacket.h"
#include <string.h>
/**
* Encodes the message length according to the MQTT algorithm
* @param buf the buffer into which the encoded data is written
* @param length the length to be encoded
* @return the number of bytes written to buffer
*/
int MQTTPacket_encode(unsigned char* buf, int length){
int rc = 0;
FUNC_ENTRY;
do
{
char d = length % 128;
length /= 128;
/* if there are more digits to encode, set the top bit of this digit */
if (length > 0)
d |= 0x80;
buf[rc++] = d;
} while (length > 0);
FUNC_EXIT_RC(rc);
return rc;
}
/**
* Decodes the message length according to the MQTT algorithm
* @param getcharfn pointer to function to read the next character from the data source
* @param value the decoded length returned
* @return the number of bytes read from the socket
*/
int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value){
unsigned char c;
int multiplier = 1;
int len = 0;
#define MAX_NO_OF_REMAINING_LENGTH_BYTES 4
FUNC_ENTRY;
*value = 0;
do
{
int rc = MQTTPACKET_READ_ERROR;
if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES)
{
rc = MQTTPACKET_READ_ERROR; /* bad data */
goto exit;
}
rc = (*getcharfn)(&c, 1);
if (rc != 1)
goto exit;
*value += (c & 127) * multiplier;
multiplier *= 128;
} while ((c & 128) != 0);
exit:
FUNC_EXIT_RC(len);
return len;
}
int MQTTPacket_len(int rem_len){
rem_len += 1; /* header byte */
/* now remaining_length field */
if (rem_len < 128)
rem_len += 1;
else if (rem_len < 16384)
rem_len += 2;
else if (rem_len < 2097151)
rem_len += 3;
else
rem_len += 4;
return rem_len;
}
static unsigned char* bufptr;
int bufchar(unsigned char* c, int count){
int i;
for (i = 0; i < count; ++i)
*c = *bufptr++;
return count;
}
int MQTTPacket_decodeBuf(unsigned char* buf, int* value){
bufptr = buf;
return MQTTPacket_decode(bufchar, value);
}
/**
* Calculates an integer from two bytes read from the input buffer
* @param pptr pointer to the input buffer - incremented by the number of bytes used & returned
* @return the integer value calculated
*/
int readInt(unsigned char** pptr){
unsigned char* ptr = *pptr;
int len = 256*(*ptr) + (*(ptr+1));
*pptr += 2;
return len;
}
/**
* Reads one character from the input buffer.
* @param pptr pointer to the input buffer - incremented by the number of bytes used & returned
* @return the character read
*/
char readChar(unsigned char** pptr){
char c = **pptr;
(*pptr)++;
return c;
}
/**
* Writes one character to an output buffer.
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
* @param c the character to write
*/
void writeChar(unsigned char** pptr, char c){
**pptr = c;
(*pptr)++;
}
/**
* Writes an integer as 2 bytes to an output buffer.
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
* @param anInt the integer to write
*/
void writeInt(unsigned char** pptr, int anInt){
**pptr = (unsigned char)(anInt / 256);
(*pptr)++;
**pptr = (unsigned char)(anInt % 256);
(*pptr)++;
}
/**
* Writes a "UTF" string to an output buffer. Converts C string to length-delimited.
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
* @param string the C string to write
*/
void writeCString(unsigned char** pptr, const char* string){
int len = strlen(string);
writeInt(pptr, len);
memcpy(*pptr, string, len);
*pptr += len;
}
int getLenStringLen(char* ptr){
int len = 256*((unsigned char)(*ptr)) + (unsigned char)(*(ptr+1));
return len;
}
void writeMQTTString(unsigned char** pptr, MQTTString mqttstring){
if (mqttstring.lenstring.len > 0)
{
writeInt(pptr, mqttstring.lenstring.len);
memcpy(*pptr, mqttstring.lenstring.data, mqttstring.lenstring.len);
*pptr += mqttstring.lenstring.len;
}
else if (mqttstring.cstring)
writeCString(pptr, mqttstring.cstring);
else
writeInt(pptr, 0);
}
/**
* @param mqttstring the MQTTString structure into which the data is to be read
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
* @param enddata pointer to the end of the data: do not read beyond
* @return 1 if successful, 0 if not
*/
int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata){
int rc = 0;
FUNC_ENTRY;
/* the first two bytes are the length of the string */
if (enddata - (*pptr) > 1) /* enough length to read the integer? */
{
mqttstring->lenstring.len = readInt(pptr); /* increments pptr to point past length */
if (&(*pptr)[mqttstring->lenstring.len] <= enddata)
{
mqttstring->lenstring.data = (char*)*pptr;
*pptr += mqttstring->lenstring.len;
rc = 1;
}
}
mqttstring->cstring = NULL;
FUNC_EXIT_RC(rc);
return rc;
}
/**
* Return the length of the MQTTstring - C string if there is one, otherwise the length delimited string
* @param mqttstring the string to return the length of
* @return the length of the string
*/
int MQTTstrlen(MQTTString mqttstring){
int rc = 0;
if (mqttstring.cstring)
rc = strlen(mqttstring.cstring);
else
rc = mqttstring.lenstring.len;
return rc;
}
/**
* Compares an MQTTString to a C string
* @param a the MQTTString to compare
* @param bptr the C string to compare
* @return boolean - equal or not
*/
int MQTTPacket_equals(MQTTString* a, char* bptr){
int alen = 0,
blen = 0;
char *aptr;
if (a->cstring)
{
aptr = a->cstring;
alen = strlen(a->cstring);
}
else
{
aptr = a->lenstring.data;
alen = a->lenstring.len;
}
blen = strlen(bptr);
return (alen == blen) && (strncmp(aptr, bptr, alen) == 0);
}
/**
* Helper function to read packet data from some source into a buffer
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer
* @param getfn pointer to a function which will read any number of bytes from the needed source
* @return integer MQTT packet type, or -1 on error
* @note the whole message must fit into the caller's buffer
*/
int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int)){
int rc = -1;
MQTTHeader header = {0};
int len = 0;
int rem_len = 0;
/* 1. 读取头字节。这里面有包类型 */
if ((*getfn)(buf, 1) != 1)
goto exit;
len = 1;
// 解码
/* 2. 读剩下的长度。它本身是可变的 */
MQTTPacket_decode(getfn, &rem_len);
/* 将原始的剩余长度放回缓冲区 */
len += MQTTPacket_encode(buf + 1, rem_len);
/* 3. read the rest of the buffer using a callback to supply the rest of the data */
if((rem_len + len) > buflen)
goto exit;
if (rem_len && ((*getfn)(buf + len, rem_len) != rem_len))
goto exit;
header.byte = buf[0];
rc = header.bits.type;
exit:
return rc;
}
/**
* Decodes the message length according to the MQTT algorithm, non-blocking
* @param trp pointer to a transport structure holding what is needed to solve getting data from it
* @param value the decoded length returned
* @return integer the number of bytes read from the socket, 0 for call again, or -1 on error
*/
static int MQTTPacket_decodenb(MQTTTransport *trp){
unsigned char c;
int rc = MQTTPACKET_READ_ERROR;
FUNC_ENTRY;
if(trp->len == 0){ /* initialize on first call */
trp->multiplier = 1;
trp->rem_len = 0;
}
do {
int frc;
if (trp->len >= MAX_NO_OF_REMAINING_LENGTH_BYTES)
goto exit;
if ((frc=(*trp->getfn)(trp->sck, &c, 1)) == -1)
goto exit;
if (frc == 0){
rc = 0;
goto exit;
}
++(trp->len);
trp->rem_len += (c & 127) * trp->multiplier;
trp->multiplier *= 128;
} while ((c & 128) != 0);
rc = trp->len;
exit:
FUNC_EXIT_RC(rc);
return rc;
}
/**
* Helper function to read packet data from some source into a buffer, non-blocking
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer
* @param trp pointer to a transport structure holding what is needed to solve getting data from it
* @return integer MQTT packet type, 0 for call again, or -1 on error
* @note the whole message must fit into the caller's buffer
*/
int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp){
int rc = -1, frc;
MQTTHeader header = {0};
switch(trp->state){
default:
trp->state = 0;
/*FALLTHROUGH*/
case 0:
/* read the header byte. This has the packet type in it */
if ((frc=(*trp->getfn)(trp->sck, buf, 1)) == -1)
goto exit;
if (frc == 0)
return 0;
trp->len = 0;
++trp->state;
/*FALLTHROUGH*/
/* read the remaining length. This is variable in itself */
case 1:
if((frc=MQTTPacket_decodenb(trp)) == MQTTPACKET_READ_ERROR)
goto exit;
if(frc == 0)
return 0;
trp->len = 1 + MQTTPacket_encode(buf + 1, trp->rem_len); /* put the original remaining length back into the buffer */
if((trp->rem_len + trp->len) > buflen)
goto exit;
++trp->state;
/*FALLTHROUGH*/
case 2:
if(trp->rem_len){
/* read the rest of the buffer using a callback to supply the rest of the data */
if ((frc=(*trp->getfn)(trp->sck, buf + trp->len, trp->rem_len)) == -1)
goto exit;
if (frc == 0)
return 0;
trp->rem_len -= frc;
trp->len += frc;
if(trp->rem_len)
return 0;
}
header.byte = buf[0];
rc = header.bits.type;
break;
}
exit:
trp->state = 0;
return rc;
}

View File

@@ -0,0 +1,116 @@
#ifndef _MQTTPACKET_H_
#define _MQTTPACKET_H_
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
#if defined(WIN32_DLL) || defined(WIN64_DLL)
#define DLLImport __declspec(dllimport)
#define DLLExport __declspec(dllexport)
#elif defined(LINUX_SO)
#define DLLImport extern
#define DLLExport __attribute__ ((visibility ("default")))
#else
#define DLLImport
#define DLLExport
#endif
enum errors
{
MQTTPACKET_BUFFER_TOO_SHORT = -2,
MQTTPACKET_READ_ERROR = -1,
MQTTPACKET_READ_COMPLETE
};
enum msgTypes
{
CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL,
PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK,
PINGREQ, PINGRESP, DISCONNECT
};
/**
* Bitfields for the MQTT header byte.
*/
typedef union
{
unsigned char byte; /**< the whole byte */
#if defined(REVERSED)
struct
{
unsigned int type : 4; /**< message type nibble */
unsigned int dup : 1; /**< DUP flag bit */
unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
unsigned int retain : 1; /**< retained flag bit */
} bits;
#else
struct
{
unsigned int retain : 1; /**< retained flag bit */
unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
unsigned int dup : 1; /**< DUP flag bit */
unsigned int type : 4; /**< message type nibble */
} bits;
#endif
} MQTTHeader;
typedef struct
{
int len;
char* data;
} MQTTLenString;
typedef struct
{
char* cstring;
MQTTLenString lenstring;
} MQTTString;
#define MQTTString_initializer {NULL, {0, NULL}}
int MQTTstrlen(MQTTString mqttstring);
#include "MQTTConnect.h"
#include "MQTTPublish.h"
#include "MQTTSubscribe.h"
#include "MQTTUnsubscribe.h"
#include "MQTTFormat.h"
DLLExport int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid);
DLLExport int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen);
int MQTTPacket_len(int rem_len);
DLLExport int MQTTPacket_equals(MQTTString* a, char* b);
DLLExport int MQTTPacket_encode(unsigned char* buf, int length);
int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value);
int MQTTPacket_decodeBuf(unsigned char* buf, int* value);
int readInt(unsigned char** pptr);
char readChar(unsigned char** pptr);
void writeChar(unsigned char** pptr, char c);
void writeInt(unsigned char** pptr, int anInt);
int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata);
void writeCString(unsigned char** pptr, const char* string);
void writeMQTTString(unsigned char** pptr, MQTTString mqttstring);
DLLExport int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int));
typedef struct {
int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */
void *sck; /* pointer to whatever the system may use to identify the transport */
int multiplier;
int rem_len;
int len;
char state;
}MQTTTransport;
int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp);
#ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
}
#endif
#endif /* MQTTPACKET_H_ */

View File

@@ -0,0 +1,38 @@
/*******************************************************************************
* Copyright (c) 2014 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
* Xiang Rong - 442039 Add makefile to Embedded C client
*******************************************************************************/
#ifndef MQTTPUBLISH_H_
#define MQTTPUBLISH_H_
#if !defined(DLLImport)
#define DLLImport
#endif
#if !defined(DLLExport)
#define DLLExport
#endif
DLLExport int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid,
MQTTString topicName, unsigned char* payload, int payloadlen);
DLLExport int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName,
unsigned char** payload, int* payloadlen, unsigned char* buf, int len);
DLLExport int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid);
DLLExport int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid);
DLLExport int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid);
#endif /* MQTTPUBLISH_H_ */

View File

@@ -0,0 +1,145 @@
#include "MQTTPacket.h"
#include "StackTrace.h"
#include <string.h>
/**
* Determines the length of the MQTT publish packet that would be produced using the supplied parameters
* @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0)
* @param topicName the topic name to be used in the publish
* @param payloadlen the length of the payload to be sent
* @return the length of buffer needed to contain the serialized version of the packet
*/
int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen){
int len = 0;
len += 2 + MQTTstrlen(topicName) + payloadlen;
if (qos > 0)
len += 2; /* packetid */
return len;
}
/**
* Serializes the supplied publish data into the supplied buffer, ready for sending
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer
* @param dup integer - the MQTT dup flag
* @param qos integer - the MQTT QoS value
* @param retained integer - the MQTT retained flag
* @param packetid integer - the MQTT packet identifier
* @param topicName MQTTString - the MQTT topic in the publish
* @param payload byte buffer - the MQTT publish payload
* @param payloadlen integer - the length of the MQTT payload
* @return the length of the serialized data. <= 0 indicates error
*/
int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid,
MQTTString topicName, unsigned char* payload, int payloadlen){
unsigned char *ptr = buf;
MQTTHeader header = {0};
int rem_len = 0;
int rc = 0;
FUNC_ENTRY;
if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen)
{
rc = MQTTPACKET_BUFFER_TOO_SHORT;
goto exit;
}
header.bits.type = PUBLISH;
header.bits.dup = dup;
header.bits.qos = qos;
header.bits.retain = retained;
writeChar(&ptr, header.byte); /* write header */
ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
writeMQTTString(&ptr, topicName);
if (qos > 0)
writeInt(&ptr, packetid);
memcpy(ptr, payload, payloadlen);
ptr += payloadlen;
rc = ptr - buf;
exit:
FUNC_EXIT_RC(rc);
return rc;
}
/**
* Serializes the ack packet into the supplied buffer.
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer
* @param type the MQTT packet type
* @param dup the MQTT dup flag
* @param packetid the MQTT packet identifier
* @return serialized length, or error if 0
*/
int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid){
MQTTHeader header = {0};
int rc = 0;
unsigned char *ptr = buf;
FUNC_ENTRY;
if (buflen < 4)
{
rc = MQTTPACKET_BUFFER_TOO_SHORT;
goto exit;
}
header.bits.type = packettype;
header.bits.dup = dup;
header.bits.qos = (packettype == PUBREL) ? 1 : 0;
writeChar(&ptr, header.byte); /* write header */
ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
writeInt(&ptr, packetid);
rc = ptr - buf;
exit:
FUNC_EXIT_RC(rc);
return rc;
}
/**
* Serializes a puback packet into the supplied buffer.
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer
* @param packetid integer - the MQTT packet identifier
* @return serialized length, or error if 0
*/
int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid){
return MQTTSerialize_ack(buf, buflen, PUBACK, 0, packetid);
}
/**
* Serializes a pubrel packet into the supplied buffer.
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer
* @param dup integer - the MQTT dup flag
* @param packetid integer - the MQTT packet identifier
* @return serialized length, or error if 0
*/
int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid){
return MQTTSerialize_ack(buf, buflen, PUBREL, dup, packetid);
}
/**
* Serializes a pubrel packet into the supplied buffer.
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer
* @param packetid integer - the MQTT packet identifier
* @return serialized length, or error if 0
*/
int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid){
return MQTTSerialize_ack(buf, buflen, PUBCOMP, 0, packetid);
}

View File

@@ -0,0 +1,39 @@
/*******************************************************************************
* Copyright (c) 2014 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
* Xiang Rong - 442039 Add makefile to Embedded C client
*******************************************************************************/
#ifndef MQTTSUBSCRIBE_H_
#define MQTTSUBSCRIBE_H_
#if !defined(DLLImport)
#define DLLImport
#endif
#if !defined(DLLExport)
#define DLLExport
#endif
DLLExport int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
int count, MQTTString topicFilters[], int requestedQoSs[]);
DLLExport int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid,
int maxcount, int* count, MQTTString topicFilters[], int requestedQoSs[], unsigned char* buf, int len);
DLLExport int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs);
DLLExport int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int len);
#endif /* MQTTSUBSCRIBE_H_ */

View File

@@ -0,0 +1,118 @@
#include "MQTTPacket.h"
#include "StackTrace.h"
#include <string.h>
/**
* Determines the length of the MQTT subscribe packet that would be produced using the supplied parameters
* @param count the number of topic filter strings in topicFilters
* @param topicFilters the array of topic filter strings to be used in the publish
* @return the length of buffer needed to contain the serialized version of the packet
*/
int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[]){
int i;
int len = 2; /* packetid */
for (i = 0; i < count; ++i)
len += 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */
return len;
}
/**
* Serializes the supplied subscribe data into the supplied buffer, ready for sending
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied bufferr
* @param dup integer - the MQTT dup flag
* @param packetid integer - the MQTT packet identifier
* @param count - number of members in the topicFilters and reqQos arrays
* @param topicFilters - array of topic filter names
* @param requestedQoSs - array of requested QoS
* @return the length of the serialized data. <= 0 indicates error
*/
int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, int count,
MQTTString topicFilters[], int requestedQoSs[]){
unsigned char *ptr = buf;
MQTTHeader header = {0};
int rem_len = 0;
int rc = 0;
int i = 0;
FUNC_ENTRY;
if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen)
{
rc = MQTTPACKET_BUFFER_TOO_SHORT;
goto exit;
}
header.byte = 0;
header.bits.type = SUBSCRIBE;
header.bits.dup = dup;
header.bits.qos = 1;
writeChar(&ptr, header.byte); /* write header */
ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
writeInt(&ptr, packetid);
for (i = 0; i < count; ++i)
{
writeMQTTString(&ptr, topicFilters[i]);
writeChar(&ptr, requestedQoSs[i]);
}
rc = ptr - buf;
exit:
FUNC_EXIT_RC(rc);
return rc;
}
/**
* Deserializes the supplied (wire) buffer into suback data
* @param packetid returned integer - the MQTT packet identifier
* @param maxcount - the maximum number of members allowed in the grantedQoSs array
* @param count returned integer - number of members in the grantedQoSs array
* @param grantedQoSs returned array of integers - the granted qualities of service
* @param buf the raw buffer data, of the correct length determined by the remaining length field
* @param buflen the length in bytes of the data in the supplied buffer
* @return error code. 1 is success, 0 is failure
*/
int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int buflen){
MQTTHeader header = {0};
unsigned char* curdata = buf;
unsigned char* enddata = NULL;
int rc = 0;
int mylen;
FUNC_ENTRY;
header.byte = readChar(&curdata);
if (header.bits.type != SUBACK)
goto exit;
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
enddata = curdata + mylen;
if (enddata - curdata < 2)
goto exit;
*packetid = readInt(&curdata);
*count = 0;
while (curdata < enddata)
{
if (*count > maxcount)
{
rc = -1;
goto exit;
}
grantedQoSs[(*count)++] = readChar(&curdata);
}
rc = 1;
exit:
FUNC_EXIT_RC(rc);
return rc;
}

View File

@@ -0,0 +1,92 @@
#include "MQTTPacket.h"
#include "StackTrace.h"
#include <string.h>
/**
* Deserializes the supplied (wire) buffer into subscribe data
* @param dup integer returned - the MQTT dup flag
* @param packetid integer returned - the MQTT packet identifier
* @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays
* @param count - number of members in the topicFilters and requestedQoSs arrays
* @param topicFilters - array of topic filter names
* @param requestedQoSs - array of requested QoS
* @param buf the raw buffer data, of the correct length determined by the remaining length field
* @param buflen the length in bytes of the data in the supplied buffer
* @return the length of the serialized data. <= 0 indicates error
*/
int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[],
int requestedQoSs[], unsigned char* buf, int buflen){
MQTTHeader header = {0};
unsigned char* curdata = buf;
unsigned char* enddata = NULL;
int rc = -1;
int mylen = 0;
FUNC_ENTRY;
header.byte = readChar(&curdata);
if (header.bits.type != SUBSCRIBE)
goto exit;
*dup = header.bits.dup;
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
enddata = curdata + mylen;
*packetid = readInt(&curdata);
*count = 0;
while (curdata < enddata)
{
if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
goto exit;
if (curdata >= enddata) /* do we have enough data to read the req_qos version byte? */
goto exit;
requestedQoSs[*count] = readChar(&curdata);
(*count)++;
}
rc = 1;
exit:
FUNC_EXIT_RC(rc);
return rc;
}
/**
* Serializes the supplied suback data into the supplied buffer, ready for sending
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer
* @param packetid integer - the MQTT packet identifier
* @param count - number of members in the grantedQoSs array
* @param grantedQoSs - array of granted QoS
* @return the length of the serialized data. <= 0 indicates error
*/
int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs){
MQTTHeader header = {0};
int rc = -1;
unsigned char *ptr = buf;
int i;
FUNC_ENTRY;
if (buflen < 2 + count)
{
rc = MQTTPACKET_BUFFER_TOO_SHORT;
goto exit;
}
header.byte = 0;
header.bits.type = SUBACK;
writeChar(&ptr, header.byte); /* write header */
ptr += MQTTPacket_encode(ptr, 2 + count); /* write remaining length */
writeInt(&ptr, packetid);
for (i = 0; i < count; ++i)
writeChar(&ptr, grantedQoSs[i]);
rc = ptr - buf;
exit:
FUNC_EXIT_RC(rc);
return rc;
}

View File

@@ -0,0 +1,38 @@
/*******************************************************************************
* Copyright (c) 2014 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
* Xiang Rong - 442039 Add makefile to Embedded C client
*******************************************************************************/
#ifndef MQTTUNSUBSCRIBE_H_
#define MQTTUNSUBSCRIBE_H_
#if !defined(DLLImport)
#define DLLImport
#endif
#if !defined(DLLExport)
#define DLLExport
#endif
DLLExport int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
int count, MQTTString topicFilters[]);
DLLExport int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int max_count, int* count, MQTTString topicFilters[],
unsigned char* buf, int len);
DLLExport int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid);
DLLExport int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int len);
#endif /* MQTTUNSUBSCRIBE_H_ */

View File

@@ -0,0 +1,87 @@
#include "MQTTPacket.h"
#include "StackTrace.h"
#include <string.h>
/**
* Determines the length of the MQTT unsubscribe packet that would be produced using the supplied parameters
* @param count the number of topic filter strings in topicFilters
* @param topicFilters the array of topic filter strings to be used in the publish
* @return the length of buffer needed to contain the serialized version of the packet
*/
int MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[]){
int i;
int len = 2; /* packetid */
for (i = 0; i < count; ++i)
len += 2 + MQTTstrlen(topicFilters[i]); /* length + topic*/
return len;
}
/**
* Serializes the supplied unsubscribe data into the supplied buffer, ready for sending
* @param buf the raw buffer data, of the correct length determined by the remaining length field
* @param buflen the length in bytes of the data in the supplied buffer
* @param dup integer - the MQTT dup flag
* @param packetid integer - the MQTT packet identifier
* @param count - number of members in the topicFilters array
* @param topicFilters - array of topic filter names
* @return the length of the serialized data. <= 0 indicates error
*/
int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
int count, MQTTString topicFilters[]){
unsigned char *ptr = buf;
MQTTHeader header = {0};
int rem_len = 0;
int rc = -1;
int i = 0;
FUNC_ENTRY;
if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen)
{
rc = MQTTPACKET_BUFFER_TOO_SHORT;
goto exit;
}
header.byte = 0;
header.bits.type = UNSUBSCRIBE;
header.bits.dup = dup;
header.bits.qos = 1;
writeChar(&ptr, header.byte); /* write header */
ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
writeInt(&ptr, packetid);
for (i = 0; i < count; ++i)
writeMQTTString(&ptr, topicFilters[i]);
rc = ptr - buf;
exit:
FUNC_EXIT_RC(rc);
return rc;
}
/**
* Deserializes the supplied (wire) buffer into unsuback data
* @param packetid returned integer - the MQTT packet identifier
* @param buf the raw buffer data, of the correct length determined by the remaining length field
* @param buflen the length in bytes of the data in the supplied buffer
* @return error code. 1 is success, 0 is failure
*/
int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen){
unsigned char type = 0;
unsigned char dup = 0;
int rc = 0;
FUNC_ENTRY;
rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen);
if (type == UNSUBACK)
rc = 1;
FUNC_EXIT_RC(rc);
return rc;
}

View File

@@ -0,0 +1,83 @@
#include "MQTTPacket.h"
#include "StackTrace.h"
#include <string.h>
/**
* Deserializes the supplied (wire) buffer into unsubscribe data
* @param dup integer returned - the MQTT dup flag
* @param packetid integer returned - the MQTT packet identifier
* @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays
* @param count - number of members in the topicFilters and requestedQoSs arrays
* @param topicFilters - array of topic filter names
* @param buf the raw buffer data, of the correct length determined by the remaining length field
* @param buflen the length in bytes of the data in the supplied buffer
* @return the length of the serialized data. <= 0 indicates error
*/
int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[],
unsigned char* buf, int len){
MQTTHeader header = {0};
unsigned char* curdata = buf;
unsigned char* enddata = NULL;
int rc = 0;
int mylen = 0;
FUNC_ENTRY;
header.byte = readChar(&curdata);
if (header.bits.type != UNSUBSCRIBE)
goto exit;
*dup = header.bits.dup;
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
enddata = curdata + mylen;
*packetid = readInt(&curdata);
*count = 0;
while (curdata < enddata)
{
if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
goto exit;
(*count)++;
}
rc = 1;
exit:
FUNC_EXIT_RC(rc);
return rc;
}
/**
* Serializes the supplied unsuback data into the supplied buffer, ready for sending
* @param buf the buffer into which the packet will be serialized
* @param buflen the length in bytes of the supplied buffer
* @param packetid integer - the MQTT packet identifier
* @return the length of the serialized data. <= 0 indicates error
*/
int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid){
MQTTHeader header = {0};
int rc = 0;
unsigned char *ptr = buf;
FUNC_ENTRY;
if (buflen < 2)
{
rc = MQTTPACKET_BUFFER_TOO_SHORT;
goto exit;
}
header.byte = 0;
header.bits.type = UNSUBACK;
writeChar(&ptr, header.byte); /* write header */
ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
writeInt(&ptr, packetid);
rc = ptr - buf;
exit:
FUNC_EXIT_RC(rc);
return rc;
}

View File

@@ -0,0 +1,61 @@
#ifndef STACKTRACE_H_
#define STACKTRACE_H_
#include <stdio.h>
#define NOSTACKTRACE 1
#if defined(NOSTACKTRACE)
#define FUNC_ENTRY
#define FUNC_ENTRY_NOLOG
#define FUNC_ENTRY_MED
#define FUNC_ENTRY_MAX
#define FUNC_EXIT
#define FUNC_EXIT_NOLOG
#define FUNC_EXIT_MED
#define FUNC_EXIT_MAX
#define FUNC_EXIT_RC(x)
#define FUNC_EXIT_MED_RC(x)
#define FUNC_EXIT_MAX_RC(x)
#else
#if defined(WIN32)
#define inline __inline
#define FUNC_ENTRY StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MINIMUM)
#define FUNC_ENTRY_NOLOG StackTrace_entry(__FUNCTION__, __LINE__, -1)
#define FUNC_ENTRY_MED StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MEDIUM)
#define FUNC_ENTRY_MAX StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MAXIMUM)
#define FUNC_EXIT StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MINIMUM)
#define FUNC_EXIT_NOLOG StackTrace_exit(__FUNCTION__, __LINE__, -1)
#define FUNC_EXIT_MED StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MEDIUM)
#define FUNC_EXIT_MAX StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MAXIMUM)
#define FUNC_EXIT_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MINIMUM)
#define FUNC_EXIT_MED_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MEDIUM)
#define FUNC_EXIT_MAX_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MAXIMUM)
#else
#define FUNC_ENTRY StackTrace_entry(__func__, __LINE__, TRACE_MINIMUM)
#define FUNC_ENTRY_NOLOG StackTrace_entry(__func__, __LINE__, -1)
#define FUNC_ENTRY_MED StackTrace_entry(__func__, __LINE__, TRACE_MEDIUM)
#define FUNC_ENTRY_MAX StackTrace_entry(__func__, __LINE__, TRACE_MAXIMUM)
#define FUNC_EXIT StackTrace_exit(__func__, __LINE__, NULL, TRACE_MINIMUM)
#define FUNC_EXIT_NOLOG StackTrace_exit(__func__, __LINE__, NULL, -1)
#define FUNC_EXIT_MED StackTrace_exit(__func__, __LINE__, NULL, TRACE_MEDIUM)
#define FUNC_EXIT_MAX StackTrace_exit(__func__, __LINE__, NULL, TRACE_MAXIMUM)
#define FUNC_EXIT_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MINIMUM)
#define FUNC_EXIT_MED_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MEDIUM)
#define FUNC_EXIT_MAX_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MAXIMUM)
void StackTrace_entry(const char* name, int line, int trace);
void StackTrace_exit(const char* name, int line, void* return_value, int trace);
void StackTrace_printStack(FILE* dest);
char* StackTrace_get(unsigned long);
#endif
#endif
#endif /* STACKTRACE_H_ */

View File

@@ -0,0 +1,220 @@
#include "battery.h"
/*电池,获取电池信息*/
void Bat_GetWorkInfor(void)
{
/***读取正在充电指示***/
if(Read_BatCharging()==0)
DevParam.BatCharging = 1;
else
DevParam.BatCharging = 0;
if(DevParam.BatInforGetTime >= 300)
{
if(DevParam.BatCheck < 100)
DevParam.BatCheck++;
/***获取电池电压***/
DevParam.BatVoltage = ADC_GetSimpleData();
if(DevParam.BatVoltage >= 3.58)
{
/***转换电池容量***/
if(DevParam.BatVoltage>=4.16)
DevParam.ShortBatSOC = 100;
else if(DevParam.BatVoltage>=4.08)
DevParam.ShortBatSOC = 90;
else if(DevParam.BatVoltage>=3.97)
DevParam.ShortBatSOC = 80;
else if(DevParam.BatVoltage>=3.90)
DevParam.ShortBatSOC = 70;
else if(DevParam.BatVoltage>=3.84)
DevParam.ShortBatSOC = 60;
else if(DevParam.BatVoltage>=3.79)
DevParam.ShortBatSOC = 50;
else if(DevParam.BatVoltage>=3.76)
DevParam.ShortBatSOC = 40;
else if(DevParam.BatVoltage>=3.73)
DevParam.ShortBatSOC = 30;
else if(DevParam.BatVoltage>=3.71)
DevParam.ShortBatSOC = 20;
else if(DevParam.BatVoltage>=3.65)
DevParam.ShortBatSOC = 10;
}
else
{
DevParam.ShortBatSOC = 0;
}
DevParam.BatInforGetTime = 0;
}
}
/*显示部分,电池图标外形刷新*/
void Show_BatIconShapeRefresh(void)
{
#ifdef Device_LCD
// 电池外形
SimLCD_PointColor = SimLCDColor_WHITE;
SimLCD_DrawLine( 200, 9, 222, 9);
SimLCD_DrawLine( 200, 21, 222, 21);
SimLCD_DrawLine( 200, 9, 200, 21);
SimLCD_DrawLine( 222, 9, 222, 21);
SimLCD_DrawLine( 197, 13, 200, 13); // 上横线
SimLCD_DrawLine( 197, 13, 197, 17); // 中横线
SimLCD_DrawLine( 197, 17, 200, 17); // 下横线
#endif
}
/*显示部分,电池图标刷新*/
void Show_BatIconRefresh(void)
{
if((DevParam.BatIconRefreshTime>=500)&&(DevParam.BatCheck>=2))
{
#ifdef Device_LCD
uint8_t i = 0;
uint16_t FontColor = 0;
if( (DevParam.BatCharging==1)&&(DevParam.RunPhase!=0) )
{// 充电中,电池图标变成绿色
FontColor = SimLCDColor_GREEN;
}
else if(DevParam.RunPhase==RunPhase_Runing)
{// 其他,电池图标变成白色
FontColor = SimLCDColor_WHITE;
}
// 显示SOC
if(DevParam.ShortBatSOC!=DevParam.BatSOC)
{
if( DevParam.ShortBatSOC >= 80 ) {
SimLCD_PointColor = FontColor;
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 202, i, 205, i);
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 207, i, 210, i);
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 212, i, 215, i);
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 217, i, 220, i);
}
else if( DevParam.ShortBatSOC >= 60 ) {
SimLCD_PointColor = SimLCDColor_BLACK;
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 202, i, 205, i);
SimLCD_PointColor = FontColor;
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 207, i, 210, i);
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 212, i, 215, i);
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 217, i, 220, i);
}
else if( DevParam.ShortBatSOC >= 40 ) {
SimLCD_PointColor = SimLCDColor_BLACK;
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 202, i, 205, i);
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 207, i, 210, i);
SimLCD_PointColor = FontColor;
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 212, i, 215, i);
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 217, i, 220, i);
}
else if( DevParam.ShortBatSOC >= 20 ) {
SimLCD_PointColor = SimLCDColor_BLACK;
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 202, i, 205, i);
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 207, i, 210, i);
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 212, i, 215, i);
SimLCD_PointColor = FontColor;
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 217, i, 220, i);
}
else if( DevParam.ShortBatSOC >= 1 ) {
SimLCD_PointColor = SimLCDColor_BLACK;
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 202, i, 205, i);
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 207, i, 210, i);
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 212, i, 215, i);
for( i = 12; i < 19; i++)
SimLCD_DrawLine( 217, i, 220, i);
}
Power_LCD(1);
DevParam.BatSOC =DevParam.ShortBatSOC;
}
#endif
DevParam.BatIconRefreshTime = 0;
}
}
/*显示部分,侧边跳动*/
void Show_BroadSideBeat(void)
{
if(DevParam.SideBeatTime>=2000)
{
#ifdef Device_LCD
uint8_t i = 0;
if(DevParam.SideBeat==1) {
SimLCD_PointColor = SimLCDColor_CYAN;
for(i = 0; i < 13; i++)
SimLCD_DrawLine( 0, i, 32, i);
DevParam.SideBeat = 2;
}
else if(DevParam.SideBeat==2) {
SimLCD_PointColor = SimLCDColor_CYAN;
for(i = 15; i < 28; i++)
SimLCD_DrawLine( 0, i, 24, i);
DevParam.SideBeat = 3;
}
else if(DevParam.SideBeat==3) {
SimLCD_PointColor = SimLCDColor_CYAN;
for(i = 30; i < 43; i++)
SimLCD_DrawLine( 0, i, 16, i);
DevParam.SideBeat = 4;
}
else if(DevParam.SideBeat==4) {
SimLCD_PointColor = SimLCDColor_CYAN;
for(i = 45; i < 58; i++)
SimLCD_DrawLine( 0, i, 8, i);
DevParam.SideBeat = 5;
}
else if(DevParam.SideBeat==5) {
SimLCD_PointColor = SimLCDColor_BLACK;
for(i = 0; i < 13; i++)
SimLCD_DrawLine( 0, i, 32, i);
for(i = 15; i < 28; i++)
SimLCD_DrawLine( 0, i, 24, i);
for(i = 30; i < 43; i++)
SimLCD_DrawLine( 0, i, 16, i);
for(i = 45; i < 58; i++)
SimLCD_DrawLine( 0, i, 8, i);
DevParam.SideBeat = 1;
}
#endif
DevParam.SideBeatTime = 0;
}
}

View File

@@ -0,0 +1,31 @@
#ifndef _BATTERY_H_
#define _BATTERY_H_
#include "stm32f10x.h"
#include "bsp_timer3.h"
#include "bsp_port.h"
#include "bsp_adc.h"
#include "simulation_lcd.h"
/*电池,获取电池信息*/
void Bat_GetWorkInfor(void);
/*显示部分,电池图标外形刷新*/
void Show_BatIconShapeRefresh(void);
/*显示部分,电池图标刷新*/
void Show_BatIconRefresh(void);
/*显示部分,侧边跳动*/
void Show_BroadSideBeat(void);
#endif

View File

@@ -0,0 +1,127 @@
#include "dev_work.h"
void Dev_ScanExtIndicate(void)
{
/*开机检测*/
if(DevParam.RunPhase==0)
{
if(Read_BootKey())
{
Delay_ms(200);
if(Read_BootKey())
{
while(Read_BootKey());
DevParam.ShortRunPhase = RunPhase_Runing;
}
}
else
{
DevParam.ShortRunPhase = RunPhase_Standby;
}
}
// 待机模式
if(DevParam.RunPhase==RunPhase_Standby)
{
if(Read_BootKey())
{
Delay_ms(200);
if(Read_BootKey())
{
while(Read_BootKey());
DevParam.ShortRunPhase = RunPhase_Runing;
}
}
}
// 运行模式
if(DevParam.RunPhase==RunPhase_Runing)
{
if(Read_BootKey())
{
Delay_ms(200);
if(Read_BootKey())
{
while(Read_BootKey());
// 关机/进入待机状态
if(DevParam.BatCharging)
DevParam.ShortRunPhase = RunPhase_Standby;
else
DevParam.ShortRunPhase = RunPhase_Close;
}
}
}
}
/*切换工作模式*/
void Dev_SwitchWorkMode(void)
{
if(DevParam.RunPhase!=DevParam.ShortRunPhase)
{
if(DevParam.ShortRunPhase == RunPhase_Runing)
{
// 显示初始化
#ifdef Device_LCD
Power_LCD(0);
/*显示部分,基础外形刷新*/
Show_BasicShapeRefresh();
/*显示部分,电池图标外形刷新*/
if(DevParam.RunPhase == 0)
Show_BatIconShapeRefresh();
else
Power_LCD(1);
#endif
// 侧边跳动初始化
DevParam.SideBeat = 1;
/*空气检测仪初始化*/
PMD4_Init();
DevParam.RunPhase = RunPhase_Runing;
}
if(DevParam.ShortRunPhase == RunPhase_Standby)
{
Power_PMD4(0);
#ifdef Device_LCD
Power_LCD(0);
if(DevParam.RunPhase==0)
{
/*显示部分,电池图标外形刷新*/
Show_BatIconShapeRefresh();
}
else if(DevParam.RunPhase==RunPhase_Runing)
{
// 进行清屏操作
SimLCD_Clear(SimLCDColor_BLACK);
/*显示部分,电池图标外形刷新*/
Show_BatIconShapeRefresh();
// 数据部分清零
DevParam.BatSOC = 0;
Power_LCD(1);
}
#endif
DevParam.RunPhase = RunPhase_Standby;
}
// 关机模式
if(DevParam.ShortRunPhase == RunPhase_Close)
{
Delay_ms(1000);
Power_PMD4(0);
Power_LCD(0);
Power_3V3(0);
DevParam.RunPhase = RunPhase_Close;
while(1);
}
}
}

View File

@@ -0,0 +1,29 @@
#ifndef _DEV_WORK_H_
#define _DEV_WORK_H_
#include "stm32f10x.h"
#include "bsp_timer3.h"
#include "bsp_port.h"
#include "bsp_adc.h"
#include "simulation_lcd.h"
#include "battery.h"
#include "pm_uart3.h"
void Dev_ScanExtIndicate(void);
void Dev_SwitchWorkMode(void);
#endif

View File

@@ -0,0 +1,253 @@
#include "esp8266_uart2.h"
//检测应答命令
static uint8_t* ESP8266_CheckCmd(uint8_t *str)
{
char *strx = 0;
if(UART2ReadFlag&0x8000)
{
UART2ReadBuf[UART2ReadFlag&0x7FFF] = 0;//添加结束符
strx = strstr((const char*)UART2ReadBuf,(const char*)str);
}
return (uint8_t*)strx;
}
//cmd:发送的命令字符串
//ack:期待的应答结果,如果为空,则表示不需要等待应答
//返回值:0,发送成功 1,发送失败
static uint8_t ESP8266_SendCmd(uint8_t *cmd,uint8_t *ack,uint16_t waittime) {
UART2ReadFlag = 0;
memset((void *)UART2ReadBuf, 0, sizeof(UART2ReadBuf));
Dev_UART2SendStr("%s\r\n", cmd);//发送命令
if(ack&&waittime) //需要等待应答
{
while(--waittime) //等待倒计时
{
Delay_ms(10);
if(UART2ReadFlag&0x8000)
{
if(ESP8266_CheckCmd(ack))
{
UART2ReadFlag = 0;
return 0;
}
else
{
UART2ReadFlag = 0;
return 1;
}
}
}
if(waittime==0)
{
return 1;
}
}
return 1;
}
uint8_t ESP8266_SendStr(char* fmt,...)
{
uint8_t tbuf[200] = {0};
uint16_t i = 0,j = 0;
va_list ap;
va_start(ap, fmt);
vsprintf((char*)tbuf, fmt, ap);
va_end(ap);
j = strlen((const char*)tbuf);
for( i = 0; i < j; i++)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET){}
USART_SendData(USART2, tbuf[i]);
}
while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET){}
return 0;
}
uint8_t ESP8266_SendData(uint8_t *tbuf, uint16_t len)
{
uint16_t i = 0;
for( i = 0; i < len; i++)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET){}
USART_SendData(USART2, tbuf[i]);
}
while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET){}
return 0;
}
// STA模式下获取本地IP
void ESP8266_GetLocalIP(uint8_t* ipbuf){
uint8_t *p,*p1;
if(ESP8266_SendCmd( (uint8_t *)"AT+CIFSR", (uint8_t *)"OK", 50))
{
ipbuf[0] = 0;
return;
}
p = ESP8266_CheckCmd((uint8_t *)"\"");
p1 = (uint8_t *)strstr((const char*)(p+1),"\"");
*p1=0;
sprintf((char*)ipbuf,"%s",p+1);
}
//退出透传模式 0,退出成功; 1,退出失败
uint8_t ESP8266_QuitTrans(void){
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET){};
USART_SendData(USART2, '+');
Delay_ms(15);
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET){};
USART_SendData(USART2, '+');
Delay_ms(15);
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET){};
USART_SendData(USART2, '+');
Delay_ms(500); //等待500ms
return ESP8266_SendCmd((uint8_t *)"AT",(uint8_t *)"OK",20);//退出透传判断
}
//获取连接状态 0,未连接;1,连接成功.
uint8_t ESP8266_ConstaCheck(void){
while(ESP8266_SendCmd((uint8_t *)"AT+CIPSTATUS",(uint8_t *)"OK",50));
return 0;
}
uint8_t ESP8266_APInit(char *name, char *password){
uint8_t Sbuf[60] ={0};
// UART2
BSP_UART2Init(115200);
Delay_ms(1000);
Delay_ms(1000);
while(ESP8266_SendCmd((uint8_t *)"AT",(uint8_t *)"OK",200))
{//退出透传
ESP8266_QuitTrans();
//关闭透传模式
ESP8266_SendCmd((uint8_t *)"AT+CIPMODE=0",(uint8_t *)"OK",200);
Delay_ms(1000);
}
// 关闭回显
while(ESP8266_SendCmd((uint8_t *)"ATE0",(uint8_t *)"OK",200));
// 设置波特率
while(ESP8266_SendCmd((uint8_t *)"AT+UART=115200,8,1,0,0",(uint8_t *)"OK",200));
Delay_ms(10);
// 设置WIFI AP模式
while(ESP8266_SendCmd((uint8_t *)"AT+CWMODE=2",(uint8_t *)"OK",200));
while(ESP8266_SendCmd((uint8_t *)"AT+RST",(uint8_t *)"OK",200));
// 延时4秒等待重启成功
Delay_ms(1000);
Delay_ms(1000);
Delay_ms(1000);
Delay_ms(1000);
memset(Sbuf, 0 , sizeof(Sbuf));
sprintf((char*)Sbuf, "AT+CWSAP=\"%s\",\"%s\",1,4", name, password);
while(ESP8266_SendCmd(Sbuf, (uint8_t *)"OK", 1000));
#ifdef TCP_Mode
#endif
#ifdef UDP_Mode
while(ESP8266_SendCmd((uint8_t *)"AT+CIPMUX=0",(uint8_t *)"OK",200));
while(ESP8266_SendCmd((uint8_t *)"AT+CIPSTART=\"UDP\",\"255.255.255.255\",60156,42254,0",(uint8_t *)"OK",500));
while(ESP8266_SendCmd((uint8_t *)"AT+CIPMODE=1",(uint8_t *)"OK",300));
while(ESP8266_SendCmd((uint8_t *)"AT+CIPSEND",(uint8_t *)"OK",200));
#endif
UART2ReadFlag = 0;
memset((void *)UART2ReadBuf, 0, sizeof(UART2ReadBuf));
return 0;
}
uint8_t ESP8266_STAConnect(char *name, char *password){
uint8_t Sbuf[60] ={0};
memset(Sbuf, 0 , sizeof(Sbuf));
sprintf((char*)Sbuf,"AT+CWJAP=\"%s\",\"%s\"", name, password);
if(ESP8266_SendCmd( Sbuf, (uint8_t *)"WIFI GOT IP", 100)){
return 1;
}
else {
#ifdef TCP_Mode
while(ESP8266_SendCmd((uint8_t *)"AT+CIPMUX=0",(uint8_t *)"OK",200));
while(ESP8266_SendCmd((uint8_t *)"AT+CIPSTART=\"TCP\",\"192.168.0.102\",8086",(uint8_t *)"OK",200));
while(ESP8266_SendCmd((uint8_t *)"AT+CIPMODE=1",(uint8_t *)"OK",200));
while(ESP8266_SendCmd((uint8_t *)"AT+CIPSEND",(uint8_t *)"OK",20));
#endif
#ifdef UDP_Mode
while(ESP8266_SendCmd((uint8_t *)"AT+CIPMUX=0",(uint8_t *)"OK",200));
while(ESP8266_SendCmd((uint8_t *)"AT+CIPSTART=\"UDP\",\"192.168.1.6\",8086,8086,0",(uint8_t *)"OK",200));
while(ESP8266_SendCmd((uint8_t *)"AT+CIPMODE=1",(uint8_t *)"OK",200));
while(ESP8266_SendCmd((uint8_t *)"AT+CIPSEND",(uint8_t *)"OK",20));
#endif
UART2ReadFlag = 0;
memset((void *)UART2ReadBuf, 0, sizeof(UART2ReadBuf));
return 0;
}
}
uint8_t ESP8266_STAInit(void){
// 初始化串口
BSP_UART2Init(115200);
// 延时2秒等待串口初始化完成
Delay_ms(1000);
Delay_ms(1000);
while(ESP8266_SendCmd((uint8_t *)"AT",(uint8_t *)"OK",200))
{
//退出透传
ESP8266_QuitTrans();
//关闭透传模式
ESP8266_SendCmd((uint8_t *)"AT+CIPMODE=0",(uint8_t *)"OK",200);
Delay_ms(800);
}
// 关闭回显
while(ESP8266_SendCmd((uint8_t *)"ATE0",(uint8_t *)"OK",200));
// 设置波特率
while(ESP8266_SendCmd((uint8_t *)"AT+UART=115200,8,1,0,0",(uint8_t *)"OK",200));
Delay_ms(10);
// 设置WIFI STA模式
while(ESP8266_SendCmd((uint8_t *)"AT+CWMODE=1",(uint8_t *)"OK",200));
while(ESP8266_SendCmd((uint8_t *)"AT+RST",(uint8_t *)"OK",200));
Delay_ms(1000);
Delay_ms(1000);
Delay_ms(1000);
Delay_ms(1000);
return 1;
}

View File

@@ -0,0 +1,23 @@
#ifndef _ESP8266_UART2_H_
#define _ESP8266_UART2_H_
#include "stm32f10x.h"
#include "bsp_uart2.h"
#include "stdio.h"
#include "string.h"
#include "stdarg.h"
//#define TCP_Mode 1
#define UDP_Mode 1
uint8_t ESP8266_SendStr(char* fmt,...);
uint8_t ESP8266_SendData(uint8_t *tbuf, uint16_t len);
uint8_t ESP8266_APInit(char *name, char *password);
uint8_t ESP8266_STAInit(void);
uint8_t ESP8266_STAConnect(char *name, char *password);
#endif

View File

@@ -0,0 +1,269 @@
#include "esp8266_work.h"
/****************************
配网流程
1、将无线设备初始化为AP模式默认设置为
2、
****************************/
static uint8_t NetWorkFlow = 1;
static uint32_t Nport = 0;
static uint8_t Nssid[20] = {0};
static uint8_t Npassword[20] = {0};
MQTTPacket_connectData MQTT_ConnectData = MQTTPacket_connectData_initializer;
char MQTT_ClientId[150] = {0};
char MQTT_Username[65] = {0};
char MQTT_Password[65] = {0};
volatile uint16_t MQTT_ReadBufLen = 0;
int ESP8266_WaitData(unsigned char* buf, int count)
{
while(1)
{
if(UART2ReadFlag&0x8000)
{
memcpy(buf, (void*)&UART2ReadBuf[MQTT_ReadBufLen], count);
MQTT_ReadBufLen += count;
return count;
}
Delay_ms(10);
}
}
uint8_t MQTT_UserSubscribe(char *pSubTopic) {
uint8_t buf[200] = {0};
uint32_t buflen = sizeof(buf);
int32_t msgid = 1;
int32_t req_qos = 0; // 服务质量
MQTTString topicString = MQTTString_initializer;
topicString.cstring = pSubTopic;
// 订阅格式化
MQTTSerialize_subscribe(buf, buflen, 0, msgid, 1, &topicString, &req_qos);
// 发送订阅
UART2ReadFlag = 0;
ESP8266_SendStr((char *)buf);
if (MQTTPacket_read(buf, buflen, ESP8266_WaitData)==SUBACK) /* wait for suback */
{
unsigned short submsgid;
int subcount;
int granted_qos;
MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen);
if (granted_qos != 0)
{
//Sys_SendLog("granted qos != 0, %d\n", granted_qos);
return 0;
}
else
{
//Sys_SendLog("granted qos = 0\n");
return 1;
}
}
else
{
//Sys_SendLog("no suback received!\r\n");
return 0;
}
}
// MQTT_Publish("/mqtt/topic/0", "hello0");
void MQTT_UserPublish(char *pPubTopic, char *payload, int payloadlen) {
uint8_t buf[400] = {0};
int buflen = sizeof(buf);
MQTTString topicString = MQTTString_initializer;
int len = 0;
topicString.cstring = pPubTopic;
len += MQTTSerialize_publish((unsigned char *)(buf + len), buflen - len, 0, 0, 0, 0, topicString, (unsigned char *)payload, payloadlen);
ESP8266_SendStr((char *)buf);
}
uint8_t buf[400] = {0};
int buflen = 0;
void ESP8266_NetWorkFlow(void)
{
// 第一步初始化为AP
if(NetWorkFlow == 1) {
ESP8266_APInit("XiaoYi_IOT_AirBox", "asdqwe9867");
NetWorkFlow = 2;
}
// 第二步持续发送UDP广播
else if(NetWorkFlow == 2) {
if(DevParam.ESP8266SendTime >= 2000)
{
ESP8266_SendStr("{\"code\":\"0\",\"msg\":\"start connect\"}");
DevParam.ESP8266SendTime = 0;
}
}
// 第三步,返回接受成功信息
else if(NetWorkFlow == 3) {
ESP8266_SendStr("{\"code\":\"1\",\"msg\":\"getWifiInfo:success\"}");
NetWorkFlow = 4;
}
// 第四步,连接家庭无线
else if(NetWorkFlow == 4) {
ESP8266_STAInit();
while(ESP8266_STAConnect((char *)Nssid, (char *)Npassword));
NetWorkFlow = 5;
}
else if(NetWorkFlow == 5) {
MQTT_ConnectData.MQTTVersion = 4; // 3.1.1
MQTT_ConnectData.keepAliveInterval = 60; // 设置心跳包间隔时间
MQTT_ConnectData.clientID.cstring = (char *)MQTT_ClientId;// 客户端ID
MQTT_ConnectData.username.cstring = (char *)MQTT_Username;// 用户名
MQTT_ConnectData.password.cstring = (char *)MQTT_Password;// 密码
MQTT_ConnectData.cleansession = 0; // 清除会话
//rc = aiotMqttSign(product_key, device_name, device_secret, \
//MQTT_ConnectData.clientID.cstring, MQTT_ConnectData.username.cstring, MQTT_ConnectData.password.cstring);
//if (rc < 0) {
//Sys_SendLog("aiotMqttSign -%0x4x\n", -rc);
//return -1;
//}
// 网络连接
//char *host = "a1ykSq0uPgd.iot-as-mqtt.cn-shanghai.aliyuncs.com";
//int port = 1883;
//transport_open( host, port);
// 发送 登录数据
// 将连接字符串格式化一下,现在还没有发送
memset(buf, 0 ,sizeof(buf));
buflen = sizeof(buf);
MQTTSerialize_connect( buf, buflen, &MQTT_ConnectData);
UART2ReadFlag = 0;
ESP8266_SendStr((char *)buf);
// 等待connack
if (MQTTPacket_read(buf, buflen, ESP8266_WaitData) == CONNACK){
unsigned char sessionPresent, connack_rc;
if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) != 1 || connack_rc != 0)
{
//Sys_SendLog("Unable to connect,return code %d\r\n", connack_rc);
//return -1;
}
}
/*订阅*/
MQTT_UserSubscribe("status/set/7CDFA1049ADA"); //更新设备状态
MQTT_UserSubscribe("status/get/7CDFA1049ADA"); //获取设备状态
MQTT_UserSubscribe("setting/set/7CDFA1049ADA");//更新设备配置
MQTT_UserSubscribe("setting/get/7CDFA1049ADA");//获取设备配置
}
else if(NetWorkFlow == 10)
{
if(DevParam.ESP8266SendTime >= 2000)
{
MQTTSerialize_pingreq(buf, buflen);// 发送心跳
ESP8266_SendStr((char *)buf);
DevParam.ESP8266SendTime = 0;
}
// 发布设备状态
else if(DevParam.ESP8266SendTime>=1500){
// /a1ykSq0uPgd/qmvH76OCy2FeGp9DDMPx/user/update
// 0x00 00000001 0032 0100 001223
char txBuf[30] = {0};
char txLen = 0;
//txBuf[txLen++] = COMMAND_REPORT;// 属性上报
txBuf[txLen++] = 0x00;txBuf[txLen++] = 0x00;txBuf[txLen++] = 0x00;txBuf[txLen++] = 0x01;// ID
//txBuf[txLen++] = (uint8_t)(DeviceData.prop_int16 >> 8);txBuf[txLen++] = (uint8_t)(DeviceData.prop_int16 >> 0); // INT16
//txBuf[txLen++] = DeviceData.prop_bool; // BOOL
//txBuf[txLen++] = (uint8_t)(DeviceData.prop_int16 >> 8);txBuf[txLen++] = (uint8_t)(DeviceData.prop_int16 >> 0);// FLOAT
MQTT_UserPublish("device_info", txBuf, txLen);
DevParam.ESP8266SendTime = 0;
}
}
}
cJSON *root = NULL;
char JsonString[100] = {0};
uint16_t init = 0;
uint16_t i = 0;
uint8_t rbuf[256] = {0};
uint8_t rlen = 0;
void ESP8266_NetReceiveInfor(void)
{
if(UART2ReadFlag&0x8000)
{
if(NetWorkFlow == 2) {
// {"port":"60156","ssid":"xxx","password":"xxxx"}
if(UART2ReadBuf[0] == '{')
{
for(i = 0;;i++)
{
JsonString[i] = UART2ReadBuf[i];
if(UART2ReadBuf[i] == '}')
{
// 解析数据
root = cJSON_Parse((char *)JsonString);
if(root == NULL)
{
memset((void *)UART2ReadBuf, 0, sizeof(UART2ReadBuf));
UART3ReadFlag = 0;
return;
}
Nport = cJSON_GetObjectItem(root, "port")->valuedouble;
if(Nport==60156)
{
sprintf((char *)Nssid, "%s",cJSON_GetObjectItem(root, "ssid")->valuestring);
sprintf((char *)Npassword,"%s",cJSON_GetObjectItem(root, "password")->valuestring);
// 进入无线第三步
NetWorkFlow = 3;
}
cJSON_Delete(root);
memset(JsonString, 0, sizeof(JsonString));
break;
}
}
}
}
if(NetWorkFlow == 10)
{
rlen = UART2ReadFlag&(~(1<<15));
memcpy(rbuf, (void*)&UART2ReadBuf, rlen);
memset(UART2ReadBuf, 0, sizeof(UART2ReadBuf));
UART2ReadFlag = 0;
if (strstr((char *)rbuf, "CLOSED"))
{
}
else
{
if (0xD0 == *rbuf)
{
//Sys_SendLog("MQTT Heart Beat \r\n");
}
else
{
// strncpy(Topic, (void*)(rbuf+4), rbuf[3]);
// printf("Topic: %s\r\n", Topic);
// strncpy(message,(void*)(rbuf+4+rbuf[3]),rbuf[1]-rbuf[3]-2);
// printf("Message: %s\r\n", message);
// memset(rbuf, 0, sizeof(rbuf));
}
}
}
}
}

View File

@@ -0,0 +1,18 @@
#ifndef _ESP8266_WORK_H_
#define _ESP8266_WORK_H_
#include "stm32f10x.h"
#include "esp8266_uart2.h"
#include "cJSON.h"
#include "MQTTPacket.h"
#include "MQTTConnect.h"
void ESP8266_NetWorkFlow(void);
void ESP8266_NetReceiveInfor(void);
#endif

View File

@@ -0,0 +1,196 @@
#include "pm_uart3.h"
// 校验
// 校验正确--0xff 校验错误--0x00
uint8_t PMD4_CheckSum(uint8_t *data) {
uint8_t i = 0, result = 0;
uint16_t sum = 0;
if( (data[0] == 0x42) && (data[1] == 0x4d) )
{
for(i = 0; i < 30; i++)
{
sum += data[i];
}
if( sum == ((data[30]<<8) + data[31]) )
{
result = 0xff;
}
}
return result;
}
// PM 初始化
void PMD4_Init(void) {
BSP_UART3Init(9600);
Power_PMD4(1);
Delay_ms(20);
}
// 请求获取空气信息
void PMD4_ReqGetAirInfor(uint8_t Code) {
uint16_t sum = 0;
uint8_t data[20] = {0};
uint8_t i = 0;
data[0] = 0x42;
data[1] = 0x4d;
data[2] = Code;
data[3] = 0x00;
data[4] = 0x00;
for(i = 0; i < 5; i++)
{
sum += data[i];
}
data[5] = (uint8_t)(sum>>8);
data[6] = (uint8_t)(sum>>0);
BSP_UART3SendData(data, 7);
}
/*PMD4获取空气信息*/
void PMD4_GetAirInfor(void)
{
#ifdef Device_LCD
uint16_t x = 0, y = 0;
uint8_t i = 0;
#endif
if(UART3ReadFlag&0x8000)
{
if( PMD4_CheckSum(UART3ReadBuf)==0xff)
{
DevParam.PM1_0 = (uint16_t)(UART3ReadBuf[4]<<8) + (uint16_t)(UART3ReadBuf[5]<<0);
DevParam.PM2_5 = (uint16_t)(UART3ReadBuf[6]<<8) + (uint16_t)(UART3ReadBuf[7]<<0);
DevParam.PM10 = (uint16_t)(UART3ReadBuf[8]<<8) + (uint16_t)(UART3ReadBuf[9]<<0);
}
UART3ReadFlag = 0;
memset((void *)UART3ReadBuf, 0, sizeof(UART3ReadBuf));
}
if(DevParam.AirInforGetTime >= 2000)
{
PMD4_ReqGetAirInfor(0xE2);
DevParam.AirInforGetTime = 0;
}
/**********显示实时空气信息*****************/
#ifdef Device_LCD
// PM1.0
if(DevParam.ShowPM1_0 != DevParam.PM1_0)
{
x = (uint16_t)(SimLCD.width/2/2-20);
y = 260;
// 1
i = (uint8_t)(DevParam.PM1_0/100);
SimLCD_NewShowChar( (x+8), y, (i + 16));
// 1
i = (uint8_t)(DevParam.PM1_0/10%10);
SimLCD_NewShowChar( (x+16), y, (i + 16));
// 0
i = (uint8_t)(DevParam.PM1_0%10);
SimLCD_NewShowChar( (x+24), y, (i + 16));
DevParam.ShowPM1_0 = DevParam.PM1_0;
}
// PM2.5
if(DevParam.ShowPM2_5 != DevParam.PM2_5)
{
x = (uint16_t)(SimLCD.width/2-20);
y = 80;
// 1
i = (uint8_t)(DevParam.PM2_5/100);
SimLCD_NewShowChar( (x+8), y, (i + 16));
// 1
i = (uint8_t)(DevParam.PM2_5/10%10);
SimLCD_NewShowChar( (x+16), y, (i + 16));
// 0
i = (uint8_t)(DevParam.PM2_5%10);
SimLCD_NewShowChar( (x+24), y, (i + 16));
DevParam.ShowPM2_5 = DevParam.PM2_5;
}
// PM10
if(DevParam.PM10 != DevParam.ShowPM10)
{
x = (uint16_t)(SimLCD.width/2/2*3-16);
y = 260;
// 1
i = (uint8_t)(DevParam.PM10/100);
SimLCD_NewShowChar( (x+8), y, (i + 16));
// 1
i = (uint8_t)(DevParam.PM10/10%10);
SimLCD_NewShowChar( (x+16), y, (i + 16));
// 0
i = (uint8_t)(DevParam.PM10%10);
SimLCD_NewShowChar( (x+24), y, (i + 16));
DevParam.ShowPM10 = DevParam.PM10;
}
#endif
}
/*显示部分,基础外形刷新*/
void Show_BasicShapeRefresh(void)
{
#ifdef Device_LCD
uint16_t x = 0, y = 0;
// 画上边内圆
x = (uint16_t)(SimLCD.width/2);
y = (uint16_t)(SimLCD.height/3) + 10;
// 填充圆环 r0外圆 r1内圆
SimLCD_PointColor = SimLCDColor_CYAN;
SimLCD_FillTorus( x, y, 100, 90);
// 画下边两个圆
x = (uint16_t)(SimLCD.width/2/2*3);
y = (uint16_t)(SimLCD.height/3*2) + (uint16_t)(SimLCD.height/3/2);
// 填充圆环 r0外圆 r1内圆
SimLCD_PointColor = SimLCDColor_YELLOW;
SimLCD_FillTorus( x, y, 50, 45);
x = (uint16_t)(SimLCD.width/2/2);
y = (uint16_t)(SimLCD.height/3*2) + (uint16_t)(SimLCD.height/3/2);
SimLCD_PointColor = SimLCDColor_LIGHTGREEN;
SimLCD_FillTorus( x, y, 50, 45);
// 显示字符串 PM2.5 ug/m3 PM1.0 PM10
x = (uint16_t)(SimLCD.width/2-24);
y = 40;
SimLCD_Show_PM2_5( x, y);
x = (uint16_t)(SimLCD.width/2-20);
y = 180;
SimLCD_Show_ug_m3( x, y);
x = (uint16_t)(SimLCD.width/2/2-20);
y = 230;
SimLCD_Show_PM1_0( x, y);
x = (uint16_t)(SimLCD.width/2/2*3-16);
y = 230;
SimLCD_Show_PM10( x, y);
#endif
}

View File

@@ -0,0 +1,28 @@
#ifndef _PM_UART3_H_
#define _PM_UART3_H_
#include "stm32f10x.h"
#include "bsp_uart3.h"
#include "bsp_timer3.h"
#include "bsp_port.h"
#include "simulation_lcd.h"
void PMD4_Init(void);
/*PMD4获取空气信息*/
void PMD4_GetAirInfor(void);
/*显示部分,基础外形刷新*/
void Show_BasicShapeRefresh(void);
#endif

View File

@@ -0,0 +1,270 @@
#ifndef _SIMULATION_FONT_H_
#define _SIMULATION_FONT_H_
//常用ASCII表
//偏移量32
//ASCII字符集: !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
//PC2LCD2002取模方式设置阴码+逐列式+顺向+C51格式
//总共3个字符集12*12、16*16和24*24用户可以自行新增其他分辨率的字符集。
//每个字符所占用的字节数为:(size/8+((size%8)?1:0))*(size/2),其中size:是字库生成时的点阵大小(12/16/24...)
const unsigned char Simulation_Asc1206[95][12]={
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*" ",0*/
{0x00,0x00,0x00,0x00,0x3F,0x40,0x00,0x00,0x00,0x00,0x00,0x00},/*"!",1*/
{0x00,0x00,0x30,0x00,0x40,0x00,0x30,0x00,0x40,0x00,0x00,0x00},/*""",2*/
{0x09,0x00,0x0B,0xC0,0x3D,0x00,0x0B,0xC0,0x3D,0x00,0x09,0x00},/*"#",3*/
{0x18,0xC0,0x24,0x40,0x7F,0xE0,0x22,0x40,0x31,0x80,0x00,0x00},/*"$",4*/
{0x18,0x00,0x24,0xC0,0x1B,0x00,0x0D,0x80,0x32,0x40,0x01,0x80},/*"%",5*/
{0x03,0x80,0x1C,0x40,0x27,0x40,0x1C,0x80,0x07,0x40,0x00,0x40},/*"&",6*/
{0x10,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"'",7*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x80,0x20,0x40,0x40,0x20},/*"(",8*/
{0x00,0x00,0x40,0x20,0x20,0x40,0x1F,0x80,0x00,0x00,0x00,0x00},/*")",9*/
{0x09,0x00,0x06,0x00,0x1F,0x80,0x06,0x00,0x09,0x00,0x00,0x00},/*"*",10*/
{0x04,0x00,0x04,0x00,0x3F,0x80,0x04,0x00,0x04,0x00,0x00,0x00},/*"+",11*/
{0x00,0x10,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*",",12*/
{0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x00,0x00},/*"-",13*/
{0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*".",14*/
{0x00,0x20,0x01,0xC0,0x06,0x00,0x38,0x00,0x40,0x00,0x00,0x00},/*"/",15*/
{0x1F,0x80,0x20,0x40,0x20,0x40,0x20,0x40,0x1F,0x80,0x00,0x00},/*"0",16*/
{0x00,0x00,0x10,0x40,0x3F,0xC0,0x00,0x40,0x00,0x00,0x00,0x00},/*"1",17*/
{0x18,0xC0,0x21,0x40,0x22,0x40,0x24,0x40,0x18,0x40,0x00,0x00},/*"2",18*/
{0x10,0x80,0x20,0x40,0x24,0x40,0x24,0x40,0x1B,0x80,0x00,0x00},/*"3",19*/
{0x02,0x00,0x0D,0x00,0x11,0x00,0x3F,0xC0,0x01,0x40,0x00,0x00},/*"4",20*/
{0x3C,0x80,0x24,0x40,0x24,0x40,0x24,0x40,0x23,0x80,0x00,0x00},/*"5",21*/
{0x1F,0x80,0x24,0x40,0x24,0x40,0x34,0x40,0x03,0x80,0x00,0x00},/*"6",22*/
{0x30,0x00,0x20,0x00,0x27,0xC0,0x38,0x00,0x20,0x00,0x00,0x00},/*"7",23*/
{0x1B,0x80,0x24,0x40,0x24,0x40,0x24,0x40,0x1B,0x80,0x00,0x00},/*"8",24*/
{0x1C,0x00,0x22,0xC0,0x22,0x40,0x22,0x40,0x1F,0x80,0x00,0x00},/*"9",25*/
{0x00,0x00,0x00,0x00,0x08,0x40,0x00,0x00,0x00,0x00,0x00,0x00},/*":",26*/
{0x00,0x00,0x00,0x00,0x04,0x60,0x00,0x00,0x00,0x00,0x00,0x00},/*";",27*/
{0x00,0x00,0x04,0x00,0x0A,0x00,0x11,0x00,0x20,0x80,0x40,0x40},/*"<",28*/
{0x09,0x00,0x09,0x00,0x09,0x00,0x09,0x00,0x09,0x00,0x00,0x00},/*"=",29*/
{0x00,0x00,0x40,0x40,0x20,0x80,0x11,0x00,0x0A,0x00,0x04,0x00},/*">",30*/
{0x18,0x00,0x20,0x00,0x23,0x40,0x24,0x00,0x18,0x00,0x00,0x00},/*"?",31*/
{0x1F,0x80,0x20,0x40,0x27,0x40,0x29,0x40,0x1F,0x40,0x00,0x00},/*"@",32*/
{0x00,0x40,0x07,0xC0,0x39,0x00,0x0F,0x00,0x01,0xC0,0x00,0x40},/*"A",33*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x24,0x40,0x1B,0x80,0x00,0x00},/*"B",34*/
{0x1F,0x80,0x20,0x40,0x20,0x40,0x20,0x40,0x30,0x80,0x00,0x00},/*"C",35*/
{0x20,0x40,0x3F,0xC0,0x20,0x40,0x20,0x40,0x1F,0x80,0x00,0x00},/*"D",36*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x2E,0x40,0x30,0xC0,0x00,0x00},/*"E",37*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x2E,0x00,0x30,0x00,0x00,0x00},/*"F",38*/
{0x0F,0x00,0x10,0x80,0x20,0x40,0x22,0x40,0x33,0x80,0x02,0x00},/*"G",39*/
{0x20,0x40,0x3F,0xC0,0x04,0x00,0x04,0x00,0x3F,0xC0,0x20,0x40},/*"H",40*/
{0x20,0x40,0x20,0x40,0x3F,0xC0,0x20,0x40,0x20,0x40,0x00,0x00},/*"I",41*/
{0x00,0x60,0x20,0x20,0x20,0x20,0x3F,0xC0,0x20,0x00,0x20,0x00},/*"J",42*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x0B,0x00,0x30,0xC0,0x20,0x40},/*"K",43*/
{0x20,0x40,0x3F,0xC0,0x20,0x40,0x00,0x40,0x00,0x40,0x00,0xC0},/*"L",44*/
{0x3F,0xC0,0x3C,0x00,0x03,0xC0,0x3C,0x00,0x3F,0xC0,0x00,0x00},/*"M",45*/
{0x20,0x40,0x3F,0xC0,0x0C,0x40,0x23,0x00,0x3F,0xC0,0x20,0x00},/*"N",46*/
{0x1F,0x80,0x20,0x40,0x20,0x40,0x20,0x40,0x1F,0x80,0x00,0x00},/*"O",47*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x24,0x00,0x18,0x00,0x00,0x00},/*"P",48*/
{0x1F,0x80,0x21,0x40,0x21,0x40,0x20,0xE0,0x1F,0xA0,0x00,0x00},/*"Q",49*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x26,0x00,0x19,0xC0,0x00,0x40},/*"R",50*/
{0x18,0xC0,0x24,0x40,0x24,0x40,0x22,0x40,0x31,0x80,0x00,0x00},/*"S",51*/
{0x30,0x00,0x20,0x40,0x3F,0xC0,0x20,0x40,0x30,0x00,0x00,0x00},/*"T",52*/
{0x20,0x00,0x3F,0x80,0x00,0x40,0x00,0x40,0x3F,0x80,0x20,0x00},/*"U",53*/
{0x20,0x00,0x3E,0x00,0x01,0xC0,0x07,0x00,0x38,0x00,0x20,0x00},/*"V",54*/
{0x38,0x00,0x07,0xC0,0x3C,0x00,0x07,0xC0,0x38,0x00,0x00,0x00},/*"W",55*/
{0x20,0x40,0x39,0xC0,0x06,0x00,0x39,0xC0,0x20,0x40,0x00,0x00},/*"X",56*/
{0x20,0x00,0x38,0x40,0x07,0xC0,0x38,0x40,0x20,0x00,0x00,0x00},/*"Y",57*/
{0x30,0x40,0x21,0xC0,0x26,0x40,0x38,0x40,0x20,0xC0,0x00,0x00},/*"Z",58*/
{0x00,0x00,0x00,0x00,0x7F,0xE0,0x40,0x20,0x40,0x20,0x00,0x00},/*"[",59*/
{0x00,0x00,0x70,0x00,0x0C,0x00,0x03,0x80,0x00,0x40,0x00,0x00},/*"\",60*/
{0x00,0x00,0x40,0x20,0x40,0x20,0x7F,0xE0,0x00,0x00,0x00,0x00},/*"]",61*/
{0x00,0x00,0x20,0x00,0x40,0x00,0x20,0x00,0x00,0x00,0x00,0x00},/*"^",62*/
{0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10},/*"_",63*/
{0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"`",64*/
{0x00,0x00,0x02,0x80,0x05,0x40,0x05,0x40,0x03,0xC0,0x00,0x40},/*"a",65*/
{0x20,0x00,0x3F,0xC0,0x04,0x40,0x04,0x40,0x03,0x80,0x00,0x00},/*"b",66*/
{0x00,0x00,0x03,0x80,0x04,0x40,0x04,0x40,0x06,0x40,0x00,0x00},/*"c",67*/
{0x00,0x00,0x03,0x80,0x04,0x40,0x24,0x40,0x3F,0xC0,0x00,0x40},/*"d",68*/
{0x00,0x00,0x03,0x80,0x05,0x40,0x05,0x40,0x03,0x40,0x00,0x00},/*"e",69*/
{0x00,0x00,0x04,0x40,0x1F,0xC0,0x24,0x40,0x24,0x40,0x20,0x00},/*"f",70*/
{0x00,0x00,0x02,0xE0,0x05,0x50,0x05,0x50,0x06,0x50,0x04,0x20},/*"g",71*/
{0x20,0x40,0x3F,0xC0,0x04,0x40,0x04,0x00,0x03,0xC0,0x00,0x40},/*"h",72*/
{0x00,0x00,0x04,0x40,0x27,0xC0,0x00,0x40,0x00,0x00,0x00,0x00},/*"i",73*/
{0x00,0x10,0x00,0x10,0x04,0x10,0x27,0xE0,0x00,0x00,0x00,0x00},/*"j",74*/
{0x20,0x40,0x3F,0xC0,0x01,0x40,0x07,0x00,0x04,0xC0,0x04,0x40},/*"k",75*/
{0x20,0x40,0x20,0x40,0x3F,0xC0,0x00,0x40,0x00,0x40,0x00,0x00},/*"l",76*/
{0x07,0xC0,0x04,0x00,0x07,0xC0,0x04,0x00,0x03,0xC0,0x00,0x00},/*"m",77*/
{0x04,0x40,0x07,0xC0,0x04,0x40,0x04,0x00,0x03,0xC0,0x00,0x40},/*"n",78*/
{0x00,0x00,0x03,0x80,0x04,0x40,0x04,0x40,0x03,0x80,0x00,0x00},/*"o",79*/
{0x04,0x10,0x07,0xF0,0x04,0x50,0x04,0x40,0x03,0x80,0x00,0x00},/*"p",80*/
{0x00,0x00,0x03,0x80,0x04,0x40,0x04,0x50,0x07,0xF0,0x00,0x10},/*"q",81*/
{0x04,0x40,0x07,0xC0,0x02,0x40,0x04,0x00,0x04,0x00,0x00,0x00},/*"r",82*/
{0x00,0x00,0x06,0x40,0x05,0x40,0x05,0x40,0x04,0xC0,0x00,0x00},/*"s",83*/
{0x00,0x00,0x04,0x00,0x1F,0x80,0x04,0x40,0x00,0x40,0x00,0x00},/*"t",84*/
{0x04,0x00,0x07,0x80,0x00,0x40,0x04,0x40,0x07,0xC0,0x00,0x40},/*"u",85*/
{0x04,0x00,0x07,0x00,0x04,0xC0,0x01,0x80,0x06,0x00,0x04,0x00},/*"v",86*/
{0x06,0x00,0x01,0xC0,0x07,0x00,0x01,0xC0,0x06,0x00,0x00,0x00},/*"w",87*/
{0x04,0x40,0x06,0xC0,0x01,0x00,0x06,0xC0,0x04,0x40,0x00,0x00},/*"x",88*/
{0x04,0x10,0x07,0x10,0x04,0xE0,0x01,0x80,0x06,0x00,0x04,0x00},/*"y",89*/
{0x00,0x00,0x04,0x40,0x05,0xC0,0x06,0x40,0x04,0x40,0x00,0x00},/*"z",90*/
{0x00,0x00,0x00,0x00,0x04,0x00,0x7B,0xE0,0x40,0x20,0x00,0x00},/*"{",91*/
{0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xF0,0x00,0x00,0x00,0x00},/*"|",92*/
{0x00,0x00,0x40,0x20,0x7B,0xE0,0x04,0x00,0x00,0x00,0x00,0x00},/*"}",93*/
{0x40,0x00,0x80,0x00,0x40,0x00,0x20,0x00,0x20,0x00,0x40,0x00},/*"~",94*/
};
const unsigned char Simulation_Asc1608[95][16]={
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*" ",0*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xCC,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00},/*"!",1*/
{0x00,0x00,0x08,0x00,0x30,0x00,0x60,0x00,0x08,0x00,0x30,0x00,0x60,0x00,0x00,0x00},/*""",2*/
{0x02,0x20,0x03,0xFC,0x1E,0x20,0x02,0x20,0x03,0xFC,0x1E,0x20,0x02,0x20,0x00,0x00},/*"#",3*/
{0x00,0x00,0x0E,0x18,0x11,0x04,0x3F,0xFF,0x10,0x84,0x0C,0x78,0x00,0x00,0x00,0x00},/*"$",4*/
{0x0F,0x00,0x10,0x84,0x0F,0x38,0x00,0xC0,0x07,0x78,0x18,0x84,0x00,0x78,0x00,0x00},/*"%",5*/
{0x00,0x78,0x0F,0x84,0x10,0xC4,0x11,0x24,0x0E,0x98,0x00,0xE4,0x00,0x84,0x00,0x08},/*"&",6*/
{0x08,0x00,0x68,0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"'",7*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xE0,0x18,0x18,0x20,0x04,0x40,0x02,0x00,0x00},/*"(",8*/
{0x00,0x00,0x40,0x02,0x20,0x04,0x18,0x18,0x07,0xE0,0x00,0x00,0x00,0x00,0x00,0x00},/*")",9*/
{0x02,0x40,0x02,0x40,0x01,0x80,0x0F,0xF0,0x01,0x80,0x02,0x40,0x02,0x40,0x00,0x00},/*"*",10*/
{0x00,0x80,0x00,0x80,0x00,0x80,0x0F,0xF8,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x00},/*"+",11*/
{0x00,0x01,0x00,0x0D,0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*",",12*/
{0x00,0x00,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80},/*"-",13*/
{0x00,0x00,0x00,0x0C,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*".",14*/
{0x00,0x00,0x00,0x06,0x00,0x18,0x00,0x60,0x01,0x80,0x06,0x00,0x18,0x00,0x20,0x00},/*"/",15*/
{0x00,0x00,0x07,0xF0,0x08,0x08,0x10,0x04,0x10,0x04,0x08,0x08,0x07,0xF0,0x00,0x00},/*"0",16*/
{0x00,0x00,0x08,0x04,0x08,0x04,0x1F,0xFC,0x00,0x04,0x00,0x04,0x00,0x00,0x00,0x00},/*"1",17*/
{0x00,0x00,0x0E,0x0C,0x10,0x14,0x10,0x24,0x10,0x44,0x11,0x84,0x0E,0x0C,0x00,0x00},/*"2",18*/
{0x00,0x00,0x0C,0x18,0x10,0x04,0x11,0x04,0x11,0x04,0x12,0x88,0x0C,0x70,0x00,0x00},/*"3",19*/
{0x00,0x00,0x00,0xE0,0x03,0x20,0x04,0x24,0x08,0x24,0x1F,0xFC,0x00,0x24,0x00,0x00},/*"4",20*/
{0x00,0x00,0x1F,0x98,0x10,0x84,0x11,0x04,0x11,0x04,0x10,0x88,0x10,0x70,0x00,0x00},/*"5",21*/
{0x00,0x00,0x07,0xF0,0x08,0x88,0x11,0x04,0x11,0x04,0x18,0x88,0x00,0x70,0x00,0x00},/*"6",22*/
{0x00,0x00,0x1C,0x00,0x10,0x00,0x10,0xFC,0x13,0x00,0x1C,0x00,0x10,0x00,0x00,0x00},/*"7",23*/
{0x00,0x00,0x0E,0x38,0x11,0x44,0x10,0x84,0x10,0x84,0x11,0x44,0x0E,0x38,0x00,0x00},/*"8",24*/
{0x00,0x00,0x07,0x00,0x08,0x8C,0x10,0x44,0x10,0x44,0x08,0x88,0x07,0xF0,0x00,0x00},/*"9",25*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x0C,0x03,0x0C,0x00,0x00,0x00,0x00,0x00,0x00},/*":",26*/
{0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*";",27*/
{0x00,0x00,0x00,0x80,0x01,0x40,0x02,0x20,0x04,0x10,0x08,0x08,0x10,0x04,0x00,0x00},/*"<",28*/
{0x02,0x20,0x02,0x20,0x02,0x20,0x02,0x20,0x02,0x20,0x02,0x20,0x02,0x20,0x00,0x00},/*"=",29*/
{0x00,0x00,0x10,0x04,0x08,0x08,0x04,0x10,0x02,0x20,0x01,0x40,0x00,0x80,0x00,0x00},/*">",30*/
{0x00,0x00,0x0E,0x00,0x12,0x00,0x10,0x0C,0x10,0x6C,0x10,0x80,0x0F,0x00,0x00,0x00},/*"?",31*/
{0x03,0xE0,0x0C,0x18,0x13,0xE4,0x14,0x24,0x17,0xC4,0x08,0x28,0x07,0xD0,0x00,0x00},/*"@",32*/
{0x00,0x04,0x00,0x3C,0x03,0xC4,0x1C,0x40,0x07,0x40,0x00,0xE4,0x00,0x1C,0x00,0x04},/*"A",33*/
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x04,0x11,0x04,0x0E,0x88,0x00,0x70,0x00,0x00},/*"B",34*/
{0x03,0xE0,0x0C,0x18,0x10,0x04,0x10,0x04,0x10,0x04,0x10,0x08,0x1C,0x10,0x00,0x00},/*"C",35*/
{0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x04,0x10,0x04,0x08,0x08,0x07,0xF0,0x00,0x00},/*"D",36*/
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x04,0x17,0xC4,0x10,0x04,0x08,0x18,0x00,0x00},/*"E",37*/
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x00,0x17,0xC0,0x10,0x00,0x08,0x00,0x00,0x00},/*"F",38*/
{0x03,0xE0,0x0C,0x18,0x10,0x04,0x10,0x04,0x10,0x44,0x1C,0x78,0x00,0x40,0x00,0x00},/*"G",39*/
{0x10,0x04,0x1F,0xFC,0x10,0x84,0x00,0x80,0x00,0x80,0x10,0x84,0x1F,0xFC,0x10,0x04},/*"H",40*/
{0x00,0x00,0x10,0x04,0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x04,0x00,0x00,0x00,0x00},/*"I",41*/
{0x00,0x03,0x00,0x01,0x10,0x01,0x10,0x01,0x1F,0xFE,0x10,0x00,0x10,0x00,0x00,0x00},/*"J",42*/
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x03,0x80,0x14,0x64,0x18,0x1C,0x10,0x04,0x00,0x00},/*"K",43*/
{0x10,0x04,0x1F,0xFC,0x10,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x0C,0x00,0x00},/*"L",44*/
{0x10,0x04,0x1F,0xFC,0x1F,0x00,0x00,0xFC,0x1F,0x00,0x1F,0xFC,0x10,0x04,0x00,0x00},/*"M",45*/
{0x10,0x04,0x1F,0xFC,0x0C,0x04,0x03,0x00,0x00,0xE0,0x10,0x18,0x1F,0xFC,0x10,0x00},/*"N",46*/
{0x07,0xF0,0x08,0x08,0x10,0x04,0x10,0x04,0x10,0x04,0x08,0x08,0x07,0xF0,0x00,0x00},/*"O",47*/
{0x10,0x04,0x1F,0xFC,0x10,0x84,0x10,0x80,0x10,0x80,0x10,0x80,0x0F,0x00,0x00,0x00},/*"P",48*/
{0x07,0xF0,0x08,0x18,0x10,0x24,0x10,0x24,0x10,0x1C,0x08,0x0A,0x07,0xF2,0x00,0x00},/*"Q",49*/
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x00,0x11,0xC0,0x11,0x30,0x0E,0x0C,0x00,0x04},/*"R",50*/
{0x00,0x00,0x0E,0x1C,0x11,0x04,0x10,0x84,0x10,0x84,0x10,0x44,0x1C,0x38,0x00,0x00},/*"S",51*/
{0x18,0x00,0x10,0x00,0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x00,0x18,0x00,0x00,0x00},/*"T",52*/
{0x10,0x00,0x1F,0xF8,0x10,0x04,0x00,0x04,0x00,0x04,0x10,0x04,0x1F,0xF8,0x10,0x00},/*"U",53*/
{0x10,0x00,0x1E,0x00,0x11,0xE0,0x00,0x1C,0x00,0x70,0x13,0x80,0x1C,0x00,0x10,0x00},/*"V",54*/
{0x1F,0xC0,0x10,0x3C,0x00,0xE0,0x1F,0x00,0x00,0xE0,0x10,0x3C,0x1F,0xC0,0x00,0x00},/*"W",55*/
{0x10,0x04,0x18,0x0C,0x16,0x34,0x01,0xC0,0x01,0xC0,0x16,0x34,0x18,0x0C,0x10,0x04},/*"X",56*/
{0x10,0x00,0x1C,0x00,0x13,0x04,0x00,0xFC,0x13,0x04,0x1C,0x00,0x10,0x00,0x00,0x00},/*"Y",57*/
{0x08,0x04,0x10,0x1C,0x10,0x64,0x10,0x84,0x13,0x04,0x1C,0x04,0x10,0x18,0x00,0x00},/*"Z",58*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xFE,0x40,0x02,0x40,0x02,0x40,0x02,0x00,0x00},/*"[",59*/
{0x00,0x00,0x30,0x00,0x0C,0x00,0x03,0x80,0x00,0x60,0x00,0x1C,0x00,0x03,0x00,0x00},/*"\",60*/
{0x00,0x00,0x40,0x02,0x40,0x02,0x40,0x02,0x7F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00},/*"]",61*/
{0x00,0x00,0x00,0x00,0x20,0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x00,0x00},/*"^",62*/
{0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01},/*"_",63*/
{0x00,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"`",64*/
{0x00,0x00,0x00,0x98,0x01,0x24,0x01,0x44,0x01,0x44,0x01,0x44,0x00,0xFC,0x00,0x04},/*"a",65*/
{0x10,0x00,0x1F,0xFC,0x00,0x88,0x01,0x04,0x01,0x04,0x00,0x88,0x00,0x70,0x00,0x00},/*"b",66*/
{0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x01,0x04,0x00,0x88,0x00,0x00},/*"c",67*/
{0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x11,0x08,0x1F,0xFC,0x00,0x04},/*"d",68*/
{0x00,0x00,0x00,0xF8,0x01,0x44,0x01,0x44,0x01,0x44,0x01,0x44,0x00,0xC8,0x00,0x00},/*"e",69*/
{0x00,0x00,0x01,0x04,0x01,0x04,0x0F,0xFC,0x11,0x04,0x11,0x04,0x11,0x00,0x18,0x00},/*"f",70*/
{0x00,0x00,0x00,0xD6,0x01,0x29,0x01,0x29,0x01,0x29,0x01,0xC9,0x01,0x06,0x00,0x00},/*"g",71*/
{0x10,0x04,0x1F,0xFC,0x00,0x84,0x01,0x00,0x01,0x00,0x01,0x04,0x00,0xFC,0x00,0x04},/*"h",72*/
{0x00,0x00,0x01,0x04,0x19,0x04,0x19,0xFC,0x00,0x04,0x00,0x04,0x00,0x00,0x00,0x00},/*"i",73*/
{0x00,0x00,0x00,0x03,0x00,0x01,0x01,0x01,0x19,0x01,0x19,0xFE,0x00,0x00,0x00,0x00},/*"j",74*/
{0x10,0x04,0x1F,0xFC,0x00,0x24,0x00,0x40,0x01,0xB4,0x01,0x0C,0x01,0x04,0x00,0x00},/*"k",75*/
{0x00,0x00,0x10,0x04,0x10,0x04,0x1F,0xFC,0x00,0x04,0x00,0x04,0x00,0x00,0x00,0x00},/*"l",76*/
{0x01,0x04,0x01,0xFC,0x01,0x04,0x01,0x00,0x01,0xFC,0x01,0x04,0x01,0x00,0x00,0xFC},/*"m",77*/
{0x01,0x04,0x01,0xFC,0x00,0x84,0x01,0x00,0x01,0x00,0x01,0x04,0x00,0xFC,0x00,0x04},/*"n",78*/
{0x00,0x00,0x00,0xF8,0x01,0x04,0x01,0x04,0x01,0x04,0x01,0x04,0x00,0xF8,0x00,0x00},/*"o",79*/
{0x01,0x01,0x01,0xFF,0x00,0x85,0x01,0x04,0x01,0x04,0x00,0x88,0x00,0x70,0x00,0x00},/*"p",80*/
{0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x01,0x05,0x01,0xFF,0x00,0x01},/*"q",81*/
{0x01,0x04,0x01,0x04,0x01,0xFC,0x00,0x84,0x01,0x04,0x01,0x00,0x01,0x80,0x00,0x00},/*"r",82*/
{0x00,0x00,0x00,0xCC,0x01,0x24,0x01,0x24,0x01,0x24,0x01,0x24,0x01,0x98,0x00,0x00},/*"s",83*/
{0x00,0x00,0x01,0x00,0x01,0x00,0x07,0xF8,0x01,0x04,0x01,0x04,0x00,0x00,0x00,0x00},/*"t",84*/
{0x01,0x00,0x01,0xF8,0x00,0x04,0x00,0x04,0x00,0x04,0x01,0x08,0x01,0xFC,0x00,0x04},/*"u",85*/
{0x01,0x00,0x01,0x80,0x01,0x70,0x00,0x0C,0x00,0x10,0x01,0x60,0x01,0x80,0x01,0x00},/*"v",86*/
{0x01,0xF0,0x01,0x0C,0x00,0x30,0x01,0xC0,0x00,0x30,0x01,0x0C,0x01,0xF0,0x01,0x00},/*"w",87*/
{0x00,0x00,0x01,0x04,0x01,0x8C,0x00,0x74,0x01,0x70,0x01,0x8C,0x01,0x04,0x00,0x00},/*"x",88*/
{0x01,0x01,0x01,0x81,0x01,0x71,0x00,0x0E,0x00,0x18,0x01,0x60,0x01,0x80,0x01,0x00},/*"y",89*/
{0x00,0x00,0x01,0x84,0x01,0x0C,0x01,0x34,0x01,0x44,0x01,0x84,0x01,0x0C,0x00,0x00},/*"z",90*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x3E,0xFC,0x40,0x02,0x40,0x02},/*"{",91*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},/*"|",92*/
{0x00,0x00,0x40,0x02,0x40,0x02,0x3E,0xFC,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"}",93*/
{0x00,0x00,0x60,0x00,0x80,0x00,0x80,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x20,0x00},/*"~",94*/
};
const unsigned char Simulation_Asc3636[5][36]= {
{0x00,0x00,0x00,0x0F,0xFF,0xF8,0x0F,0xFF,0xF8,0x0C,0x0C,0x00,0x0C,0x0C,0x00,0x0C,0x0C,0x00,0x04,0x0C,0x00,0x06,0x0C,0x00,0x06,0x1C,0x00,0x03,0xF8,0x00,0x01,0xF0,
0x00,0x00,0x00,0x00},/*"P",0*/
{0x00,0x00,0x00,0x0F,0xFF,0xF8,0x0F,0xFF,0xF8,0x0F,0xF0,0x00,0x00,0xFF,0x80,0x00,0x07,0xF8,0x00,0x07,0xF8,0x01,0xFF,0x00,0x0F,0xE0,0x00,0x0F,0xFF,0xF8,0x0F,0xFF,
0xF8,0x00,0x00,0x00},/*"M",1*/
{0x00,0x00,0x00,0x01,0x80,0x10,0x07,0x80,0x70,0x06,0x00,0xF0,0x0C,0x01,0xD0,0x0C,0x03,0x90,0x0C,0x07,0x10,0x0E,0x1C,0x10,0x07,0xF8,0x10,0x03,0xE0,0x10,0x00,0x00,
0x10,0x00,0x00,0x00},/*"2",2*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x00,0x00,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00},/*".",3*/
{0x00,0x00,0x80,0x00,0x38,0xE0,0x07,0xF8,0x70,0x07,0xB0,0x10,0x04,0x20,0x18,0x04,0x20,0x18,0x04,0x30,0x18,0x04,0x30,0x30,0x04,0x3C,0xF0,0x04,0x1F,0xE0,0x00,0x03,
0x00,0x00,0x00,0x00},/*"5",4*/
};
const unsigned char Simulation_Asc3838[10][90]= {
{0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xFC,0x00,0x00,
0x00,0x3F,0xFF,0xC0,0x00,0x00,0x7F,0xFF,0xF0,0x00,
0x00,0xFC,0x01,0xF8,0x00,0x01,0xE0,0x00,0x78,0x00,
0x03,0xC0,0x00,0x1C,0x00,0x03,0x80,0x00,0x1C,0x00,
0x03,0x00,0x00,0x0E,0x00,0x03,0x00,0x00,0x0E,0x00,
0x03,0x80,0x00,0x1C,0x00,0x03,0xC0,0x00,0x3C,0x00,
0x01,0xF0,0x00,0x78,0x00,0x00,0xFF,0xFF,0xF0,0x00,
0x00,0x7F,0xFF,0xE0,0x00,0x00,0x0F,0xFF,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"0",0*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x38,0x00,0x00,0x00,0x00,0x70,
0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x00,0x01,0xFF,0xFF,0xFC,0x00,0x03,0xFF,0xFF,0xFC,0x00,0x03,0xFF,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"1",1*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x70,0x00,0x1C,0x00,0x00,0xF8,0x00,0x3C,0x00,0x01,0xE0,0x00,0x7C,0x00,0x03,0xC0,0x00,0xFC,0x00,0x03,0x80,
0x01,0xEC,0x00,0x03,0x80,0x03,0xCC,0x00,0x03,0x80,0x0F,0x0C,0x00,0x03,0x80,0x1E,0x0C,0x00,0x03,0x80,0x3C,0x0C,0x00,0x03,0x80,0xF8,0x0C,0x00,0x01,0xE7,0xE0,0x0C,
0x00,0x01,0xFF,0xC0,0x0C,0x00,0x00,0xFF,0x00,0x0C,0x00,0x00,0x18,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"2",2*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x30,0x00,0xE0,0x00,0x00,0x70,0x00,0xF0,0x00,0x01,0xF0,0x00,0x78,0x00,0x01,0xC0,0x00,0x3C,0x00,0x03,0x80,
0x00,0x1C,0x00,0x03,0x80,0x60,0x0C,0x00,0x03,0x00,0xE0,0x0C,0x00,0x03,0x00,0xE0,0x0C,0x00,0x03,0x80,0xE0,0x1C,0x00,0x03,0x81,0xF0,0x1C,0x00,0x01,0xE7,0xB8,0x78,
0x00,0x01,0xFF,0x3F,0xF8,0x00,0x00,0xFE,0x1F,0xF0,0x00,0x00,0x18,0x07,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"3",3*/
{0x00,0x00,0x03,0x80,0x00,0x00,0x00,0x07,0x80,0x00,0x00,0x00,0x1F,0x80,0x00,0x00,0x00,0x3F,0x80,0x00,0x00,0x00,0x7B,0x80,0x00,0x00,0x01,0xE3,0x80,0x00,0x00,0x03,
0xC3,0x80,0x00,0x00,0x0F,0x83,0x80,0x00,0x00,0x1E,0x03,0x80,0x00,0x00,0x7C,0x03,0x80,0x00,0x00,0xF0,0x03,0x80,0x00,0x03,0xFF,0xFF,0xFC,0x00,0x03,0xFF,0xFF,0xFC,
0x00,0x03,0xFF,0xFF,0xFC,0x00,0x00,0x00,0x03,0x80,0x00,0x00,0x00,0x03,0x80,0x00,0x00,0x00,0x03,0x80,0x00,0x00,0x00,0x00,0x00,0x00},/*"4",4*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0xE0,0x00,0x00,0x07,0xE0,0xF0,0x00,0x00,0xFF,0xE0,0x78,0x00,0x03,0xFF,0xC0,0x1C,0x00,0x03,0xF1,0x80,0x1C,0x00,0x03,0x83,
0x80,0x0E,0x00,0x03,0x83,0x80,0x0E,0x00,0x03,0x83,0x80,0x0E,0x00,0x03,0x83,0x80,0x0C,0x00,0x03,0x83,0x80,0x1C,0x00,0x03,0x81,0xC0,0x3C,0x00,0x03,0x81,0xE0,0x78,
0x00,0x03,0x80,0xFF,0xF8,0x00,0x03,0x80,0x7F,0xE0,0x00,0x00,0x00,0x1F,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"5",5*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0x3F,0xE0,0x00,0x00,0x00,0xFF,0xF0,0x00,0x00,0x03,0xF8,0xF8,0x00,0x00,0x07,0xE0,0x3C,0x00,0x00,0x1F,
0xC0,0x1C,0x00,0x00,0x3F,0x80,0x0C,0x00,0x00,0xF9,0x80,0x0E,0x00,0x01,0xF1,0x80,0x0E,0x00,0x03,0xC1,0x80,0x0E,0x00,0x03,0x01,0xC0,0x0C,0x00,0x02,0x01,0xC0,0x1C,
0x00,0x00,0x00,0xE0,0x7C,0x00,0x00,0x00,0xFF,0xF8,0x00,0x00,0x00,0x7F,0xF0,0x00,0x00,0x00,0x1F,0xC0,0x00,0x00,0x00,0x00,0x00,0x00},/*"6",6*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x80,0x00,0x00,0x00,0x03,0x80,0x00,0x00,0x00,0x03,0x80,0x00,0x00,0x00,0x03,0x80,0x00,0x1C,0x00,0x03,0x80,
0x00,0xFC,0x00,0x03,0x80,0x07,0xFC,0x00,0x03,0x80,0x1F,0xE0,0x00,0x03,0x80,0x7F,0x00,0x00,0x03,0x81,0xF8,0x00,0x00,0x03,0x87,0xE0,0x00,0x00,0x03,0x9F,0x00,0x00,
0x00,0x03,0xFC,0x00,0x00,0x00,0x03,0xF0,0x00,0x00,0x00,0x03,0xE0,0x00,0x00,0x00,0x03,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"7",7*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xE0,0x00,0x00,0x7E,0x1F,0xF0,0x00,0x00,0xFF,0xBF,0xF8,0x00,0x01,0xE7,0xF8,0x3C,0x00,0x03,0x81,0xF0,0x1C,0x00,0x03,0x80,
0xE0,0x0C,0x00,0x03,0x80,0xE0,0x0E,0x00,0x03,0x00,0xE0,0x0E,0x00,0x03,0x80,0xE0,0x0E,0x00,0x03,0x80,0xE0,0x0C,0x00,0x03,0x81,0xF0,0x1C,0x00,0x01,0xE7,0xB8,0x3C,
0x00,0x00,0xFF,0x3F,0xF8,0x00,0x00,0x7E,0x1F,0xF0,0x00,0x00,0x00,0x0F,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"8",8*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xC0,0x00,0x00,0x00,0x7F,0xE0,0x00,0x00,0x00,0xFF,0xF0,0x00,0x00,0x01,0xE0,0x78,0x00,0x00,0x03,0xC0,0x38,0x06,0x00,0x03,0x80,
0x18,0x1E,0x00,0x03,0x80,0x18,0x7E,0x00,0x03,0x80,0x19,0xF8,0x00,0x03,0x80,0x1F,0xE0,0x00,0x03,0x80,0x3F,0xC0,0x00,0x03,0xC0,0x7F,0x00,0x00,0x01,0xE1,0xFC,0x00,
0x00,0x00,0xFF,0xF0,0x00,0x00,0x00,0x7F,0xC0,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"9",9*/
};
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,206 @@
#ifndef _SIMULATION_LCD_H_
#define _SIMULATION_LCD_H_
#include "stm32f10x.h"
#include "bsp_clock.h"
#include "stdlib.h"
typedef struct
{
uint16_t width; // LCD 宽度
uint16_t height; // LCD 高度
uint16_t id; // LCD ID
uint8_t dir; // 横屏还是竖屏控制0竖屏1横屏。
uint8_t wramcmd; // 开始写gram指令
uint8_t setxcmd; // 设置x坐标指令
uint8_t setycmd; // 设置y坐标指令
}SimLCD_t;
extern SimLCD_t SimLCD; //管理LCD重要参数
//LCD的画笔颜色和背景色
extern uint16_t SimLCD_PointColor;// 画笔颜色
extern uint16_t SimLCD_BackColor;// 背景色
//扫描方向定义
#define SimLCD_L2R_U2D 0 //从左到右,从上到下
#define SimLCD_L2R_D2U 1 //从左到右,从下到上
#define SimLCD_R2L_U2D 2 //从右到左,从上到下
#define SimLCD_R2L_D2U 3 //从右到左,从下到上
#define SimLCD_U2D_L2R 4 //从上到下,从左到右
#define SimLCD_U2D_R2L 5 //从上到下,从右到左
#define SimLCD_D2U_L2R 6 //从下到上,从左到右
#define SimLCD_D2U_R2L 7 //从下到上,从右到左
// 默认的扫描方向
#define SimLCD_DefScanDir SimLCD_L2R_U2D
void SimLCD_Init(void);
void SimLCD_Clear(uint16_t color);
void SimLCD_DrawPoint(uint16_t x, uint16_t y);
uint16_t SimLCD_ReadPoint(uint16_t x, uint16_t y);
void SimLCD_Fill(uint16_t sx,uint16_t sy,uint16_t ex,uint16_t ey,uint16_t color);
void SimLCD_ColorFill(uint16_t sx,uint16_t sy,uint16_t ex,uint16_t ey,uint16_t *color);
void SimLCD_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
void SimLCD_DrawCircle(uint16_t x0,uint16_t y0,uint8_t r);
void SimLCD_DrawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
void SimLCD_ShowNum(uint16_t x,uint16_t y,uint32_t num,uint8_t len,uint8_t size);
void SimLCD_ShowxNum(uint16_t x,uint16_t y,uint32_t num,uint8_t len,uint8_t size,uint8_t mode);
void SimLCD_ShowString(uint16_t x,uint16_t y,uint16_t width,uint16_t height,uint8_t size,uint8_t *p);
void SimLCD_FillTorus(uint16_t x0,uint16_t y0,uint16_t r0,uint16_t r1);
void SimLCD_Show_PM2_5(uint16_t x, uint16_t y);
void SimLCD_Show_ug_m3(uint16_t x, uint16_t y);
void SimLCD_Show_PM1_0(uint16_t x, uint16_t y);
void SimLCD_Show_PM10(uint16_t x, uint16_t y);
void SimLCD_NewShowChar(uint16_t x, uint16_t y, uint16_t ziti);
void SimLCD_NewShowNum32(uint16_t x, uint16_t y, uint16_t ziti);
//画笔颜色
#define SimLCDColor_WHITE 0xFFFF
#define SimLCDColor_BLACK 0x0000
#define SimLCDColor_BLUE 0x001F
#define SimLCDColor_BRED 0XF81F
#define SimLCDColor_GRED 0XFFE0
#define SimLCDColor_GBLUE 0X07FF
#define SimLCDColor_RED 0xF800
#define SimLCDColor_MAGENTA 0xF81F
#define SimLCDColor_GREEN 0x07E0
#define SimLCDColor_CYAN 0x7FFF
#define SimLCDColor_YELLOW 0xFFE0
#define SimLCDColor_BROWN 0XBC40 //棕色
#define SimLCDColor_BRRED 0XFC07 //棕红色
#define SimLCDColor_GRAY 0X8430 //灰色
//GUI颜色
#define SimLCDColor_DARKBLUE 0X01CF //深蓝色
#define SimLCDColor_LIGHTBLUE 0X7D7C //浅蓝色
#define SimLCDColor_GRAYBLUE 0X5458 //灰蓝色
//以上三色为PANEL的颜色
#define SimLCDColor_LIGHTGREEN 0X841F //浅绿色
//#define SimLCDColor_LIGHTGRAY 0XEF5B //浅灰色(PANNEL)
#define SimLCDColor_LGRAY 0XC618 //浅灰色(PANNEL),窗体背景色
#define SimLCDColor_LGRAYBLUE 0XA651 //浅灰蓝色(中间层颜色)
#define SimLCDColor_LBBLUE 0X2B12 //浅棕蓝色(选择条目的反色)
//9320/9325 LCD寄存器
#define SimLCD_R0 0x00
#define SimLCD_R1 0x01
#define SimLCD_R2 0x02
#define SimLCD_R3 0x03
#define SimLCD_R4 0x04
#define SimLCD_R5 0x05
#define SimLCD_R6 0x06
#define SimLCD_R7 0x07
#define SimLCD_R8 0x08
#define SimLCD_R9 0x09
#define SimLCD_R10 0x0A
#define SimLCD_R12 0x0C
#define SimLCD_R13 0x0D
#define SimLCD_R14 0x0E
#define SimLCD_R15 0x0F
#define SimLCD_R16 0x10
#define SimLCD_R17 0x11
#define SimLCD_R18 0x12
#define SimLCD_R19 0x13
#define SimLCD_R20 0x14
#define SimLCD_R21 0x15
#define SimLCD_R22 0x16
#define SimLCD_R23 0x17
#define SimLCD_R24 0x18
#define SimLCD_R25 0x19
#define SimLCD_R26 0x1A
#define SimLCD_R27 0x1B
#define SimLCD_R28 0x1C
#define SimLCD_R29 0x1D
#define SimLCD_R30 0x1E
#define SimLCD_R31 0x1F
#define SimLCD_R32 0x20
#define SimLCD_R33 0x21
#define SimLCD_R34 0x22
#define SimLCD_R36 0x24
#define SimLCD_R37 0x25
#define SimLCD_R40 0x28
#define SimLCD_R41 0x29
#define SimLCD_R43 0x2B
#define SimLCD_R45 0x2D
#define SimLCD_R48 0x30
#define SimLCD_R49 0x31
#define SimLCD_R50 0x32
#define SimLCD_R51 0x33
#define SimLCD_R52 0x34
#define SimLCD_R53 0x35
#define SimLCD_R54 0x36
#define SimLCD_R55 0x37
#define SimLCD_R56 0x38
#define SimLCD_R57 0x39
#define SimLCD_R59 0x3B
#define SimLCD_R60 0x3C
#define SimLCD_R61 0x3D
#define SimLCD_R62 0x3E
#define SimLCD_R63 0x3F
#define SimLCD_R64 0x40
#define SimLCD_R65 0x41
#define SimLCD_R66 0x42
#define SimLCD_R67 0x43
#define SimLCD_R68 0x44
#define SimLCD_R69 0x45
#define SimLCD_R70 0x46
#define SimLCD_R71 0x47
#define SimLCD_R72 0x48
#define SimLCD_R73 0x49
#define SimLCD_R74 0x4A
#define SimLCD_R75 0x4B
#define SimLCD_R76 0x4C
#define SimLCD_R77 0x4D
#define SimLCD_R78 0x4E
#define SimLCD_R79 0x4F
#define SimLCD_R80 0x50
#define SimLCD_R81 0x51
#define SimLCD_R82 0x52
#define SimLCD_R83 0x53
#define SimLCD_R96 0x60
#define SimLCD_R97 0x61
#define SimLCD_R106 0x6A
#define SimLCD_R118 0x76
#define SimLCD_R128 0x80
#define SimLCD_R129 0x81
#define SimLCD_R130 0x82
#define SimLCD_R131 0x83
#define SimLCD_R132 0x84
#define SimLCD_R133 0x85
#define SimLCD_R134 0x86
#define SimLCD_R135 0x87
#define SimLCD_R136 0x88
#define SimLCD_R137 0x89
#define SimLCD_R139 0x8B
#define SimLCD_R140 0x8C
#define SimLCD_R141 0x8D
#define SimLCD_R143 0x8F
#define SimLCD_R144 0x90
#define SimLCD_R145 0x91
#define SimLCD_R146 0x92
#define SimLCD_R147 0x93
#define SimLCD_R148 0x94
#define SimLCD_R149 0x95
#define SimLCD_R150 0x96
#define SimLCD_R151 0x97
#define SimLCD_R152 0x98
#define SimLCD_R153 0x99
#define SimLCD_R154 0x9A
#define SimLCD_R157 0x9D
#define SimLCD_R192 0xC0
#define SimLCD_R193 0xC1
#define SimLCD_R229 0xE5
#endif

View File

@@ -0,0 +1,116 @@
#include "stmflash.h"
//读取指定地址的半字(16位数据)
//faddr:读地址(此地址必须为2的倍数!!)
//返回值:对应数据.
u16 STMFLASH_ReadHalfWord(u32 faddr)
{
return *(vu16*)faddr;
}
#if STM32_FLASH_WREN //如果使能了写
//不检查的写入
//WriteAddr:起始地址
//pBuffer:数据指针
//NumToWrite:半字(16位)数
void STMFLASH_Write_NoCheck(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite)
{
u16 i;
for(i=0;i<NumToWrite;i++)
{
FLASH_ProgramHalfWord(WriteAddr,pBuffer[i]);
WriteAddr+=2;//地址增加2.
}
}
//从指定地址开始写入指定长度的数据
//WriteAddr:起始地址(此地址必须为2的倍数!!)
//pBuffer:数据指针
//NumToWrite:半字(16位)数(就是要写入的16位数据的个数.)
#if STM32_FLASH_SIZE<256
#define STM_SECTOR_SIZE 1024 //字节
#else
#define STM_SECTOR_SIZE 2048
#endif
u16 STMFLASH_BUF[STM_SECTOR_SIZE/2];//最多是2K字节
void STMFLASH_Write(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite)
{
u32 secpos; //扇区地址
u16 secoff; //扇区内偏移地址(16位字计算)
u16 secremain; //扇区内剩余地址(16位字计算)
u16 i;
u32 offaddr; //去掉0X08000000后的地址
if(WriteAddr<STM32_FLASH_BASE||(WriteAddr>=(STM32_FLASH_BASE+1024*STM32_FLASH_SIZE)))return;//非法地址
FLASH_Unlock(); //解锁
offaddr=WriteAddr-STM32_FLASH_BASE; //实际偏移地址.
secpos=offaddr/STM_SECTOR_SIZE; //扇区地址 0~127 for STM32F103RBT6
secoff=(offaddr%STM_SECTOR_SIZE)/2; //在扇区内的偏移(2个字节为基本单位.)
secremain=STM_SECTOR_SIZE/2-secoff; //扇区剩余空间大小
if(NumToWrite<=secremain)secremain=NumToWrite;//不大于该扇区范围
while(1)
{
STMFLASH_Read(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);//读出整个扇区的内容
for(i=0;i<secremain;i++)//校验数据
{
if(STMFLASH_BUF[secoff+i]!=0XFFFF)break;//需要擦除
}
if(i<secremain)//需要擦除
{
FLASH_ErasePage(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE);//擦除这个扇区
for(i=0;i<secremain;i++)//复制
{
STMFLASH_BUF[i+secoff]=pBuffer[i];
}
STMFLASH_Write_NoCheck(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);//写入整个扇区
}else STMFLASH_Write_NoCheck(WriteAddr,pBuffer,secremain);//写已经擦除了的,直接写入扇区剩余区间.
if(NumToWrite==secremain)break;//写入结束了
else//写入未结束
{
secpos++; //扇区地址增1
secoff=0; //偏移位置为0
pBuffer+=secremain; //指针偏移
WriteAddr+=secremain; //写地址偏移
NumToWrite-=secremain; //字节(16位)数递减
if(NumToWrite>(STM_SECTOR_SIZE/2))secremain=STM_SECTOR_SIZE/2;//下一个扇区还是写不完
else secremain=NumToWrite;//下一个扇区可以写完了
}
};
FLASH_Lock();//上锁
}
#endif
//从指定地址开始读出指定长度的数据
//ReadAddr:起始地址
//pBuffer:数据指针
//NumToWrite:半字(16位)数
void STMFLASH_Read(u32 ReadAddr,u16 *pBuffer,u16 NumToRead)
{
u16 i;
for(i=0;i<NumToRead;i++)
{
pBuffer[i]=STMFLASH_ReadHalfWord(ReadAddr);//读取2个字节.
ReadAddr+=2;//偏移2个字节.
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
//WriteAddr:起始地址
//WriteData:要写入的数据
void Test_Write(u32 WriteAddr,u16 WriteData)
{
STMFLASH_Write(WriteAddr,&WriteData,1);//写入一个字
}

View File

@@ -0,0 +1,44 @@
#ifndef __STMFLASH_H__
#define __STMFLASH_H__
#include "stm32f10x.h"
#include "bsp_clock.h"
//////////////////////////////////////////////////////////////////////////////////////////////////////
//用户根据自己的需要设置
#define STM32_FLASH_SIZE 512 //所选STM32的FLASH容量大小(单位为K)
#define STM32_FLASH_WREN 1 //使能FLASH写入(0不是能;1使能)
//////////////////////////////////////////////////////////////////////////////////////////////////////
//FLASH起始地址
#define STM32_FLASH_BASE 0x08000000 //STM32 FLASH的起始地址
//FLASH解锁键值
u16 STMFLASH_ReadHalfWord(u32 faddr); //读出半字
void STMFLASH_WriteLenByte(u32 WriteAddr,u32 DataToWrite,u16 Len); //指定地址开始写入指定长度的数据
u32 STMFLASH_ReadLenByte(u32 ReadAddr,u16 Len); //指定地址开始读取指定长度数据
void STMFLASH_Write(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite); //从指定地址开始写入指定长度的数据
void STMFLASH_Read(u32 ReadAddr,u16 *pBuffer,u16 NumToRead); //从指定地址开始读出指定长度的数据
//测试写入
void Test_Write(u32 WriteAddr,u16 WriteData);
#endif

View File

@@ -0,0 +1,77 @@
/**
******************************************************************************
* @file Project/STM32F10x_StdPeriph_Template/stm32f10x_conf.h
* @author MCD Application Team
* @version V3.5.0
* @date 08-April-2011
* @brief Library configuration file.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM32F10x_CONF_H
#define __STM32F10x_CONF_H
/* Includes ------------------------------------------------------------------*/
/* Uncomment/Comment the line below to enable/disable peripheral header file inclusion */
#include "stm32f10x_adc.h"
#include "stm32f10x_bkp.h"
#include "stm32f10x_can.h"
#include "stm32f10x_cec.h"
#include "stm32f10x_crc.h"
#include "stm32f10x_dac.h"
#include "stm32f10x_dbgmcu.h"
#include "stm32f10x_dma.h"
#include "stm32f10x_exti.h"
#include "stm32f10x_flash.h"
#include "stm32f10x_fsmc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_i2c.h"
#include "stm32f10x_iwdg.h"
#include "stm32f10x_pwr.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_rtc.h"
#include "stm32f10x_sdio.h"
#include "stm32f10x_spi.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_wwdg.h"
#include "misc.h" /* High level functions for NVIC and SysTick (add-on to CMSIS functions) */
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Uncomment the line below to expanse the "assert_param" macro in the
Standard Peripheral Library drivers code */
/* #define USE_FULL_ASSERT 1 */
/* Exported macro ------------------------------------------------------------*/
#ifdef USE_FULL_ASSERT
/**
* @brief The assert_param macro is used for function's parameters check.
* @param expr: If expr is false, it calls assert_failed function which reports
* the name of the source file and the source line number of the call
* that failed. If expr is true, it returns no value.
* @retval None
*/
#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))
/* Exported functions ------------------------------------------------------- */
void assert_failed(uint8_t* file, uint32_t line);
#else
#define assert_param(expr) ((void)0)
#endif /* USE_FULL_ASSERT */
#endif /* __STM32F10x_CONF_H */
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

View File

@@ -0,0 +1,160 @@
/**
******************************************************************************
* @file Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c
* @author MCD Application Team
* @version V3.5.0
* @date 08-April-2011
* @brief Main Interrupt Service Routines.
* This file provides template for all exceptions handler and
* peripherals interrupt service routine.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
/** @addtogroup STM32F10x_StdPeriph_Template
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/******************************************************************************/
/* Cortex-M3 Processor Exceptions Handlers */
/******************************************************************************/
/**
* @brief This function handles NMI exception.
* @param None
* @retval None
*/
void NMI_Handler(void)
{
}
/**
* @brief This function handles Hard Fault exception.
* @param None
* @retval None
*/
void HardFault_Handler(void)
{
/* Go to infinite loop when Hard Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Memory Manage exception.
* @param None
* @retval None
*/
void MemManage_Handler(void)
{
/* Go to infinite loop when Memory Manage exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Bus Fault exception.
* @param None
* @retval None
*/
void BusFault_Handler(void)
{
/* Go to infinite loop when Bus Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Usage Fault exception.
* @param None
* @retval None
*/
void UsageFault_Handler(void)
{
/* Go to infinite loop when Usage Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles SVCall exception.
* @param None
* @retval None
*/
void SVC_Handler(void)
{
}
/**
* @brief This function handles Debug Monitor exception.
* @param None
* @retval None
*/
void DebugMon_Handler(void)
{
}
/**
* @brief This function handles PendSVC exception.
* @param None
* @retval None
*/
void PendSV_Handler(void)
{
}
/**
* @brief This function handles SysTick Handler.
* @param None
* @retval None
*/
void SysTick_Handler(void)
{
}
/******************************************************************************/
/* STM32F10x Peripherals Interrupt Handlers */
/* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */
/* available peripheral interrupt handler's name please refer to the startup */
/* file (startup_stm32f10x_xx.s). */
/******************************************************************************/
/**
* @brief This function handles PPP interrupt request.
* @param None
* @retval None
*/
/*void PPP_IRQHandler(void)
{
}*/
/**
* @}
*/
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

View File

@@ -0,0 +1,54 @@
/**
******************************************************************************
* @file Project/STM32F10x_StdPeriph_Template/stm32f10x_it.h
* @author MCD Application Team
* @version V3.5.0
* @date 08-April-2011
* @brief This file contains the headers of the interrupt handlers.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM32F10x_IT_H
#define __STM32F10x_IT_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
void NMI_Handler(void);
void HardFault_Handler(void);
void MemManage_Handler(void);
void BusFault_Handler(void);
void UsageFault_Handler(void);
void SVC_Handler(void);
void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
#ifdef __cplusplus
}
#endif
#endif /* __STM32F10x_IT_H */
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/