字符转换:void Stringchange(const char* input,char* output)

本文解析了一道华为校园招聘中的机试题目,要求实现字符串中字母的特定转换逻辑,并提供了一个完整的C语言解决方案。通过示例代码展示了如何遍历字符串并对指定字符进行替换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

华为校招机试题1:给定一个字符串,把字符串内的字母转换成该字母的下一个字母,a换成b,z换成a,Z换成A,其他字符不变,编写函数void Stringchange(const char* input,char* output),其中input是输入字符串,output是输出字符串。给出代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void Stringchange(const char* input,char* output);


int main()
{
const char *a="abcdef2gzA4HZ";
char b[100];
Stringchange(a,b);
system("pause");
return 0;
}

void Stringchange(const char* input,char* output)
{
char *p=output;//要先保存output起始地址
for(;*input!='\0';input++,output++)
{
if(*input=='z'||*input=='Z')
*output=*input-25;
else if((*input>='a'&&*input<'z')||(*input>='A'&&*input<'Z'))
*output=*input+1;
else
*output=*input;
}
*output='\0';
printf("%s",p);
}

/* stdlib.h: ANSI draft (X3J11 May 88) library header, section 4.10 */ /* Copyright (C) Codemist Ltd., 1988-1993. */ /* Copyright 1991-1998,2014 ARM Limited. All rights reserved. */ /* * RCS $Revision$ * Checkin $Date$ * Revising $Author: agrant $ */ /* * stdlib.h declares four types, several general purpose functions, * and defines several macros. */ #ifndef __stdlib_h #define __stdlib_h #define __ARMCLIB_VERSION 5060034 #if defined(__clang__) || (defined(__ARMCC_VERSION) && !defined(__STRICT_ANSI__)) /* armclang and non-strict armcc allow 'long long' in system headers */ #define __LONGLONG long long #else /* strict armcc has '__int64' */ #define __LONGLONG __int64 #endif #define _ARMABI __declspec(__nothrow) #define _ARMABI_PURE __declspec(__nothrow) __attribute__((const)) #define _ARMABI_NORETURN __declspec(__nothrow) __declspec(__noreturn) #define _ARMABI_THROW #ifndef __STDLIB_DECLS #define __STDLIB_DECLS /* * Some of these declarations are new in C99. To access them in C++ * you can use -D__USE_C99_STDLIB (or -D__USE_C99ALL). */ #ifndef __USE_C99_STDLIB #if defined(__USE_C99_ALL) || (defined(__STDC_VERSION__) && 199901L <= __STDC_VERSION__) || (defined(__cplusplus) && 201103L <= __cplusplus) #define __USE_C99_STDLIB 1 #endif #endif #undef __CLIBNS #ifdef __cplusplus namespace std { #define __CLIBNS ::std:: extern "C" { #else #define __CLIBNS #endif /* __cplusplus */ #if defined(__cplusplus) || !defined(__STRICT_ANSI__) /* unconditional in C++ and non-strict C for consistency of debug info */ #if __sizeof_ptr == 8 typedef unsigned long size_t; /* see <stddef.h> */ #else typedef unsigned int size_t; /* see <stddef.h> */ #endif #elif !defined(__size_t) #define __size_t 1 #if __sizeof_ptr == 8 typedef unsigned long size_t; /* see <stddef.h> */ #else typedef unsigned int size_t; /* see <stddef.h> */ #endif #endif #undef NULL #define NULL 0 /* see <stddef.h> */ #ifndef __cplusplus /* wchar_t is a builtin type for C++ */ #if !defined(__STRICT_ANSI__) /* unconditional in non-strict C for consistency of debug info */ #if defined(__WCHAR32) || (defined(__ARM_SIZEOF_WCHAR_T) && __ARM_SIZEOF_WCHAR_T == 4) typedef unsigned int wchar_t; /* see <stddef.h> */ #else typedef unsigned short wchar_t; /* see <stddef.h> */ #endif #elif !defined(__wchar_t) #define __wchar_t 1 #if defined(__WCHAR32) || (defined(__ARM_SIZEOF_WCHAR_T) && __ARM_SIZEOF_WCHAR_T == 4) typedef unsigned int wchar_t; /* see <stddef.h> */ #else typedef unsigned short wchar_t; /* see <stddef.h> */ #endif #endif #endif typedef struct div_t { int quot, rem; } div_t; /* type of the value returned by the div function. */ typedef struct ldiv_t { long int quot, rem; } ldiv_t; /* type of the value returned by the ldiv function. */ #if !defined(__STRICT_ANSI__) || __USE_C99_STDLIB typedef struct lldiv_t { __LONGLONG quot, rem; } lldiv_t; /* type of the value returned by the lldiv function. */ #endif #ifdef __EXIT_FAILURE # define EXIT_FAILURE __EXIT_FAILURE /* * an integral expression which may be used as an argument to the exit * function to return unsuccessful termination status to the host * environment. */ #else # define EXIT_FAILURE 1 /* unixoid */ #endif #define EXIT_SUCCESS 0 /* * an integral expression which may be used as an argument to the exit * function to return successful termination status to the host * environment. */ /* * Defining __USE_ANSI_EXAMPLE_RAND at compile time switches to * the example implementation of rand() and srand() provided in * the ANSI C standard. This implementation is very poor, but is * provided for completeness. */ #ifdef __USE_ANSI_EXAMPLE_RAND #define srand _ANSI_srand #define rand _ANSI_rand #define RAND_MAX 0x7fff #else #define RAND_MAX 0x7fffffff #endif /* * RAND_MAX: an integral constant expression, the value of which * is the maximum value returned by the rand function. */ extern _ARMABI int __aeabi_MB_CUR_MAX(void); #define MB_CUR_MAX ( __aeabi_MB_CUR_MAX() ) /* * a positive integer expression whose value is the maximum number of bytes * in a multibyte character for the extended character set specified by the * current locale (category LC_CTYPE), and whose value is never greater * than MB_LEN_MAX. */ /* * If the compiler supports signalling nans as per N965 then it * will define __SUPPORT_SNAN__, in which case a user may define * _WANT_SNAN in order to obtain a compliant version of the strtod * family of functions. */ #if defined(__SUPPORT_SNAN__) && defined(_WANT_SNAN) #pragma import(__use_snan) #endif extern _ARMABI double atof(const char * /*nptr*/) __attribute__((__nonnull__(1))); /* * converts the initial part of the string pointed to by nptr to double * representation. * Returns: the converted value. */ extern _ARMABI int atoi(const char * /*nptr*/) __attribute__((__nonnull__(1))); /* * converts the initial part of the string pointed to by nptr to int * representation. * Returns: the converted value. */ extern _ARMABI long int atol(const char * /*nptr*/) __attribute__((__nonnull__(1))); /* * converts the initial part of the string pointed to by nptr to long int * representation. * Returns: the converted value. */ #if !defined(__STRICT_ANSI__) || __USE_C99_STDLIB extern _ARMABI __LONGLONG atoll(const char * /*nptr*/) __attribute__((__nonnull__(1))); /* * converts the initial part of the string pointed to by nptr to * long long int representation. * Returns: the converted value. */ #endif extern _ARMABI double strtod(const char * __restrict /*nptr*/, char ** __restrict /*endptr*/) __attribute__((__nonnull__(1))); /* * converts the initial part of the string pointed to by nptr to double * representation. First it decomposes the input string into three parts: * an initial, possibly empty, sequence of white-space characters (as * specified by the isspace function), a subject sequence resembling a * floating point constant; and a final string of one or more unrecognised * characters, including the terminating null character of the input string. * Then it attempts to convert the subject sequence to a floating point * number, and returns the result. A pointer to the final string is stored * in the object pointed to by endptr, provided that endptr is not a null * pointer. * Returns: the converted value if any. If no conversion could be performed, * zero is returned. If the correct value is outside the range of * representable values, plus or minus HUGE_VAL is returned * (according to the sign of the value), and the value of the macro * ERANGE is stored in errno. If the correct value would cause * underflow, zero is returned and the value of the macro ERANGE is * stored in errno. */ #if !defined(__STRICT_ANSI__) || __USE_C99_STDLIB extern _ARMABI float strtof(const char * __restrict /*nptr*/, char ** __restrict /*endptr*/) __attribute__((__nonnull__(1))); extern _ARMABI long double strtold(const char * __restrict /*nptr*/, char ** __restrict /*endptr*/) __attribute__((__nonnull__(1))); /* * same as strtod, but return float and long double respectively. */ #endif extern _ARMABI long int strtol(const char * __restrict /*nptr*/, char ** __restrict /*endptr*/, int /*base*/) __attribute__((__nonnull__(1))); /* * converts the initial part of the string pointed to by nptr to long int * representation. First it decomposes the input string into three parts: * an initial, possibly empty, sequence of white-space characters (as * specified by the isspace function), a subject sequence resembling an * integer represented in some radix determined by the value of base, and a * final string of one or more unrecognised characters, including the * terminating null character of the input string. Then it attempts to * convert the subject sequence to an integer, and returns the result. * If the value of base is 0, the expected form of the subject sequence is * that of an integer constant (described in ANSI Draft, section 3.1.3.2), * optionally preceded by a '+' or '-' sign, but not including an integer * suffix. If the value of base is between 2 and 36, the expected form of * the subject sequence is a sequence of letters and digits representing an * integer with the radix specified by base, optionally preceded by a plus * or minus sign, but not including an integer suffix. The letters from a * (or A) through z (or Z) are ascribed the values 10 to 35; only letters * whose ascribed values are less than that of the base are permitted. If * the value of base is 16, the characters 0x or 0X may optionally precede * the sequence of letters and digits following the sign if present. * A pointer to the final string is stored in the object * pointed to by endptr, provided that endptr is not a null pointer. * Returns: the converted value if any. If no conversion could be performed, * zero is returned and nptr is stored in *endptr. * If the correct value is outside the range of * representable values, LONG_MAX or LONG_MIN is returned * (according to the sign of the value), and the value of the * macro ERANGE is stored in errno. */ extern _ARMABI unsigned long int strtoul(const char * __restrict /*nptr*/, char ** __restrict /*endptr*/, int /*base*/) __attribute__((__nonnull__(1))); /* * converts the initial part of the string pointed to by nptr to unsigned * long int representation. First it decomposes the input string into three * parts: an initial, possibly empty, sequence of white-space characters (as * determined by the isspace function), a subject sequence resembling an * unsigned integer represented in some radix determined by the value of * base, and a final string of one or more unrecognised characters, * including the terminating null character of the input string. Then it * attempts to convert the subject sequence to an unsigned integer, and * returns the result. If the value of base is zero, the expected form of * the subject sequence is that of an integer constant (described in ANSI * Draft, section 3.1.3.2), optionally preceded by a '+' or '-' sign, but * not including an integer suffix. If the value of base is between 2 and * 36, the expected form of the subject sequence is a sequence of letters * and digits representing an integer with the radix specified by base, * optionally preceded by a '+' or '-' sign, but not including an integer * suffix. The letters from a (or A) through z (or Z) stand for the values * 10 to 35; only letters whose ascribed values are less than that of the * base are permitted. If the value of base is 16, the characters 0x or 0X * may optionally precede the sequence of letters and digits following the * sign, if present. A pointer to the final string is stored in the object * pointed to by endptr, provided that endptr is not a null pointer. * Returns: the converted value if any. If no conversion could be performed, * zero is returned and nptr is stored in *endptr. * If the correct value is outside the range of * representable values, ULONG_MAX is returned, and the value of * the macro ERANGE is stored in errno. */ /* C90 reserves all names beginning with 'str' */ extern _ARMABI __LONGLONG strtoll(const char * __restrict /*nptr*/, char ** __restrict /*endptr*/, int /*base*/) __attribute__((__nonnull__(1))); /* * as strtol but returns a long long int value. If the correct value is * outside the range of representable values, LLONG_MAX or LLONG_MIN is * returned (according to the sign of the value), and the value of the * macro ERANGE is stored in errno. */ extern _ARMABI unsigned __LONGLONG strtoull(const char * __restrict /*nptr*/, char ** __restrict /*endptr*/, int /*base*/) __attribute__((__nonnull__(1))); /* * as strtoul but returns an unsigned long long int value. If the correct * value is outside the range of representable values, ULLONG_MAX is returned, * and the value of the macro ERANGE is stored in errno. */ extern _ARMABI int rand(void); /* * Computes a sequence of pseudo-random integers in the range 0 to RAND_MAX. * Uses an additive generator (Mitchell & Moore) of the form: * Xn = (X[n-24] + X[n-55]) MOD 2^31 * This is described in section 3.2.2 of Knuth, vol 2. It's period is * in excess of 2^55 and its randomness properties, though unproven, are * conjectured to be good. Empirical testing since 1958 has shown no flaws. * Returns: a pseudo-random integer. */ extern _ARMABI void srand(unsigned int /*seed*/); /* * uses its argument as a seed for a new sequence of pseudo-random numbers * to be returned by subsequent calls to rand. If srand is then called with * the same seed value, the sequence of pseudo-random numbers is repeated. * If rand is called before any calls to srand have been made, the same * sequence is generated as when srand is first called with a seed value * of 1. */ struct _rand_state { int __x[57]; }; extern _ARMABI int _rand_r(struct _rand_state *); extern _ARMABI void _srand_r(struct _rand_state *, unsigned int); struct _ANSI_rand_state { int __x[1]; }; extern _ARMABI int _ANSI_rand_r(struct _ANSI_rand_state *); extern _ARMABI void _ANSI_srand_r(struct _ANSI_rand_state *, unsigned int); /* * Re-entrant variants of both flavours of rand, which operate on * an explicitly supplied state buffer. */ extern _ARMABI void *calloc(size_t /*nmemb*/, size_t /*size*/); /* * allocates space for an array of nmemb objects, each of whose size is * 'size'. The space is initialised to all bits zero. * Returns: either a null pointer or a pointer to the allocated space. */ extern _ARMABI void free(void * /*ptr*/); /* * causes the space pointed to by ptr to be deallocated (i.e., made * available for further allocation). If ptr is a null pointer, no action * occurs. Otherwise, if ptr does not match a pointer earlier returned by * calloc, malloc or realloc or if the space has been deallocated by a call * to free or realloc, the behaviour is undefined. */ extern _ARMABI void *malloc(size_t /*size*/); /* * allocates space for an object whose size is specified by 'size' and whose * value is indeterminate. * Returns: either a null pointer or a pointer to the allocated space. */ extern _ARMABI void *realloc(void * /*ptr*/, size_t /*size*/); /* * changes the size of the object pointed to by ptr to the size specified by * size. The contents of the object shall be unchanged up to the lesser of * the new and old sizes. If the new size is larger, the value of the newly * allocated portion of the object is indeterminate. If ptr is a null * pointer, the realloc function behaves like a call to malloc for the * specified size. Otherwise, if ptr does not match a pointer earlier * returned by calloc, malloc or realloc, or if the space has been * deallocated by a call to free or realloc, the behaviour is undefined. * If the space cannot be allocated, the object pointed to by ptr is * unchanged. If size is zero and ptr is not a null pointer, the object it * points to is freed. * Returns: either a null pointer or a pointer to the possibly moved * allocated space. */ #if !defined(__STRICT_ANSI__) extern _ARMABI int posix_memalign(void ** /*ret*/, size_t /*alignment*/, size_t /*size*/); /* * allocates space for an object of size 'size', aligned to a * multiple of 'alignment' (which must be a power of two and at * least 4). * * On success, a pointer to the allocated object is stored in * *ret, and zero is returned. On failure, the return value is * either ENOMEM (allocation failed because no suitable piece of * memory was available) or EINVAL (the 'alignment' parameter was * invalid). */ #endif typedef int (*__heapprt)(void *, char const *, ...); extern _ARMABI void __heapstats(int (* /*dprint*/)(void * /*param*/, char const * /*format*/, ...), void * /*param*/) __attribute__((__nonnull__(1))); /* * reports current heap statistics (eg. number of free blocks in * the free-list). Output is as implementation-defined free-form * text, provided via the dprint function. `param' gives an * extra data word to pass to dprint. You can call * __heapstats(fprintf,stdout) by casting fprintf to the above * function type; the typedef `__heapprt' is provided for this * purpose. * * `dprint' will not be called while the heap is being examined, * so it can allocate memory itself without trouble. */ extern _ARMABI int __heapvalid(int (* /*dprint*/)(void * /*param*/, char const * /*format*/, ...), void * /*param*/, int /*verbose*/) __attribute__((__nonnull__(1))); /* * performs a consistency check on the heap. Errors are reported * through dprint, like __heapstats. If `verbose' is nonzero, * full diagnostic information on the heap state is printed out. * * This routine probably won't work if the heap isn't a * contiguous chunk (for example, if __user_heap_extend has been * overridden). * * `dprint' may be called while the heap is being examined or * even in an invalid state, so it must perform no memory * allocation. In particular, if `dprint' calls (or is) a stdio * function, the stream it outputs to must already have either * been written to or been setvbuf'ed, or else the system will * allocate buffer space for it on the first call to dprint. */ extern _ARMABI_NORETURN void abort(void); /* * causes abnormal program termination to occur, unless the signal SIGABRT * is being caught and the signal handler does not return. Whether open * output streams are flushed or open streams are closed or temporary * files removed is implementation-defined. * An implementation-defined form of the status 'unsuccessful termination' * is returned to the host environment by means of a call to * raise(SIGABRT). */ extern _ARMABI int atexit(void (* /*func*/)(void)) __attribute__((__nonnull__(1))); /* * registers the function pointed to by func, to be called without its * arguments at normal program termination. It is possible to register at * least 32 functions. * Returns: zero if the registration succeeds, nonzero if it fails. */ #if defined(__EDG__) && !defined(__GNUC__) #define __LANGUAGE_LINKAGE_CHANGES_FUNCTION_TYPE #endif #if defined(__cplusplus) && defined(__LANGUAGE_LINKAGE_CHANGES_FUNCTION_TYPE) /* atexit that takes a ptr to a function with C++ linkage * but not in GNU mode */ typedef void (* __C_exitfuncptr)(); extern "C++" inline int atexit(void (* __func)()) { return atexit((__C_exitfuncptr)__func); } #endif extern _ARMABI_NORETURN void exit(int /*status*/); /* * causes normal program termination to occur. If more than one call to the * exit function is executed by a program, the behaviour is undefined. * First, all functions registered by the atexit function are called, in the * reverse order of their registration. * Next, all open output streams are flushed, all open streams are closed, * and all files created by the tmpfile function are removed. * Finally, control is returned to the host environment. If the value of * status is zero or EXIT_SUCCESS, an implementation-defined form of the * status 'successful termination' is returned. If the value of status is * EXIT_FAILURE, an implementation-defined form of the status * 'unsuccessful termination' is returned. Otherwise the status returned * is implementation-defined. */ extern _ARMABI_NORETURN void _Exit(int /*status*/); /* * causes normal program termination to occur. No functions registered * by the atexit function are called. * In this implementation, all open output streams are flushed, all * open streams are closed, and all files created by the tmpfile function * are removed. * Control is returned to the host environment. The status returned to * the host environment is determined in the same way as for 'exit'. */ extern _ARMABI char *getenv(const char * /*name*/) __attribute__((__nonnull__(1))); /* * searches the environment list, provided by the host environment, for a * string that matches the string pointed to by name. The set of environment * names and the method for altering the environment list are * implementation-defined. * Returns: a pointer to a string associated with the matched list member. * The array pointed to shall not be modified by the program, but * may be overwritten by a subsequent call to the getenv function. * If the specified name cannot be found, a null pointer is * returned. */ extern _ARMABI int system(const char * /*string*/); /* * passes the string pointed to by string to the host environment to be * executed by a command processor in an implementation-defined manner. * A null pointer may be used for string, to inquire whether a command * processor exists. * * Returns: If the argument is a null pointer, the system function returns * non-zero only if a command processor is available. If the * argument is not a null pointer, the system function returns an * implementation-defined value. */ extern _ARMABI_THROW void *bsearch(const void * /*key*/, const void * /*base*/, size_t /*nmemb*/, size_t /*size*/, int (* /*compar*/)(const void *, const void *)) __attribute__((__nonnull__(1,2,5))); /* * searches an array of nmemb objects, the initial member of which is * pointed to by base, for a member that matches the object pointed to by * key. The size of each member of the array is specified by size. * The contents of the array shall be in ascending sorted order according to * a comparison function pointed to by compar, which is called with two * arguments that point to the key object and to an array member, in that * order. The function shall return an integer less than, equal to, or * greater than zero if the key object is considered, respectively, to be * less than, to match, or to be greater than the array member. * Returns: a pointer to a matching member of the array, or a null pointer * if no match is found. If two members compare as equal, which * member is matched is unspecified. */ #if defined(__cplusplus) && defined(__LANGUAGE_LINKAGE_CHANGES_FUNCTION_TYPE) /* bsearch that takes a ptr to a function with C++ linkage * but not in GNU mode */ typedef int (* __C_compareprocptr)(const void *, const void *); extern "C++" void *bsearch(const void * __key, const void * __base, size_t __nmemb, size_t __size, int (* __compar)(const void *, const void *)) __attribute__((__nonnull__(1,2,5))); extern "C++" inline void *bsearch(const void * __key, const void * __base, size_t __nmemb, size_t __size, int (* __compar)(const void *, const void *)) { return bsearch(__key, __base, __nmemb, __size, (__C_compareprocptr)__compar); } #endif extern _ARMABI_THROW void qsort(void * /*base*/, size_t /*nmemb*/, size_t /*size*/, int (* /*compar*/)(const void *, const void *)) __attribute__((__nonnull__(1,4))); /* * sorts an array of nmemb objects, the initial member of which is pointed * to by base. The size of each object is specified by size. * The contents of the array shall be in ascending order according to a * comparison function pointed to by compar, which is called with two * arguments that point to the objects being compared. The function shall * return an integer less than, equal to, or greater than zero if the first * argument is considered to be respectively less than, equal to, or greater * than the second. If two members compare as equal, their order in the * sorted array is unspecified. */ #if defined(__cplusplus) && defined(__LANGUAGE_LINKAGE_CHANGES_FUNCTION_TYPE) /* qsort that takes a ptr to a function with C++ linkage * but not in GNU mode */ extern "C++" void qsort(void * __base, size_t __nmemb, size_t __size, int (* __compar)(const void *, const void *)) __attribute__((__nonnull__(1,4))); extern "C++" inline void qsort(void * __base, size_t __nmemb, size_t __size, int (* __compar)(const void *, const void *)) { qsort(__base, __nmemb, __size, (__C_compareprocptr)__compar); } #endif extern _ARMABI_PURE int abs(int /*j*/); /* * computes the absolute value of an integer j. If the result cannot be * represented, the behaviour is undefined. * Returns: the absolute value. */ extern _ARMABI_PURE div_t div(int /*numer*/, int /*denom*/); /* * computes the quotient and remainder of the division of the numerator * numer by the denominator denom. If the division is inexact, the resulting * quotient is the integer of lesser magnitude that is the nearest to the * algebraic quotient. If the result cannot be represented, the behaviour is * undefined; otherwise, quot * denom + rem shall equal numer. * Returns: a structure of type div_t, comprising both the quotient and the * remainder. the structure shall contain the following members, * in either order. * int quot; int rem; */ extern _ARMABI_PURE long int labs(long int /*j*/); /* * computes the absolute value of an long integer j. If the result cannot be * represented, the behaviour is undefined. * Returns: the absolute value. */ #ifdef __cplusplus extern "C++" inline _ARMABI_PURE long abs(long int x) { return labs(x); } #endif extern _ARMABI_PURE ldiv_t ldiv(long int /*numer*/, long int /*denom*/); /* * computes the quotient and remainder of the division of the numerator * numer by the denominator denom. If the division is inexact, the sign of * the resulting quotient is that of the algebraic quotient, and the * magnitude of the resulting quotient is the largest integer less than the * magnitude of the algebraic quotient. If the result cannot be represented, * the behaviour is undefined; otherwise, quot * denom + rem shall equal * numer. * Returns: a structure of type ldiv_t, comprising both the quotient and the * remainder. the structure shall contain the following members, * in either order. * long int quot; long int rem; */ #ifdef __cplusplus extern "C++" inline _ARMABI_PURE ldiv_t div(long int __numer, long int __denom) { return ldiv(__numer, __denom); } #endif #if !defined(__STRICT_ANSI__) || __USE_C99_STDLIB extern _ARMABI_PURE __LONGLONG llabs(__LONGLONG /*j*/); /* * computes the absolute value of a long long integer j. If the * result cannot be represented, the behaviour is undefined. * Returns: the absolute value. */ #ifdef __cplusplus extern "C++" inline _ARMABI_PURE __LONGLONG abs(__LONGLONG x) { return llabs(x); } #endif extern _ARMABI_PURE lldiv_t lldiv(__LONGLONG /*numer*/, __LONGLONG /*denom*/); /* * computes the quotient and remainder of the division of the numerator * numer by the denominator denom. If the division is inexact, the sign of * the resulting quotient is that of the algebraic quotient, and the * magnitude of the resulting quotient is the largest integer less than the * magnitude of the algebraic quotient. If the result cannot be represented, * the behaviour is undefined; otherwise, quot * denom + rem shall equal * numer. * Returns: a structure of type lldiv_t, comprising both the quotient and the * remainder. the structure shall contain the following members, * in either order. * long long quot; long long rem; */ #ifdef __cplusplus extern "C++" inline _ARMABI_PURE lldiv_t div(__LONGLONG __numer, __LONGLONG __denom) { return lldiv(__numer, __denom); } #endif #endif #if !(__ARM_NO_DEPRECATED_FUNCTIONS) /* * ARM real-time divide functions for guaranteed performance */ typedef struct __sdiv32by16 { int quot, rem; } __sdiv32by16; typedef struct __udiv32by16 { unsigned int quot, rem; } __udiv32by16; /* used int so that values return in separate regs, although 16-bit */ typedef struct __sdiv64by32 { int rem, quot; } __sdiv64by32; __value_in_regs extern _ARMABI_PURE __sdiv32by16 __rt_sdiv32by16( int /*numer*/, short int /*denom*/); /* * Signed divide: (16-bit quot), (16-bit rem) = (32-bit) / (16-bit) */ __value_in_regs extern _ARMABI_PURE __udiv32by16 __rt_udiv32by16( unsigned int /*numer*/, unsigned short /*denom*/); /* * Unsigned divide: (16-bit quot), (16-bit rem) = (32-bit) / (16-bit) */ __value_in_regs extern _ARMABI_PURE __sdiv64by32 __rt_sdiv64by32( int /*numer_h*/, unsigned int /*numer_l*/, int /*denom*/); /* * Signed divide: (32-bit quot), (32-bit rem) = (64-bit) / (32-bit) */ #endif /* * ARM floating-point mask/status function (for both hardfp and softfp) */ extern _ARMABI unsigned int __fp_status(unsigned int /*mask*/, unsigned int /*flags*/); /* * mask and flags are bit-fields which correspond directly to the * floating point status register in the FPE/FPA and fplib. * __fp_status returns the current value of the status register, * and also sets the writable bits of the word * (the exception control and flag bytes) to: * * new = (old & ~mask) ^ flags; */ #define __fpsr_IXE 0x100000 #define __fpsr_UFE 0x80000 #define __fpsr_OFE 0x40000 #define __fpsr_DZE 0x20000 #define __fpsr_IOE 0x10000 #define __fpsr_IXC 0x10 #define __fpsr_UFC 0x8 #define __fpsr_OFC 0x4 #define __fpsr_DZC 0x2 #define __fpsr_IOC 0x1 /* * Multibyte Character Functions. * The behaviour of the multibyte character functions is affected by the * LC_CTYPE category of the current locale. For a state-dependent encoding, * each function is placed into its initial state by a call for which its * character pointer argument, s, is a null pointer. Subsequent calls with s * as other than a null pointer cause the internal state of the function to be * altered as necessary. A call with s as a null pointer causes these functions * to return a nonzero value if encodings have state dependency, and a zero * otherwise. After the LC_CTYPE category is changed, the shift state of these * functions is indeterminate. */ extern _ARMABI int mblen(const char * /*s*/, size_t /*n*/); /* * If s is not a null pointer, the mblen function determines the number of * bytes compromising the multibyte character pointed to by s. Except that * the shift state of the mbtowc function is not affected, it is equivalent * to mbtowc((wchar_t *)0, s, n); * Returns: If s is a null pointer, the mblen function returns a nonzero or * zero value, if multibyte character encodings, respectively, do * or do not have state-dependent encodings. If s is not a null * pointer, the mblen function either returns a 0 (if s points to a * null character), or returns the number of bytes that compromise * the multibyte character (if the next n of fewer bytes form a * valid multibyte character), or returns -1 (they do not form a * valid multibyte character). */ extern _ARMABI int mbtowc(wchar_t * __restrict /*pwc*/, const char * __restrict /*s*/, size_t /*n*/); /* * If s is not a null pointer, the mbtowc function determines the number of * bytes that compromise the multibyte character pointed to by s. It then * determines the code for value of type wchar_t that corresponds to that * multibyte character. (The value of the code corresponding to the null * character is zero). If the multibyte character is valid and pwc is not a * null pointer, the mbtowc function stores the code in the object pointed * to by pwc. At most n bytes of the array pointed to by s will be examined. * Returns: If s is a null pointer, the mbtowc function returns a nonzero or * zero value, if multibyte character encodings, respectively, do * or do not have state-dependent encodings. If s is not a null * pointer, the mbtowc function either returns a 0 (if s points to * a null character), or returns the number of bytes that * compromise the converted multibyte character (if the next n of * fewer bytes form a valid multibyte character), or returns -1 * (they do not form a valid multibyte character). */ extern _ARMABI int wctomb(char * /*s*/, wchar_t /*wchar*/); /* * determines the number of bytes need to represent the multibyte character * corresponding to the code whose value is wchar (including any change in * shift state). It stores the multibyte character representation in the * array object pointed to by s (if s is not a null pointer). At most * MB_CUR_MAX characters are stored. If the value of wchar is zero, the * wctomb function is left in the initial shift state). * Returns: If s is a null pointer, the wctomb function returns a nonzero or * zero value, if multibyte character encodings, respectively, do * or do not have state-dependent encodings. If s is not a null * pointer, the wctomb function returns a -1 if the value of wchar * does not correspond to a valid multibyte character, or returns * the number of bytes that compromise the multibyte character * corresponding to the value of wchar. */ /* * Multibyte String Functions. * The behaviour of the multibyte string functions is affected by the LC_CTYPE * category of the current locale. */ extern _ARMABI size_t mbstowcs(wchar_t * __restrict /*pwcs*/, const char * __restrict /*s*/, size_t /*n*/) __attribute__((__nonnull__(2))); /* * converts a sequence of multibyte character that begins in the initial * shift state from the array pointed to by s into a sequence of * corresponding codes and stores not more than n codes into the array * pointed to by pwcs. No multibyte character that follow a null character * (which is converted into a code with value zero) will be examined or * converted. Each multibyte character is converted as if by a call to * mbtowc function, except that the shift state of the mbtowc function is * not affected. No more than n elements will be modified in the array * pointed to by pwcs. If copying takes place between objects that overlap, * the behaviour is undefined. * Returns: If an invalid multibyte character is encountered, the mbstowcs * function returns (size_t)-1. Otherwise, the mbstowcs function * returns the number of array elements modified, not including * a terminating zero code, if any. */ extern _ARMABI size_t wcstombs(char * __restrict /*s*/, const wchar_t * __restrict /*pwcs*/, size_t /*n*/) __attribute__((__nonnull__(2))); /* * converts a sequence of codes that correspond to multibyte characters * from the array pointed to by pwcs into a sequence of multibyte * characters that begins in the initial shift state and stores these * multibyte characters into the array pointed to by s, stopping if a * multibyte character would exceed the limit of n total bytes or if a * null character is stored. Each code is converted as if by a call to the * wctomb function, except that the shift state of the wctomb function is * not affected. No more than n elements will be modified in the array * pointed to by s. If copying takes place between objects that overlap, * the behaviour is undefined. * Returns: If a code is encountered that does not correspond to a valid * multibyte character, the wcstombs function returns (size_t)-1. * Otherwise, the wcstombs function returns the number of bytes * modified, not including a terminating null character, if any. */ extern _ARMABI void __use_realtime_heap(void); extern _ARMABI void __use_realtime_division(void); extern _ARMABI void __use_two_region_memory(void); extern _ARMABI void __use_no_heap(void); extern _ARMABI void __use_no_heap_region(void); extern _ARMABI char const *__C_library_version_string(void); extern _ARMABI int __C_library_version_number(void); #ifdef __cplusplus } /* extern "C" */ } /* namespace std */ #endif /* __cplusplus */ #endif /* __STDLIB_DECLS */ #if _AEABI_PORTABILITY_LEVEL != 0 && !defined _AEABI_PORTABLE #define _AEABI_PORTABLE #endif #ifdef __cplusplus #ifndef __STDLIB_NO_EXPORTS #if !defined(__STRICT_ANSI__) || __USE_C99_STDLIB using ::std::atoll; using ::std::lldiv_t; #endif /* !defined(__STRICT_ANSI__) || __USE_C99_STDLIB */ using ::std::div_t; using ::std::ldiv_t; using ::std::atof; using ::std::atoi; using ::std::atol; using ::std::strtod; #if !defined(__STRICT_ANSI__) || __USE_C99_STDLIB using ::std::strtof; using ::std::strtold; #endif using ::std::strtol; using ::std::strtoul; using ::std::strtoll; using ::std::strtoull; using ::std::rand; using ::std::srand; using ::std::_rand_state; using ::std::_rand_r; using ::std::_srand_r; using ::std::_ANSI_rand_state; using ::std::_ANSI_rand_r; using ::std::_ANSI_srand_r; using ::std::calloc; using ::std::free; using ::std::malloc; using ::std::realloc; #if !defined(__STRICT_ANSI__) using ::std::posix_memalign; #endif using ::std::__heapprt; using ::std::__heapstats; using ::std::__heapvalid; using ::std::abort; using ::std::atexit; using ::std::exit; using ::std::_Exit; using ::std::getenv; using ::std::system; using ::std::bsearch; using ::std::qsort; using ::std::abs; using ::std::div; using ::std::labs; using ::std::ldiv; #if !defined(__STRICT_ANSI__) || __USE_C99_STDLIB using ::std::llabs; using ::std::lldiv; #endif /* !defined(__STRICT_ANSI__) || __USE_C99_STDLIB */ #if !(__ARM_NO_DEPRECATED_FUNCTIONS) using ::std::__sdiv32by16; using ::std::__udiv32by16; using ::std::__sdiv64by32; using ::std::__rt_sdiv32by16; using ::std::__rt_udiv32by16; using ::std::__rt_sdiv64by32; #endif using ::std::__fp_status; using ::std::mblen; using ::std::mbtowc; using ::std::wctomb; using ::std::mbstowcs; using ::std::wcstombs; using ::std::__use_realtime_heap; using ::std::__use_realtime_division; using ::std::__use_two_region_memory; using ::std::__use_no_heap; using ::std::__use_no_heap_region; using ::std::__C_library_version_string; using ::std::__C_library_version_number; using ::std::size_t; using ::std::__aeabi_MB_CUR_MAX; #endif /* __STDLIB_NO_EXPORTS */ #endif /* __cplusplus */ #undef __LONGLONG #endif /* __stdlib_h */ /* end of stdlib.h */ 这是啥
07-09
详细分析解释一下chromium源码中的下面的函数: class OmniboxViewViews : public OmniboxView, public views::Textfield, #if BUILDFLAG(IS_CHROMEOS) public ash::input_method::InputMethodManager::CandidateWindowObserver, #endif public views::TextfieldController, public ui::CompositorObserver, public TemplateURLServiceObserver { METADATA_HEADER(OmniboxViewViews, views::Textfield) public: // Max width of the gradient mask used to smooth ElideAnimation edges. static const int kSmoothingGradientMaxWidth = 15; OmniboxViewViews(std::unique_ptr<OmniboxClient> client, bool popup_window_mode, LocationBarView* location_bar_view, const gfx::FontList& font_list); OmniboxViewViews(const OmniboxViewViews&) = delete; OmniboxViewViews& operator=(const OmniboxViewViews&) = delete; ~OmniboxViewViews() override; // Initialize, create the underlying views, etc. void Init(); // Exposes the RenderText for tests. #if defined(UNIT_TEST) gfx::RenderText* GetRenderText() { return views::Textfield::GetRenderText(); } #endif // For use when switching tabs, this saves the current state onto the tab so // that it can be restored during a later call to Update(). void SaveStateToTab(content::WebContents* tab); // Called when the window's active tab changes. void OnTabChanged(const content::WebContents* web_contents); // Called to clear the saved state for |web_contents|. void ResetTabState(content::WebContents* web_contents); // Installs the placeholder text with the name of the current default search // provider. For example, if Google is the default search provider, this shows // "Search Google or type a URL" when the Omnibox is empty and unfocused. void InstallPlaceholderText(); // Indicates if the cursor is at the end of the input. Requires that both // ends of the selection reside there. bool GetSelectionAtEnd() const; // Returns the width in pixels needed to display the current text. The // returned value includes margins. int GetTextWidth() const; // Returns the width in pixels needed to display the current text unelided. int GetUnelidedTextWidth() const; // Returns the omnibox's width in pixels. int GetWidth() const; // OmniboxView: void EmphasizeURLComponents() override; void Update() override; std::u16string GetText() const override; using OmniboxView::SetUserText; void SetUserText(const std::u16string& text, bool update_popup) override; void SetWindowTextAndCaretPos(const std::u16string& text, size_t caret_pos, bool update_popup, bool notify_text_changed) override; void SetAdditionalText(const std::u16string& additional_text) override; void EnterKeywordModeForDefaultSearchProvider() override; bool IsSelectAll() const override; void GetSelectionBounds(std::u16string::size_type* start, std::u16string::size_type* end) const override; void SelectAll(bool reversed) override; void RevertAll() override; void SetFocus(bool is_user_initiated) override; bool IsImeComposing() const override; gfx::NativeView GetRelativeWindowForPopup() const override; bool IsImeShowingPopup() const override; // views::Textfield: gfx::Size GetMinimumSize() const override; bool OnMousePressed(const ui::MouseEvent& event) override; bool OnMouseDragged(const ui::MouseEvent& event) override; void OnMouseReleased(const ui::MouseEvent& event) override; void OnPaint(gfx::Canvas* canvas) override; void ExecuteCommand(int command_id, int event_flags) override; void OnInputMethodChanged() override; void AddedToWidget() override; void RemovedFromWidget() override; std::u16string GetLabelForCommandId(int command_id) const override; bool IsCommandIdEnabled(int command_id) const override; // For testing only. OmniboxPopupView* GetPopupViewForTesting() const; protected: // OmniboxView: void UpdateSchemeStyle(const gfx::Range& range) override; // views::Textfield: void OnThemeChanged() override; bool IsDropCursorForInsertion() const override; // Wrappers around Textfield methods that tests can override. virtual void ApplyColor(SkColor color, const gfx::Range& range); virtual void ApplyStyle(gfx::TextStyle style, bool value, const gfx::Range& range); private: friend class TestingOmniboxView; FRIEND_TEST_ALL_PREFIXES(OmniboxPopupViewViewsTest, EmitAccessibilityEvents); // TODO(tommycli): Remove the rest of these friends after porting these // browser tests to unit tests. FRIEND_TEST_ALL_PREFIXES(OmniboxViewViewsTest, CloseOmniboxPopupOnTextDrag); FRIEND_TEST_ALL_PREFIXES(OmniboxViewViewsTest, FriendlyAccessibleLabel); FRIEND_TEST_ALL_PREFIXES(OmniboxViewViewsTest, DoNotNavigateOnDrop); FRIEND_TEST_ALL_PREFIXES(OmniboxViewViewsTest, AyncDropCallback); FRIEND_TEST_ALL_PREFIXES(OmniboxViewViewsTest, AccessibleTextSelectBoundTest); enum class UnelisionGesture { HOME_KEY_PRESSED, MOUSE_RELEASE, OTHER, }; // Update the field with |text| and set the selection. |ranges| should not be // empty; even text with no selections must have at least 1 empty range in // |ranges| to indicate the cursor position. void SetTextAndSelectedRange(const std::u16string& text, const gfx::Range& selection); // Returns the selected text. std::u16string_view GetSelectedText() const; void UpdateAccessibleTextSelection() override; // Paste text from the clipboard into the omnibox. // Textfields implementation of Paste() pastes the contents of the clipboard // as is. We want to strip whitespace and other things (see GetClipboardText() // for details). The function invokes OnBefore/AfterPossibleChange() as // necessary. void OnOmniboxPaste(); // Handle keyword hint tab-to-search and tabbing through dropdown results. bool HandleEarlyTabActions(const ui::KeyEvent& event); void ClearAccessibilityLabel(); void SetAccessibilityLabel(const std::u16string& display_text, const AutocompleteMatch& match, bool notify_text_changed) override; // Returns true if the user text was updated with the full URL (without // steady-state elisions). |gesture| is the user gesture causing unelision. bool UnapplySteadyStateElisions(UnelisionGesture gesture); #if BUILDFLAG(IS_MAC) void AnnounceFriendlySuggestionText(); #endif // Get the preferred text input type, this checks the IME locale on Windows. ui::TextInputType GetPreferredTextInputType() const; // OmniboxView: void SetCaretPos(size_t caret_pos) override; void UpdatePopup() override; void ApplyCaretVisibility() override; void OnTemporaryTextMaybeChanged(const std::u16string& display_text, const AutocompleteMatch& match, bool save_original_selection, bool notify_text_changed) override; void OnInlineAutocompleteTextMaybeChanged( const std::u16string& user_text, const std::u16string& inline_autocompletion) override; void OnInlineAutocompleteTextCleared() override; void OnRevertTemporaryText(const std::u16string& display_text, const AutocompleteMatch& match) override; void OnBeforePossibleChange() override; bool OnAfterPossibleChange(bool allow_keyword_ui_change) override; void OnKeywordPlaceholderTextChange() override; gfx::NativeView GetNativeView() const override; void ShowVirtualKeyboardIfEnabled() override; void HideImeIfNeeded() override; int GetOmniboxTextLength() const override; void SetEmphasis(bool emphasize, const gfx::Range& range) override; // views::View void OnMouseMoved(const ui::MouseEvent& event) override; void OnMouseExited(const ui::MouseEvent& event) override; // views::Textfield: bool IsItemForCommandIdDynamic(int command_id) const override; void OnGestureEvent(ui::GestureEvent* event) override; bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) override; bool HandleAccessibleAction(const ui::AXActionData& action_data) override; void OnFocus() override; void OnBlur() override; std::u16string GetSelectionClipboardText() const override; void DoInsertChar(char16_t ch) override; bool IsTextEditCommandEnabled(ui::TextEditCommand command) const override; void ExecuteTextEditCommand(ui::TextEditCommand command) override; bool ShouldShowPlaceholderText() const override; void UpdateAccessibleValue() override; // ash::input_method::InputMethodManager::CandidateWindowObserver: #if BUILDFLAG(IS_CHROMEOS) void CandidateWindowOpened( ash::input_method::InputMethodManager* manager) override; void CandidateWindowClosed( ash::input_method::InputMethodManager* manager) override; #endif // views::TextfieldController: void ContentsChanged(views::Textfield* sender, const std::u16string& new_contents) override; bool HandleKeyEvent(views::Textfield* sender, const ui::KeyEvent& key_event) override; void OnBeforeUserAction(views::Textfield* sender) override; void OnAfterUserAction(views::Textfield* sender) override; void OnAfterCutOrCopy(ui::ClipboardBuffer clipboard_buffer) override; void OnWriteDragData(ui::OSExchangeData* data) override; void OnGetDragOperationsForTextfield(int* drag_operations) override; void AppendDropFormats( int* formats, std::set<ui::ClipboardFormatType>* format_types) override; ui::mojom::DragOperation OnDrop(const ui::DropTargetEvent& event) override; views::View::DropCallback CreateDropCallback( const ui::DropTargetEvent& event) override; void UpdateContextMenu(ui::SimpleMenuModel* menu_contents) override; // ui::SimpleMenuModel::Delegate: bool IsCommandIdChecked(int id) const override; // ui::CompositorObserver: void OnCompositingDidCommit(ui::Compositor* compositor) override; void OnCompositingStarted(ui::Compositor* compositor, base::TimeTicks start_time) override; void OnDidPresentCompositorFrame( uint32_t frame_token, const gfx::PresentationFeedback& feedback) override; void OnCompositingShuttingDown(ui::Compositor* compositor) override; // TemplateURLServiceObserver: void OnTemplateURLServiceChanged() override; // Permits launch of the external protocol handler after user actions in // the omnibox. The handler needs to be informed that omnibox input should // always be considered "user gesture-triggered", lest it always return BLOCK. void PermitExternalProtocolHandler(); // Drops dragged text and updates `output_drag_op` accordingly. void PerformDrop(const ui::DropTargetEvent& event, ui::mojom::DragOperation& output_drag_op, std::unique_ptr<ui::LayerTreeOwner> drag_image_layer_owner); // Helper method to construct part of the context menu. void MaybeAddSendTabToSelfItem(ui::SimpleMenuModel* menu_contents); // Called when the popup view becomes visible. void OnPopupOpened(); // Helper for updating placeholder color depending on whether its a keyword or // DSE placeholder. void UpdatePlaceholderTextColor(); // When true, the location bar view is read only and also is has a slightly // different presentation (smaller font size). This is used for popups. bool popup_window_mode_; // Owns either an OmniboxPopupViewViews or an OmniboxPopupViewWebUI. std::unique_ptr<OmniboxPopupView> popup_view_; base::CallbackListSubscription popup_view_opened_subscription_; // Selection persisted across temporary text changes, like popup suggestions. gfx::Range saved_temporary_selection_; // Holds the user's selection across focus changes. There is only a saved // selection if this range IsValid(). gfx::Range saved_selection_for_focus_change_; // Tracking state before and after a possible change. State state_before_change_; bool ime_composing_before_change_ = false; // |location_bar_view_| can be NULL in tests. raw_ptr<LocationBarView> location_bar_view_; #if BUILDFLAG(IS_CHROMEOS) // True if the IME candidate window is open. When this is true, we want to // avoid showing the popup. So far, the candidate window is detected only // on Chrome OS. bool ime_candidate_window_open_ = false; #endif // True if any mouse button is currently depressed. bool is_mouse_pressed_ = false; // Applies a minimum threshold to drag events after unelision. Because the // text shifts after unelision, we don't want unintentional mouse drags to // change the selection. bool filter_drag_events_for_unelision_ = false; // Should we select all the text when we see the mouse button get released? // We select in response to a click that focuses the omnibox, but we defer // until release, setting this variable back to false if we saw a drag, to // allow the user to select just a portion of the text. bool select_all_on_mouse_release_ = false; // Indicates if we want to select all text in the omnibox when we get a // GESTURE_TAP. We want to select all only when the textfield is not in focus // and gets a tap. So we use this variable to remember focus state before tap. bool select_all_on_gesture_tap_ = false; // Whether the user should be notified if the clipboard is restricted. bool show_rejection_ui_if_any_ = false; // Keep track of the word that would be selected if URL is unelided between // a single and double click. This is an edge case where the elided URL is // selected. On the double click, unelision is performed in between the first // and second clicks. This results in both the wrong word to be selected and // the wrong selection length. For example, if example.com is shown and you // try to double click on the "x", it unelides to https://example.com after // the first click, resulting in "https" being selected. size_t next_double_click_selection_len_ = 0; size_t next_double_click_selection_offset_ = 0; // The time of the first character insert operation that has not yet been // painted. Used to measure omnibox responsiveness with a histogram. base::TimeTicks insert_char_time_; // The state machine for logging the Omnibox.CharTypedToRepaintLatency // histogram. enum { NOT_ACTIVE, // Not currently tracking a char typed event. CHAR_TYPED, // Character was typed. ON_PAINT_CALLED, // Character was typed and OnPaint() called. COMPOSITING_COMMIT, // Compositing was committed after OnPaint(). COMPOSITING_STARTED, // Compositing was started. } latency_histogram_state_; // The currently selected match, if any, with additional labelling text // such as the document title and the type of search, for example: // "Google https://google.com location from bookmark", or // "cats are liquid search suggestion". std::u16string friendly_suggestion_text_; // The number of added labelling characters before editable text begins. // For example, "Google https://google.com location from history", // this is set to 7 (the length of "Google "). int friendly_suggestion_text_prefix_length_; base::ScopedObservation<ui::Compositor, ui::CompositorObserver> scoped_compositor_observation_{this}; base::ScopedObservation<TemplateURLService, TemplateURLServiceObserver> scoped_template_url_service_observation_{this}; PrefChangeRegistrar pref_change_registrar_; base::WeakPtrFactory<OmniboxViewViews> weak_factory_{this}; };
最新发布
08-02
一、系统分层功能 感知层:用实验箱的两个传感器模块 传感器 1:温湿度传感器,采集温度、湿度数据。 传感器 2:红外对射 ,采集触发状态。 网络层: Zigbee 通信:传感器 1、2 的数据,经 Zigbee 节点传给协调器,协调器通过串口发 PC 端。 数据处理:解析串口数据,分主题发 MQTT 网络,或存数据库 。 应用层:用 Python 做终端,实现界面、串口、数据库交互(可加 MQTT),完成这些功能: 二、具体功能 数据上传: 远程设备实时收传感器 1 数据,格式:学号姓名缩写+温度+湿度 。 远程设备实时收传感器 2 数据,格式:学号姓名缩写+传感器名+interrupt 。 Mysql 数据库:建不同数据表存两个传感器数据,表含 学号姓名缩写、传感器名、数据值、传感器状态 等字段 。 命令下发: 发 学号姓名缩写+Num1Led+on ,传感器 1 连的 Zigbee 模块 LED2 亮;发 ...+off 则灭 。 发 学号姓名缩写+Num2Led+on ,传感器 2 连的 Zigbee 模块 LED2 亮;发 ...+off 则灭 。 数据联动: 传感器 1:温度>25 且湿度>60,其连的 Zigbee 模块 LED2 闪烁,远程设备、数据库表显示 “温湿度异常”;恢复后显示 “异常解除” 。 传感器 2:触发超 5 次,其连的 Zigbee 模块 LED2 闪烁,远程设备、数据库表显示 “传感状态异常”;恢复后显示 “传感异常解除” 。 强制解除:发 学号姓名缩写+Num1Led+relie ,传感器 1 连的 LED2 停闪,设备和表显示 “强制异常解除温湿度” ;发 ...+Num2Led+relie 同理。 数据展示:Python 界面里,串口控制,实时显示传感器 1 的 学号姓名缩写、温度、湿度 数据,呈现底层传感器联动状态 。 #include <stdio.h> #include <string.h> #include "OSAL.h" #include "ZGlobals.h" #include "AF.h" #include "aps_groups.h" #include "ZDApp.h" #include "MT_UART.h" //???? #include "SampleApp.h" #include "SampleAppHw.h" #include "OnBoard.h" /* HAL */ #include "hal_lcd.h" #include "hal_led.h" #include "hal_key.h" #include "hal_uart.h" #include "hal_mcu.h" #include "sht11.h" // P1 interrupt vector value is already defined in ioCC2530.h // #define P1INT_VECTOR 0x7B // Commented out to avoid redefinition warning // Sensor type configuration: // Combined sensor: supports both temp/humidity and IR sensor functions // sht11 command activates temp/humidity sensor, interrupt command activates IR sensor //#define COMBINED_SENSOR // Single sensor configuration (optional): //#define TEMP_HUMIDITY_SENSOR // Temperature/humidity sensor only // Default: IR sensor only (no macro defined) /********************************************************************* * CONSTANTS */ /********************************************************************* * TYPEDEFS */ /********************************************************************* * GLOBAL VARIABLES */ // This list should be filled with Application specific Cluster IDs. const cId_t SampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS] = { SAMPLEAPP_PERIODIC_CLUSTERID, SAMPLEAPP_FLASH_CLUSTERID, SAMPLEAPP_BUZZER_CLUSTERID, SAMPLEAPP_SENSOR_IDENTIFY_CLUSTERID, }; // Add function declarations at the top of the file, after other declarations void SetBuzzerStatus(uint8 status); void SampleApp_ProcessMTMessage(afIncomingMSGPacket_t *msg); void InitIRSensorInterrupt(void); void ProcessIRSensorInterrupt(void); void ProcessLED2Flash(void); // LED2闪烁处理函数 void PrintDeviceInfo(void); // 打印设备信息 void PrintHexBytes(const char* title, const char* data, uint8 len); // 十六进制打印辅助函数 const SimpleDescriptionFormat_t SampleApp_SimpleDesc = { SAMPLEAPP_ENDPOINT, // int Endpoint; SAMPLEAPP_PROFID, // uint16 AppProfId[2]; SAMPLEAPP_DEVICEID, // uint16 AppDeviceId[2]; SAMPLEAPP_DEVICE_VERSION, // int AppDevVer:4; SAMPLEAPP_FLAGS, // int AppFlags:4; SAMPLEAPP_MAX_CLUSTERS, // uint8 AppNumInClusters; (cId_t *)SampleApp_ClusterList, // uint8 *pAppInClusterList; SAMPLEAPP_MAX_CLUSTERS, // uint8 AppNumOutClusters; (cId_t *)SampleApp_ClusterList // uint8 *pAppOutClusterList; }; // This is the Endpoint/Interface description. It is defined here, but // filled-in in SampleApp_Init(). Another way to go would be to fill // in the structure here and make it a "const" (in code space). The // way it's defined in this sample app it is define in RAM. endPointDesc_t SampleApp_epDesc; // Add global variable to control temperature and humidity reading static uint8 g_bReadTempHumi = 0; // Add IR sensor status variable static uint8 g_IRSensorStatus = 0; // Add sensor type variable - configure this based on device type // For combined sensor nodes, set to SENSOR_TYPE_COMBINED // For temperature/humidity sensor nodes, set to SENSOR_TYPE_TEMP_HUMIDITY // For IR sensor nodes, set to SENSOR_TYPE_IR #ifdef COMBINED_SENSOR static uint8 g_SensorType = SENSOR_TYPE_COMBINED; #elif defined(TEMP_HUMIDITY_SENSOR) static uint8 g_SensorType = SENSOR_TYPE_TEMP_HUMIDITY; #else static uint8 g_SensorType = SENSOR_TYPE_IR; #endif // 添加全局变量,用于防止中断处理函数重入 static uint8 g_IRProcessingInProgress = 0; // 添加控制LED2闪烁的全局变量 static uint8 g_LED2FlashStatus = 0; // 0: 停止闪烁, 1: 闪烁中 // 定义命令字符串常量,避免硬编码字符串和在函数调用中重复创建 const char* CMD_NUM1LED_ON = "42213238YFC+Num1Led+on"; const char* CMD_NUM1LED_OFF = "42213238YFC+Num1Led+off"; const char* CMD_NUM2LED_ON = "42213238YFC+Num2Led+on"; const char* CMD_NUM2LED_OFF = "42213238YFC+Num2Led+off"; const char* CMD_ON = "on"; const char* CMD_OFF = "off"; const char* CMD_HT = "ht"; const char* CMD_HF = "hf"; const char* CMD_FLASH = "flash"; // 闪烁命令 const char* CMD_RELIE = "42213238YFC+Num1Led+relie"; // 停止闪烁命令 const char* CMD_SHUO = "shuo"; // 红外对射传感器闪烁命令 const char* CMD_IR_RELIE = "42213238YFC+Num2Led+relie"; // 红外对射传感器停止闪烁命令 /********************************************************************* * EXTERNAL VARIABLES */ /********************************************************************* * EXTERNAL FUNCTIONS */ /********************************************************************* * LOCAL VARIABLES */ uint8 SampleApp_TaskID; // Task ID for internal task/event processing // This variable will be received when // SampleApp_Init() is called. devStates_t SampleApp_NwkState; uint8 SampleApp_TransID; // This is the unique message ID (counter) afAddrType_t SampleApp_Periodic_DstAddr; afAddrType_t SampleApp_Flash_DstAddr; aps_Group_t SampleApp_Group; uint8 SampleAppPeriodicCounter = 0; uint8 SampleAppFlashCounter = 0; /********************************************************************* * LOCAL FUNCTIONS */ void SampleApp_HandleKeys( uint8 shift, uint8 keys ); void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pckt ); void SampleApp_SendPeriodicMessage( void ); void SampleApp_SendFlashMessage( uint16 flashTime ); /********************************************************************* * NETWORK LAYER CALLBACKS */ /********************************************************************* * PUBLIC FUNCTIONS */ /********************************************************************* * @fn SampleApp_Init * * @brief Initialization function for the Generic App Task. * This is called during initialization and should contain * any application specific initialization (ie. hardware * initialization/setup, table initialization, power up * notificaiton ... ). * * @param task_id - the ID assigned by OSAL. This ID should be * used to send messages and set timers. * * @return none */ void SampleApp_Init( uint8 task_id ) { SampleApp_TaskID = task_id; SampleApp_NwkState = DEV_INIT; SampleApp_TransID = 0; // Device hardware initialization can be added here or in main() (Zmain.c). // If the hardware is application specific - add it here. // If the hardware is other parts of the device add it in main(). // Initialize IR sensor interrupt InitIRSensorInterrupt(); #if defined ( BUILD_ALL_DEVICES ) // The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START // We are looking at a jumper (defined in SampleAppHw.c) to be jumpered // together - if they are - we will start up a coordinator. Otherwise, // the device will start as a router. if ( readCoordinatorJumper() ) zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR; else zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER; #endif // BUILD_ALL_DEVICES #if defined ( HOLD_AUTO_START ) // HOLD_AUTO_START is a compile option that will surpress ZDApp // from starting the device and wait for the application to // start the device. ZDOInitDevice(0); #endif // Setup for the periodic message's destination address // Broadcast to everyone SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast; SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF; // Setup for the flash command's destination address - Group 1 SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup; SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP; // Fill out the endpoint description. SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_epDesc.task_id = &SampleApp_TaskID; SampleApp_epDesc.simpleDesc = (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc; SampleApp_epDesc.latencyReq = noLatencyReqs; // Register the endpoint description with the AF afRegister( &SampleApp_epDesc ); // Register for all key events - This app will handle all key events RegisterForKeys( SampleApp_TaskID ); MT_UartRegisterTaskID( SampleApp_TaskID ); //add by 1305106 // By default, all devices start out in Group 1 SampleApp_Group.ID = 0x0001; osal_memcpy( SampleApp_Group.name, "Group 1", 7 ); aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group ); #if defined ( LCD_SUPPORTED ) HalLcdWriteString( "SampleApp", HAL_LCD_LINE_1 ); #endif // 打印设备类型和传感器类型信息 PrintDeviceInfo(); } /********************************************************************* * @fn SampleApp_ProcessEvent * * @brief Generic Application Task event processor. This function * is called to process all events for the task. Events * include timers, messages and any other user defined events. * * @param task_id - The OSAL assigned task ID. * @param events - events to process. This is a bit map and can * contain more than one event. * * @return none */ uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events ) { afIncomingMSGPacket_t *MSGpkt; (void)task_id; // Intentionally unreferenced parameter if ( events & SYS_EVENT_MSG ) { MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); while ( MSGpkt ) { switch ( MSGpkt->hdr.event ) { // Received when a key is pressed case KEY_CHANGE: SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys ); break; // Received when a messages is received (OTA) for this endpoint case AF_INCOMING_MSG_CMD: SampleApp_MessageMSGCB( MSGpkt ); break;; case SPI_INCOMING_ZAPP_DATA: SampleApp_ProcessMTMessage(MSGpkt); MT_UartAppFlowControl (MT_UART_ZAPP_RX_READY); break; // Received whenever the device changes state in the network case ZDO_STATE_CHANGE: SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status); Sht11Init(); if ( (SampleApp_NwkState == DEV_ZB_COORD) || (SampleApp_NwkState == DEV_ROUTER) || (SampleApp_NwkState == DEV_END_DEVICE) ) { // Start sending the periodic message in a regular interval. HalLedSet(HAL_LED_1, HAL_LED_MODE_ON); osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT ); } else { // Device is no longer in the network } break; default: break; } osal_msg_deallocate( (uint8 *)MSGpkt ); MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); } return (events ^ SYS_EVENT_MSG); // return unprocessed events } // Send a message out - This event is generated by a timer // (setup in SampleApp_Init()). if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT ) { SampleApp_SendPeriodicMessage(); // Send the periodic message // Setup to send message again in normal period (+ a little jitter) osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) ); return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT); // return unprocessed events } // Process IR sensor interrupt event if ( events & SAMPLEAPP_SEND_SENSOR_INT_EVT ) { ProcessIRSensorInterrupt(); return (events ^ SAMPLEAPP_SEND_SENSOR_INT_EVT); } // 处理LED2闪烁事件 if ( events & SAMPLEAPP_LED2_FLASH_EVT ) { // 添加调试输出 char buf[100]; sprintf(buf, "处理LED2闪烁事件, g_LED2FlashStatus=%d, g_SensorType=%d\r\n", g_LED2FlashStatus, g_SensorType); HalUARTWrite(0, (uint8*)buf, strlen(buf)); // 检查是否是温湿度传感器或红外对射传感器,且闪烁标志为1 if (g_LED2FlashStatus == 1 && (g_SensorType == SENSOR_TYPE_TEMP_HUMIDITY || g_SensorType == SENSOR_TYPE_IR)) { // 执行闪烁处理函数 ProcessLED2Flash(); // 不需要在这里设置下一次闪烁计时器,已在ProcessLED2Flash函数中处理 } else { // g_LED2FlashStatus不为1时,确保LED2关闭 if (g_LED2FlashStatus == 0) { HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); HalUARTWrite(0, (uint8*)"闪烁标志为0,停止闪烁并关闭LED2\r\n", 32); } } return (events ^ SAMPLEAPP_LED2_FLASH_EVT); } return 0; // Discard unknown events } /********************************************************************* * Event Generation Functions */ /********************************************************************* * @fn SampleApp_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * HAL_KEY_SW_2 * HAL_KEY_SW_1 * * @return none */ void SampleApp_HandleKeys( uint8 shift, uint8 keys ) { (void)shift; // Intentionally unreferenced parameter if ( keys & HAL_KEY_SW_6 ) { /* This key sends the Flash Command is sent to Group 1. * This device will not receive the Flash Command from this * device (even if it belongs to group 1). */ SampleApp_SendFlashMessage( SAMPLEAPP_FLASH_DURATION ); } if ( keys & HAL_KEY_SW_2 ) { /* The Flashr Command is sent to Group 1. * This key toggles this device in and out of group 1. * If this device doesn't belong to group 1, this application * will not receive the Flash command sent to group 1. */ aps_Group_t *grp; grp = aps_FindGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP ); if ( grp ) { // Remove from the group aps_RemoveGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP ); } else { // Add to the flash group aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group ); } } } /********************************************************************* * LOCAL FUNCTIONS */ /********************************************************************* * @fn SampleApp_MessageMSGCB * * @brief Data message processor callback. This function processes * any incoming data - probably from other devices. So, based * on cluster ID, perform the intended action. * * @param none * * @return none */ void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ) { uint16 flashTime; uint8 *buf; switch(pkt->clusterId) { case SAMPLEAPP_PERIODIC_CLUSTERID: buf = pkt->cmd.Data; if(zgDeviceLogicalType == ZG_DEVICETYPE_COORDINATOR) { // Coordinator received data from end device if(pkt->cmd.DataLength == 4 && buf[0] <= 100) { // Format and print temperature data char tempStr[50]; sprintf(tempStr, "42213238YFC+温湿度传感器+温度:%d.%d C+湿度:%d.%d%%\r\n", buf[0], buf[1], buf[2], buf[3]); HalUARTWrite(0, (uint8*)tempStr, strlen(tempStr)); } else if(pkt->cmd.DataLength == 2 && buf[0] == 0x02) { // Process IR sensor data char irStr[50]; if(buf[1] == 0x01) { sprintf(irStr, "IR Sensor Node: Obstacle Detected!\r\n"); } else { sprintf(irStr, "IR Sensor Node: No Obstacle\r\n"); } HalUARTWrite(0, (uint8*)irStr, strlen(irStr)); } else if(pkt->cmd.DataLength == 3 && buf[0] == 0x03) { // Process interrupt message from end device if(buf[1] == 0x01) { // Coordinator prints the message and also blinks LED2 HalUARTWrite(0, (uint8*)"42213238YFC+红外对射传感器+interrupt\r\n", 36); // Flash LED2 10 times on coordinator as well HalLedBlink(HAL_LED_2, 10, 50, 200); } } else if(pkt->cmd.DataLength == 2 && buf[0] == 0x04) { // Process sensor identification response uint8 sensorType = buf[1]; char coordMsg[80]; uint16 srcAddr = pkt->srcAddr.addr.shortAddr; if(sensorType == SENSOR_TYPE_TEMP_HUMIDITY) { sprintf(coordMsg, "Device 0x%04X: Temperature/Humidity sensor identified and LED2 activated\r\n", srcAddr); HalUARTWrite(0, (uint8*)coordMsg, strlen(coordMsg)); } else if(sensorType == SENSOR_TYPE_IR) { sprintf(coordMsg, "Device 0x%04X: IR sensor identified and LED2 activated\r\n", srcAddr); HalUARTWrite(0, (uint8*)coordMsg, strlen(coordMsg)); } } } else { // End device received command from coordinator if(pkt->cmd.DataLength == 1) { if(buf[0] == 0x01) { // Start temperature reading g_bReadTempHumi = 1; osal_start_timerEx(SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, 1000); } else { // Stop temperature reading g_bReadTempHumi = 0; osal_stop_timerEx(SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT); } } else if(pkt->cmd.DataLength == 2 && buf[0] == 0x10) { // 处理协调器转发的闪烁命令,仅终端设备响应 if(zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR && g_SensorType == SENSOR_TYPE_TEMP_HUMIDITY) { if(buf[1] == 0x01) { // 启动LED2闪烁 g_LED2FlashStatus = 1; osal_set_event(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"接收到闪烁命令,启动LED2闪烁\r\n", 30); } else { // 停止LED2闪烁 g_LED2FlashStatus = 0; HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); // 明确停止闪烁定时器 osal_stop_timerEx(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"接收到停止命令,停止LED2闪烁\r\n", 30); } } } // 处理协调器转发的红外对射传感器闪烁命令 else if(pkt->cmd.DataLength == 2 && buf[0] == 0x11) { // 处理协调器转发的红外对射传感器闪烁命令,仅终端设备响应 if(zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR && g_SensorType == SENSOR_TYPE_IR) { if(buf[1] == 0x01) { // 启动红外对射传感器LED2闪烁 g_LED2FlashStatus = 1; osal_set_event(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"接收到红外对射传感器闪烁命令,启动LED2闪烁\r\n", 42); } else { // 停止红外对射传感器LED2闪烁 g_LED2FlashStatus = 0; HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); // 明确停止闪烁定时器 osal_stop_timerEx(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"接收到红外对射传感器停止命令,停止LED2闪烁\r\n", 42); } } } } break; case SAMPLEAPP_FLASH_CLUSTERID: flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2]); HalLedBlink(HAL_LED_4, 4, 50, (flashTime / 4)); break; case SAMPLEAPP_BUZZER_CLUSTERID: SetBuzzerStatus(pkt->cmd.Data[0]); break; case SAMPLEAPP_SENSOR_IDENTIFY_CLUSTERID: // Handle sensor identification requests if(zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { // This is an end device, check if we should respond uint8 cmd = pkt->cmd.Data[0]; if(cmd == SENSOR_IDENTIFY_CMD_SHT11 && (g_SensorType == SENSOR_TYPE_TEMP_HUMIDITY || g_SensorType == SENSOR_TYPE_COMBINED)) { // This is a temperature/humidity sensor or combined sensor responding to sht11 command // Turn on LED2 and send identification response HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); // Send identification response to coordinator uint8 response[2]; response[0] = 0x04; // Sensor identification response // For combined sensor responding to sht11, report as temp/humidity sensor response[1] = SENSOR_TYPE_TEMP_HUMIDITY; AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 2, response, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(cmd == SENSOR_IDENTIFY_CMD_INTERRUPT && (g_SensorType == SENSOR_TYPE_IR || g_SensorType == SENSOR_TYPE_COMBINED)) { // This is an IR sensor or combined sensor responding to interrupt command // Turn on LED2 and send identification response HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); // Send identification response to coordinator uint8 response[2]; response[0] = 0x04; // Sensor identification response // For combined sensor responding to interrupt, report as IR sensor response[1] = SENSOR_TYPE_IR; AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 2, response, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(cmd == 0x05 && (g_SensorType == SENSOR_TYPE_TEMP_HUMIDITY || g_SensorType == SENSOR_TYPE_COMBINED)) { // Turn off LED2 for temperature/humidity sensors HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); } else if(cmd == 0x06 && (g_SensorType == SENSOR_TYPE_IR || g_SensorType == SENSOR_TYPE_COMBINED)) { // Turn off LED2 for IR sensors HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); } } break; } } /********************************************************************* * @fn SampleApp_SendPeriodicMessage * * @brief Send the periodic message. * * @param none * * @return none */ void SampleApp_SendPeriodicMessage(void) { if(g_bReadTempHumi) { uint8 temp_int, temp_dec, humi_int, humi_dec; char buf[50]; float humi, temp; // Initialize SHT11 sensor Sht11Init(); if(GetHumiAndTemp(&humi, &temp) == 0) { // Convert float to integer parts temp_int = (uint8)temp; temp_dec = (uint8)((temp - temp_int) * 10); humi_int = (uint8)humi; humi_dec = (uint8)((humi - humi_int) * 10); // Format the message sprintf(buf, "42213238YFC+温湿度传感器+温度:%d.%d C+湿度:%d.%d%%\r\n", temp_int, temp_dec, humi_int, humi_dec); // Send to UART HalUARTWrite(0, (uint8*)buf, strlen(buf)); // Prepare network data packet buf[0] = temp_int; buf[1] = temp_dec; buf[2] = humi_int; buf[3] = humi_dec; // Send data to network and check result afStatus_t status = AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 4, // Only send 4 bytes of data (uint8*)buf, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); // 只记录错误但不中断流程,保持原有功能不变 if (status != afStatus_SUCCESS) { // 可以在这里添加错误处理代码,如重试或记录日志 // 但不要中断原有功能流程 } } // Restart timer for next reading osal_start_timerEx(SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, 2000); // Read every 2 seconds } } /********************************************************************* * @fn SampleApp_SendFlashMessage * * @brief Send the flash message to group 1. * * @param flashTime - in milliseconds * * @return none */ void SampleApp_SendFlashMessage( uint16 flashTime ){ uint8 buffer[3]; buffer[0] = (uint8)(SampleAppFlashCounter++); buffer[1] = LO_UINT16( flashTime ); buffer[2] = HI_UINT16( flashTime ); if ( AF_DataRequest( &SampleApp_Flash_DstAddr, &SampleApp_epDesc, SAMPLEAPP_FLASH_CLUSTERID, 3, buffer, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ) { } else { // Error occurred in request to send. } } void SampleApp_ProcessMTMessage(afIncomingMSGPacket_t *msg) { // 增加安全检查,确保msg指针有效 if(msg == NULL) { return; } const char *msgPtr = ((const char *)msg+2); uint8 status; // 调试输出,打印收到的命令 HalUARTWrite(0, (uint8*)"收到命令: ", 10); HalUARTWrite(0, (uint8*)msgPtr, strlen(msgPtr)); HalUARTWrite(0, (uint8*)"\r\n", 2); // 打印命令的十六进制值,帮助调试 PrintHexBytes("命令十六进制", msgPtr, strlen(msgPtr)); // 打印预期命令常量进行比较 char buf[100]; sprintf(buf, "预期relie命令: %s, 长度: %d\r\n", CMD_RELIE, strlen(CMD_RELIE)); HalUARTWrite(0, (uint8*)buf, strlen(buf)); // 打印预期命令的十六进制值 PrintHexBytes("预期十六进制", CMD_RELIE, strlen(CMD_RELIE)); // 使用更安全的字符串比较方式 if(strncmp(msgPtr, CMD_NUM1LED_OFF, strlen(CMD_NUM1LED_OFF)) == 0) { // Send command to turn off LED2 for temperature/humidity sensors uint8 cmd[2]; cmd[0] = 0x05; // LED control command cmd[1] = 0x00; // Turn off LED2 for temp/humidity sensors AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_SENSOR_IDENTIFY_CLUSTERID, 2, cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(strncmp(msgPtr, CMD_NUM2LED_OFF, strlen(CMD_NUM2LED_OFF)) == 0) { // Send command to turn off LED2 for IR sensors uint8 cmd[2]; cmd[0] = 0x06; // LED control command cmd[1] = 0x00; // Turn off LED2 for IR sensors AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_SENSOR_IDENTIFY_CLUSTERID, 2, cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(strncmp(msgPtr, CMD_ON, strlen(CMD_ON)) == 0) { status = 0x01; AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_BUZZER_CLUSTERID, 1, &status, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(strncmp(msgPtr, CMD_OFF, strlen(CMD_OFF)) == 0) { status = 0x00; AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_BUZZER_CLUSTERID, 1, &status, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(strncmp(msgPtr, CMD_HT, strlen(CMD_HT)) == 0) { // Forward the command to end device uint8 cmd = 0x01; // Command for starting temperature reading AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 1, &cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(strncmp(msgPtr, CMD_HF, strlen(CMD_HF)) == 0) { // Forward the command to end device uint8 cmd = 0x00; // Command for stopping temperature reading AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 1, &cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(strncmp(msgPtr, CMD_NUM1LED_ON, strlen(CMD_NUM1LED_ON)) == 0) { // Send sensor identification request for temperature/humidity sensors uint8 cmd = SENSOR_IDENTIFY_CMD_SHT11; AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_SENSOR_IDENTIFY_CLUSTERID, 1, &cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(strncmp(msgPtr, CMD_NUM2LED_ON, strlen(CMD_NUM2LED_ON)) == 0) { // Send sensor identification request for IR sensors uint8 cmd = SENSOR_IDENTIFY_CMD_INTERRUPT; AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_SENSOR_IDENTIFY_CLUSTERID, 1, &cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(strncmp(msgPtr, CMD_FLASH, strlen(CMD_FLASH)) == 0) { // 添加调试输出 HalUARTWrite(0, (uint8*)"收到flash命令\r\n", 14); // 只有温湿度传感器模块响应flash命令 if (g_SensorType == SENSOR_TYPE_TEMP_HUMIDITY) { // 添加调试输出 HalUARTWrite(0, (uint8*)"当前是温湿度传感器模块\r\n", 25); if (zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { // 只有终端设备执行闪烁,协调器不闪烁 // 启动LED2闪烁 g_LED2FlashStatus = 1; // 触发闪烁事件,立即开始 osal_set_event(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"终端设备开始LED2闪烁\r\n", 24); } else { // 协调器接收到flash命令时,转发给所有设备 uint8 cmd[2]; cmd[0] = 0x10; // 自定义的flash命令标识 cmd[1] = 0x01; // 开始闪烁 // 添加调试输出 HalUARTWrite(0, (uint8*)"协调器转发flash命令到网络\r\n", 27); AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 2, cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } } else { // 添加调试输出 HalUARTWrite(0, (uint8*)"不是温湿度传感器模块,忽略命令\r\n", 32); } } else if(strncmp(msgPtr, CMD_RELIE, strlen(CMD_RELIE)) == 0) { // 添加调试输出 HalUARTWrite(0, (uint8*)"匹配到relie命令\r\n", 17); // 只有温湿度传感器模块响应relie命令 if (g_SensorType == SENSOR_TYPE_TEMP_HUMIDITY) { // 添加调试输出 HalUARTWrite(0, (uint8*)"当前是温湿度传感器模块\r\n", 25); if (zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { // 只有终端设备执行停止闪烁,协调器不处理 // 停止LED2闪烁 g_LED2FlashStatus = 0; // 关闭LED2 HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); // 明确停止闪烁定时器 osal_stop_timerEx(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"终端设备停止LED2闪烁\r\n", 24); } else { // 协调器接收到relie命令时,转发给所有设备 uint8 cmd[2]; cmd[0] = 0x10; // 自定义的flash命令标识 cmd[1] = 0x00; // 停止闪烁 // 添加调试输出 HalUARTWrite(0, (uint8*)"协调器转发停止闪烁命令到网络\r\n", 31); AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 2, cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } } else { // 添加调试输出 HalUARTWrite(0, (uint8*)"不是温湿度传感器模块,忽略命令\r\n", 32); } } // 添加对红外对射传感器停止闪烁命令的处理 else if(strncmp(msgPtr, CMD_IR_RELIE, strlen(CMD_IR_RELIE)) == 0) { // 添加调试输出 HalUARTWrite(0, (uint8*)"匹配到红外对射停止闪烁命令\r\n", 29); // 只有红外对射传感器模块响应停止闪烁命令 if (g_SensorType == SENSOR_TYPE_IR) { // 添加调试输出 HalUARTWrite(0, (uint8*)"当前是红外对射传感器模块\r\n", 27); if (zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { // 只有终端设备执行停止闪烁,协调器不处理 // 停止LED2闪烁 g_LED2FlashStatus = 0; // 关闭LED2 HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); // 明确停止闪烁定时器 osal_stop_timerEx(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"红外对射终端设备停止LED2闪烁\r\n", 31); } else { // 协调器接收到停止闪烁命令时,转发给所有设备 uint8 cmd[2]; cmd[0] = 0x11; // 自定义的红外闪烁命令标识 cmd[1] = 0x00; // 停止闪烁 // 添加调试输出 HalUARTWrite(0, (uint8*)"协调器转发红外对射停止闪烁命令到网络\r\n", 38); AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 2, cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } } else { // 添加调试输出 HalUARTWrite(0, (uint8*)"不是红外对射传感器模块,忽略命令\r\n", 34); } } // 尝试直接匹配红外对射传感器停止闪烁命令 else if(strcmp(msgPtr, "42213238YFC+Num2Led+relie") == 0) { // 添加调试输出 HalUARTWrite(0, (uint8*)"直接匹配到红外对射停止闪烁命令\r\n", 33); // 只有红外对射传感器模块响应停止闪烁命令 if (g_SensorType == SENSOR_TYPE_IR) { if (zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { // 只有终端设备执行停止闪烁,协调器不处理 // 停止LED2闪烁 g_LED2FlashStatus = 0; // 关闭LED2 HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); // 明确停止闪烁定时器 osal_stop_timerEx(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"红外对射终端设备直接停止LED2闪烁\r\n", 35); } else { // 协调器接收到停止闪烁命令时,转发给所有设备 uint8 cmd[2]; cmd[0] = 0x11; // 自定义的红外闪烁命令标识 cmd[1] = 0x00; // 停止闪烁 // 添加调试输出 HalUARTWrite(0, (uint8*)"协调器直接转发红外对射停止闪烁命令到网络\r\n", 42); AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 2, cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } } } // 尝试另一种方式匹配温湿度传感器的relie命令(恢复原有逻辑) else if(strcmp(msgPtr, "42213238YFC+Num1Led+relie") == 0) { // 添加调试输出 HalUARTWrite(0, (uint8*)"直接匹配到温湿度传感器relie命令\r\n", 33); // 只有温湿度传感器模块响应relie命令 if (g_SensorType == SENSOR_TYPE_TEMP_HUMIDITY) { if (zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { // 只有终端设备执行停止闪烁,协调器不处理 // 停止LED2闪烁 g_LED2FlashStatus = 0; // 关闭LED2 HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); // 明确停止闪烁定时器 osal_stop_timerEx(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"温湿度传感器终端设备直接停止LED2闪烁\r\n", 39); } else { // 协调器接收到relie命令时,转发给所有设备 uint8 cmd[2]; cmd[0] = 0x10; // 自定义的flash命令标识 cmd[1] = 0x00; // 停止闪烁 // 添加调试输出 HalUARTWrite(0, (uint8*)"协调器直接转发温湿度传感器停止闪烁命令到网络\r\n", 46); AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 2, cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } } } // 添加对shuo命令的处理 - 红外对射传感器LED闪烁命令 else if(strncmp(msgPtr, CMD_SHUO, strlen(CMD_SHUO)) == 0) { // 添加调试输出 HalUARTWrite(0, (uint8*)"收到shuo命令\r\n", 14); // 只有红外对射传感器模块响应shuo命令 if (g_SensorType == SENSOR_TYPE_IR) { // 添加调试输出 HalUARTWrite(0, (uint8*)"当前是红外对射传感器模块\r\n", 27); if (zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { // 只有终端设备执行闪烁,协调器不闪烁 // 启动LED2闪烁 g_LED2FlashStatus = 1; // 触发闪烁事件,立即开始 osal_set_event(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"红外对射终端设备开始LED2闪烁\r\n", 30); } else { // 协调器接收到shuo命令时,转发给所有设备 uint8 cmd[2]; cmd[0] = 0x11; // 自定义的红外闪烁命令标识,与温湿度传感器区分 cmd[1] = 0x01; // 开始闪烁 // 添加调试输出 HalUARTWrite(0, (uint8*)"协调器转发shuo命令到网络\r\n", 27); AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 2, cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } } else { // 添加调试输出 HalUARTWrite(0, (uint8*)"不是红外对射传感器模块,忽略命令\r\n", 34); } } } // Initialize IR sensor interrupt function void InitIRSensorInterrupt(void) { // Set P1_2 as input pin P1DIR &= ~BV(2); // Configure P1_2 as input P1SEL &= ~BV(2); // Set as general I/O, not peripheral function // Configure P1_2 as pull-up input P1INP &= ~BV(2); // Set as tri-state input P2INP &= ~BV(5); // Clear P1 port pull-down setting, configure as pull-up // Read current status g_IRSensorStatus = (P1 & BV(2)) ? 0 : 1; // Set P1_2 as falling edge triggered PICTL |= BV(1); // P1 port uses falling edge trigger (PICTL.P1ICON = 1) // Enable P1_2 interrupt P1IEN |= BV(2); // Enable P1_2 interrupt function // Clear possible interrupt flags P1IFG &= ~BV(2); // Enable P1 port interrupt IEN2 |= BV(4); // Enable P1 port interrupt (IEN2.P1IE = 1) } // Process IR sensor interrupt function void ProcessIRSensorInterrupt(void) { // 简单的重入保护:如果已经在处理中,则退出 if(g_IRProcessingInProgress) { return; } g_IRProcessingInProgress = 1; // 设置处理中标志 // Read current status uint8 currentStatus = (P1 & BV(2)) ? 0 : 1; // For end devices, flash LED2 10 times and send interrupt message to coordinator if (zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { // Flash LED2 10 times on end device only HalUARTWrite(0, (uint8*)"42213238YFC+红外对射传感器+interrupt\r\n", 36); HalLedBlink(HAL_LED_2, 10, 50, 200); uint8 buf[3]; buf[0] = 0x03; // Indicates this is an interrupt event message buf[1] = 0x01; // Interrupt occurred buf[2] = currentStatus; // Current sensor status AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 3, (uint8*)buf, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } // Status changed if (currentStatus != g_IRSensorStatus) { g_IRSensorStatus = currentStatus; // Send status to coordinator if (zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { uint8 buf[2]; buf[0] = 0x02; // Indicates this is IR sensor data buf[1] = g_IRSensorStatus; AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 2, (uint8*)buf, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } } // Re-enable interrupt after processing // Clear interrupt flag P1IFG &= ~BV(2); // Re-enable interrupt P1IEN |= BV(2); g_IRProcessingInProgress = 0; // 清除处理中标志 } // LED2闪烁处理函数 void ProcessLED2Flash(void) { // 添加调试输出 HalUARTWrite(0, (uint8*)"执行LED2闪烁处理函数\r\n", 22); // 判断是温湿度传感器还是红外对射传感器 if (zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { // 温湿度传感器模块执行闪烁 if (g_SensorType == SENSOR_TYPE_TEMP_HUMIDITY && g_LED2FlashStatus == 1) { // 添加调试输出 HalUARTWrite(0, (uint8*)"温湿度传感器符合条件,切换LED2状态\r\n", 35); // 切换LED2状态 (开/关) if ((HalLedGetState() & HAL_LED_2) == 0) { HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); HalUARTWrite(0, (uint8*)"温湿度传感器LED2打开\r\n", 22); } else { HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); HalUARTWrite(0, (uint8*)"温湿度传感器LED2关闭\r\n", 22); } // 继续闪烁,500ms后再次触发 osal_start_timerEx(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT, 500); } // 红外对射传感器模块执行闪烁 else if (g_SensorType == SENSOR_TYPE_IR && g_LED2FlashStatus == 1) { // 添加调试输出 HalUARTWrite(0, (uint8*)"红外对射传感器符合条件,切换LED2状态\r\n", 37); // 切换LED2状态 (开/关) if ((HalLedGetState() & HAL_LED_2) == 0) { HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); HalUARTWrite(0, (uint8*)"红外对射传感器LED2打开\r\n", 24); } else { HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); HalUARTWrite(0, (uint8*)"红外对射传感器LED2关闭\r\n", 24); } // 继续闪烁,300ms后再次触发(红外传感器闪烁频率比温湿度传感器快一些) osal_start_timerEx(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT, 300); } else { // LED2闪烁状态为0或传感器类型不匹配,不执行闪烁 if (g_LED2FlashStatus == 0) { HalUARTWrite(0, (uint8*)"闪烁状态为0,不执行LED2闪烁\r\n", 29); // 确保LED2处于关闭状态 HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); } else { // 传感器类型不匹配 HalUARTWrite(0, (uint8*)"传感器类型不匹配,不执行LED2闪烁\r\n", 33); } } } else { // 协调器不执行闪烁 HalUARTWrite(0, (uint8*)"协调器不执行LED2闪烁\r\n", 22); } } // Modify P1 interrupt handler related code, add P1 interrupt service routine HAL_ISR_FUNCTION(halP1Isr, P1INT_VECTOR) { if (P1IFG & BV(2)) { // Clear interrupt flag P1IFG &= ~BV(2); // Notify application to process interrupt via OSAL event osal_set_event(SampleApp_TaskID, SAMPLEAPP_SEND_SENSOR_INT_EVT); } // Clear P1 port interrupt flag P1IF = 0; } // Modify SetBuzzerStatus function to use another pin (P1_0) as buzzer output void SetBuzzerStatus(uint8 status) { if(status == 0x01) { P1_0 = 1; // Use P1_0 as buzzer control pin (active high) } else { P1_0 = 0; // Turn off buzzer } } void PrintDeviceInfo(void) { char buf[80]; // 打印设备类型信息 if(zgDeviceLogicalType == ZG_DEVICETYPE_COORDINATOR) { sprintf(buf, "设备类型: 协调器 (ZG_DEVICETYPE_COORDINATOR)\r\n"); } else if(zgDeviceLogicalType == ZG_DEVICETYPE_ROUTER) { sprintf(buf, "设备类型: 路由器 (ZG_DEVICETYPE_ROUTER)\r\n"); } else if(zgDeviceLogicalType == ZG_DEVICETYPE_ENDDEVICE) { sprintf(buf, "设备类型: 终端设备 (ZG_DEVICETYPE_ENDDEVICE)\r\n"); } else { sprintf(buf, "设备类型: 未知 (%d)\r\n", zgDeviceLogicalType); } HalUARTWrite(0, (uint8*)buf, strlen(buf)); // 打印传感器类型信息 if(g_SensorType == SENSOR_TYPE_TEMP_HUMIDITY) { sprintf(buf, "传感器类型: 温湿度传感器 (SENSOR_TYPE_TEMP_HUMIDITY)\r\n"); } else if(g_SensorType == SENSOR_TYPE_IR) { sprintf(buf, "传感器类型: 红外传感器 (SENSOR_TYPE_IR)\r\n"); } else if(g_SensorType == SENSOR_TYPE_COMBINED) { sprintf(buf, "传感器类型: 组合传感器 (SENSOR_TYPE_COMBINED)\r\n"); } else { sprintf(buf, "传感器类型: 未知 (%d)\r\n", g_SensorType); } HalUARTWrite(0, (uint8*)buf, strlen(buf)); // 打印编译时定义的宏 #ifdef TEMP_HUMIDITY_SENSOR sprintf(buf, "宏定义: TEMP_HUMIDITY_SENSOR 已定义\r\n"); #else sprintf(buf, "宏定义: TEMP_HUMIDITY_SENSOR 未定义\r\n"); #endif HalUARTWrite(0, (uint8*)buf, strlen(buf)); #ifdef COMBINED_SENSOR sprintf(buf, "宏定义: COMBINED_SENSOR 已定义\r\n"); #else sprintf(buf, "宏定义: COMBINED_SENSOR 未定义\r\n"); #endif HalUARTWrite(0, (uint8*)buf, strlen(buf)); } // 辅助函数,将字符串以十六进制格式打印出来,帮助调试 void PrintHexBytes(const char* title, const char* data, uint8 len) { char buf[200]; uint8 offset = 0; uint8 i; // 打印标题 offset = sprintf(buf, "%s: ", title); // 打印十六进制值 for (i = 0; i < len && offset < 190; i++) { offset += sprintf(buf + offset, "%02X ", (uint8)data[i]); } sprintf(buf + offset, "\r\n"); HalUARTWrite(0, (uint8*)buf, strlen(buf)); } /********************************************************************* *********************************************************************/ 根据要求和代码,实现相关功能,给出修改后的完整代码
06-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值