diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..baae1548 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +################################################################################ +# 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。 +################################################################################ + +/vs/Debug +*.lib +*.db +*.opendb +*.suo diff --git a/App/Alarm.cpp b/App/Alarm.cpp index 0578a5bc..f5427d1e 100644 --- a/App/Alarm.cpp +++ b/App/Alarm.cpp @@ -50,9 +50,9 @@ bool Alarm::Set(const Pair& args, Stream& result) result.Write(id); // 马上调度一次 - if(id) Sys.SetTask(_taskid, true, 0); + if (id) Sys.SetTask(_taskid, true, 0); - return id; + return id > 0; } bool Alarm::Get(const Pair& args, Stream& result) @@ -145,12 +145,12 @@ static int CheckTime(const AlarmItem& item) } // 判断时间有效性 - dt.Hour = item.Hour; - dt.Minute = item.Minutes; - dt.Second = item.Seconds; + dt.Hour = item.Hour; + dt.Minute = item.Minutes; + dt.Second = item.Seconds; // 需要特别小心时间的偏差 - return (dt - now).TotalSeconds(); + return (dt - now).TotalSeconds(); } void Alarm::AlarmTask() @@ -185,7 +185,7 @@ void Alarm::AlarmTask() if (flag) cfg.Save(); if (next > 0) - Sys.SetTask(_taskid, true, next * 1000); + Sys.SetTask(_taskid, true, next * 1000); } void Alarm::Start() diff --git a/Core/Array.h b/Core/Array.h index 7d0c19de..a07e411a 100644 --- a/Core/Array.h +++ b/Core/Array.h @@ -59,7 +59,7 @@ public: #endif protected: - uint _Capacity; // 最大个数。非字节数 + int _Capacity; // 最大个数。非字节数 bool _needFree; // 是否需要释放 bool _canWrite; // 是否可写 ushort _Size; // 单个元素大小。字节 diff --git a/Core/List.cpp b/Core/List.cpp index c4568c37..958cd1e2 100644 --- a/Core/List.cpp +++ b/Core/List.cpp @@ -91,7 +91,7 @@ void IList::Add(void* item) } // 添加多个元素 -void IList::Add(void** items, uint count) +void IList::Add(void** items, int count) { if(!items || !count) return; @@ -101,7 +101,7 @@ void IList::Add(void** items, uint count) } // 删除指定位置元素 -void IList::RemoveAt(uint index) +void IList::RemoveAt(int index) { int len = _Count; if(len <= 0 || index >= len) return; diff --git a/Core/List.h b/Core/List.h index d49ac0fc..363ba2ba 100644 --- a/Core/List.h +++ b/Core/List.h @@ -23,10 +23,10 @@ public: void Add(void* item); // 添加多个元素 - void Add(void** items, uint count); + void Add(void** items, int count); // 删除指定位置元素 - void RemoveAt(uint index); + void RemoveAt(int index); // 删除指定元素 int Remove(const void* item); @@ -49,8 +49,8 @@ public: private: void** _Arr; - uint _Count; - uint _Capacity; + int _Count; + int _Capacity; void* Arr[0x04]; diff --git a/Core/Queue.cpp b/Core/Queue.cpp index 991751f7..aab8d479 100644 --- a/Core/Queue.cpp +++ b/Core/Queue.cpp @@ -9,7 +9,7 @@ Queue::Queue() : _s((void*)nullptr, 0) Clear(); } -void Queue::SetCapacity(uint len) +void Queue::SetCapacity(int len) { _s.SetLength(len); } @@ -63,7 +63,7 @@ INROOT byte Queue::Dequeue() return dat; } -uint Queue::Write(const Buffer& bs) +int Queue::Write(const Buffer& bs) { int total = _s.Capacity(); if(!total) _s.SetLength(64); @@ -77,17 +77,17 @@ uint Queue::Write(const Buffer& bs) 4,如果队列过小,很有可能后来数据会覆盖前面数据 */ - uint len = bs.Length(); + int len = bs.Length(); // 如果队列满了,不需要覆盖 if(_size + len > total) len = total - _size; - uint rs = 0; + int rs = 0; while(true) { // 计算这一个循环剩下的位置 - uint remain = _s.Capacity() - _head; + int remain = _s.Capacity() - _head; // 如果要写入的数据足够存放 if(len <= remain) { @@ -113,7 +113,7 @@ uint Queue::Write(const Buffer& bs) return rs; } -uint Queue::Read(Buffer& bs) +int Queue::Read(Buffer& bs) { if(_size == 0) return 0; @@ -124,17 +124,17 @@ uint Queue::Read(Buffer& bs) 4,如果队列过小,很有可能后来数据会覆盖前面数据 */ - uint len = bs.Length(); + int len = bs.Length(); if(!len) return 0; if(len > _size) len = _size; - uint rs = 0; + int rs = 0; while(true) { int total = _s.Capacity(); // 计算这一个循环剩下的位置 - uint remain = total - _tail; + int remain = total - _tail; // 如果要读取的数据都在这里 if(len <= remain) { diff --git a/Core/Queue.h b/Core/Queue.h index 8c7fa9fd..d0348f50 100644 --- a/Core/Queue.h +++ b/Core/Queue.h @@ -9,25 +9,25 @@ class Queue { private: Array _s; // 数据流 - uint _head; // 头部位置 - uint _tail; // 尾部位置 - uint _size; // 长度 + int _head; // 头部位置 + int _tail; // 尾部位置 + int _size; // 长度 public: Queue(); bool Empty() const { return _size == 0; } // 队列空 - uint Capacity() const { return _s.Capacity(); } // 队列容量 - uint Length() const { return _size; } // 队列大小 - void SetCapacity(uint len); + int Capacity() const { return _s.Capacity(); } // 队列容量 + int Length() const { return _size; } // 队列大小 + void SetCapacity(int len); void Clear(); void Enqueue(byte dat); byte Dequeue(); - uint Write(const Buffer& bs); // 批量写入 - uint Read(Buffer& bs); // 批量读取 + int Write(const Buffer& bs); // 批量写入 + int Read(Buffer& bs); // 批量读取 }; #endif diff --git a/Core/Random.cpp b/Core/Random.cpp index ff72fefc..2bbe59d0 100644 --- a/Core/Random.cpp +++ b/Core/Random.cpp @@ -11,19 +11,19 @@ Random::Random() srand((uint)time(NULL)); } -Random::Random(uint seed) +Random::Random(int seed) { srand(seed); } -uint Random::Next() const +int Random::Next() const { return rand(); } -uint Random::Next(uint max) const +int Random::Next(int max) const { - uint value = rand(); + int value = rand(); if(max == 0x100) return value & 0xFF; diff --git a/Core/Random.h b/Core/Random.h index 8d640974..cb4340c6 100644 --- a/Core/Random.h +++ b/Core/Random.h @@ -6,10 +6,10 @@ class Random { public: Random(); - Random(uint seed); + Random(int seed); - uint Next() const; - uint Next(uint max) const; + int Next() const; + int Next(int max) const; void Next(Buffer& bs) const; }; diff --git a/Core/SString.h b/Core/SString.h index b603acc4..e1a00ade 100644 --- a/Core/SString.h +++ b/Core/SString.h @@ -158,14 +158,14 @@ private: void init(); void release(); - bool Concat(cstring cstr, uint length); + bool Concat(cstring cstr, int length); - String& copy(cstring cstr, uint length); + String& copy(cstring cstr, int length); void move(String& rhs); bool CopyOrWrite(); using Array::CheckCapacity; - bool CheckCapacity(uint size); + bool CheckCapacity(int size); virtual void* Alloc(int len); int Search(cstring str, int len, int startIndex, bool rev) const; diff --git a/Core/Stream.cpp b/Core/Stream.cpp index 6fe2b074..bc69fbfe 100644 --- a/Core/Stream.cpp +++ b/Core/Stream.cpp @@ -11,16 +11,16 @@ extern ushort _REV16(ushort); extern uint _REV(uint); // 使用缓冲区初始化数据流。注意,此时指针位于0,而内容长度为缓冲区长度 -Stream::Stream(void* buf, uint len) +Stream::Stream(void* buf, int len) { Init(buf, len); } -Stream::Stream(const void* buf, uint len) +Stream::Stream(const void* buf, int len) { Init((void*)buf, len); - CanWrite = false; + CanWrite = false; } // 使用字节数组初始化数据流。注意,此时指针位于0,而内容长度为缓冲区长度 @@ -33,31 +33,31 @@ Stream::Stream(const Buffer& bs) { Init((void*)bs.GetBuffer(), bs.Length()); - CanWrite = false; + CanWrite = false; } -Stream::~Stream(){ } +Stream::~Stream() { } -void Stream::Init(void* buf, uint len) +void Stream::Init(void* buf, int len) { assert(buf, "buf"); - _Buffer = (byte*)buf; - _Capacity = len; - _Position = 0; - CanWrite = true; - CanResize = true; - Length = len; - Little = true; + _Buffer = (byte*)buf; + _Capacity = len; + _Position = 0; + CanWrite = true; + CanResize = true; + Length = len; + Little = true; } -bool Stream::CheckRemain(uint count) +bool Stream::CheckRemain(int count) { - uint remain = _Capacity - _Position; + int remain = _Capacity - _Position; // 容量不够,需要扩容 - if(count > remain) + if (count > remain) { - if(CanResize) + if (CanResize) debug_printf("数据流 0x%p 剩余容量 (%d - %d) = %d 不足 %d ,无法扩容!\r\n", this, _Capacity, _Position, remain, count); else assert(false, "无法扩容"); @@ -69,18 +69,18 @@ bool Stream::CheckRemain(uint count) } // 数据流容量 -uint Stream::Capacity() const { return _Capacity; } -void Stream::SetCapacity(uint len) { CheckRemain(len - _Position); } +int Stream::Capacity() const { return _Capacity; } +void Stream::SetCapacity(int len) { CheckRemain(len - _Position); } // 当前位置 -uint Stream::Position() const { return _Position; } +int Stream::Position() const { return _Position; } // 设置位置 bool Stream::SetPosition(int p) { // 允许移动到最后一个字节之后,也就是Length //assert(p <= Length, "设置的位置超出长度"); - if(p < 0 && p > Length) + if (p < 0 && p > Length) { debug_printf("设置的位置 %d 超出长度 %d\r\n", p, Length); return false; @@ -91,12 +91,12 @@ bool Stream::SetPosition(int p) } // 余下的有效数据流长度。0表示已经到达终点 -uint Stream::Remain() const { return Length - _Position; }; +int Stream::Remain() const { return Length - _Position; }; // 尝试前后移动一段距离,返回成功或者失败。如果失败,不移动游标 bool Stream::Seek(int offset) { - if(offset == 0) return true; + if (offset == 0) return true; return SetPosition(offset + _Position); } @@ -108,28 +108,28 @@ byte* Stream::GetBuffer() const { return _Buffer; } byte* Stream::Current() const { return &_Buffer[_Position]; } // 读取7位压缩编码整数 -uint Stream::ReadEncodeInt() +int Stream::ReadEncodeInt() { - uint value = 0; + int value = 0; // 同时计算最大4字节,避免无限读取错误数据 - for(int i = 0, k = 0; i < 4; i++, k += 7) + for (int i = 0, k = 0; i < 4; i++, k += 7) { int temp = ReadByte(); - if(temp < 0) break; + if (temp < 0) break; value |= (temp & 0x7F) << k; - if((temp & 0x80) == 0) break; + if ((temp & 0x80) == 0) break; } return value; } // 读取数据到字节数组,由字节数组指定大小。不包含长度前缀 -uint Stream::Read(Buffer& bs) +int Stream::Read(Buffer& bs) { - if(bs.Length() == 0) return 0; + if (bs.Length() == 0) return 0; Buffer ss(_Buffer, Length); - int count = bs.Copy(0, ss, _Position, bs.Length()); + int count = bs.Copy(0, ss, _Position, bs.Length()); // 游标移动 _Position += count; @@ -140,17 +140,17 @@ uint Stream::Read(Buffer& bs) } // 写入7位压缩编码整数 -uint Stream::WriteEncodeInt(uint value) +int Stream::WriteEncodeInt(int value) { - if(!CanWrite) return 0; + if (!CanWrite) return 0; byte buf[8]; - int k = 0; - for(int i = 0; i < 4 && value >= 0x80; i++) + int k = 0; + for (int i = 0; i < 4 && value >= 0x80; i++) { - buf[k++] = (byte)(value | 0x80); + buf[k++] = (byte)(value | 0x80); - value >>= 7; + value >>= 7; } { buf[k++] = (byte)value; @@ -162,17 +162,17 @@ uint Stream::WriteEncodeInt(uint value) // 把字节数组的数据写入到数据流。不包含长度前缀 bool Stream::Write(const Buffer& bs) { - int count = bs.Length(); - if(count == 0) return true; - if(!CanWrite) return false; - if(!CheckRemain(count)) return false; + int count = bs.Length(); + if (count == 0) return true; + if (!CanWrite) return false; + if (!CheckRemain(count)) return false; Buffer ss(_Buffer, _Capacity); - count = ss.Copy((uint)_Position, bs, 0, count); + count = ss.Copy(_Position, bs, 0, count); _Position += count; // 内容长度不是累加,而是根据位置而扩大 - if(_Position > Length) Length = _Position; + if (_Position > Length) Length = _Position; return true; } @@ -181,10 +181,10 @@ bool Stream::Write(const Buffer& bs) byte* Stream::ReadBytes(int count) { // 默认小于0时,读取全部数据 - if(count < 0) count = Remain(); + if (count < 0) count = Remain(); byte* p = Current(); - if(!Seek(count)) return nullptr; + if (!Seek(count)) return nullptr; return p; } @@ -192,22 +192,22 @@ byte* Stream::ReadBytes(int count) // 读取一个字节,不移动游标。如果没有可用数据,则返回-1 int Stream::Peek() const { - if(!Remain()) return -1; + if (!Remain()) return -1; return *Current(); } // 从数据流读取变长数据到字节数组。以压缩整数开头表示长度 -uint Stream::ReadArray(Buffer& bs) +int Stream::ReadArray(Buffer& bs) { - uint len = ReadEncodeInt(); - if(!len) + int len = ReadEncodeInt(); + if (!len) { bs.SetLength(0); return 0; } - if(len > bs.Length() && !bs.SetLength(len)) + if (len > bs.Length() && !bs.SetLength(len)) { // 在设计时,如果取得的长度超级大,可能是设计错误 //if(len > 0x40) @@ -257,10 +257,10 @@ String Stream::ReadString() int Stream::ReadByte() { - if(Length == _Position) return -1; + if (Length == _Position) return -1; byte* p = Current(); - if(!Seek(1)) return 0; + if (!Seek(1)) return 0; return *p; } @@ -269,8 +269,8 @@ ushort Stream::ReadUInt16() { ushort v; Buffer bs(&v, sizeof(v)); - if(!Read(bs)) return 0; - if(!Little) v = _REV16(v); + if (!Read(bs)) return 0; + if (!Little) v = _REV16(v); return v; } @@ -278,8 +278,8 @@ uint Stream::ReadUInt32() { uint v; Buffer bs(&v, sizeof(v)); - if(!Read(bs)) return 0; - if(!Little) v = _REV(v); + if (!Read(bs)) return 0; + if (!Little) v = _REV(v); return v; } @@ -287,8 +287,8 @@ UInt64 Stream::ReadUInt64() { UInt64 v; Buffer bs(&v, sizeof(v)); - if(!Read(bs)) return 0; - if(!Little) v = _REV(v >> 32) | ((UInt64)_REV(v & 0xFFFFFFFF) << 32); + if (!Read(bs)) return 0; + if (!Little) v = _REV(v >> 32) | ((UInt64)_REV(v & 0xFFFFFFFF) << 32); return v; } @@ -299,77 +299,77 @@ bool Stream::Write(byte value) bool Stream::Write(ushort value) { - if(!Little) value = _REV16(value); + if (!Little) value = _REV16(value); return Write(Buffer(&value, sizeof(value))); } bool Stream::Write(uint value) { - if(!Little) value = _REV(value); + if (!Little) value = _REV(value); return Write(Buffer(&value, sizeof(value))); } bool Stream::Write(UInt64 value) { - if(!Little) value = _REV(value >> 32) | ((UInt64)_REV(value & 0xFFFFFFFF) << 32); + if (!Little) value = _REV(value >> 32) | ((UInt64)_REV(value & 0xFFFFFFFF) << 32); return Write(Buffer(&value, sizeof(value))); } /******************************** MemoryStream ********************************/ -MemoryStream::MemoryStream(uint len) : Stream(_Arr, ArrayLength(_Arr)) +MemoryStream::MemoryStream(int len) : Stream(_Arr, ArrayLength(_Arr)) { - Length = 0; - _needFree = false; - if(len > ArrayLength(_Arr)) + Length = 0; + _needFree = false; + if (len > ArrayLength(_Arr)) { - byte* buf = new byte[len]; + byte* buf = new byte[len]; Init(buf, len); - _needFree = true; + _needFree = true; } } -MemoryStream::MemoryStream(void* buf, uint len) : Stream(buf, len) +MemoryStream::MemoryStream(void* buf, int len) : Stream(buf, len) { - _needFree = false; + _needFree = false; } // 销毁数据流 MemoryStream::~MemoryStream() { - if(_needFree) + if (_needFree) { - if(_Buffer != _Arr) delete[] _Buffer; + if (_Buffer != _Arr) delete[] _Buffer; _Buffer = nullptr; } } -bool MemoryStream::CheckRemain(uint count) +bool MemoryStream::CheckRemain(int count) { - uint remain = _Capacity - _Position; + int remain = _Capacity - _Position; // 容量不够,需要扩容 - if(count > remain) + if (count > remain) { - if(!CanResize) return Stream::CheckRemain(count); + if (!CanResize) return Stream::CheckRemain(count); // 原始容量成倍扩容 - uint total = _Position + count; - uint size = _Capacity; - if(size < 0x10) size = 0x10; - while(size < total) size <<= 1; + int total = _Position + count; + int size = _Capacity; + if (size < 0x10) size = 0x10; + while (size < total) size <<= 1; // 申请新的空间,并复制数据 byte* bufNew = new byte[size]; - if(Length > 0) Buffer(_Buffer, Length).CopyTo(0, bufNew, -1); + if (Length > 0) Buffer(_Buffer, Length).CopyTo(0, bufNew, -1); - if(_Buffer != _Arr && _needFree == true) delete[] _Buffer; + if (_Buffer != _Arr && _needFree == true) delete[] _Buffer; - _Buffer = bufNew; - _Capacity = size; - _needFree = true; + _Buffer = bufNew; + _Capacity = size; + _needFree = true; } return true; @@ -382,5 +382,5 @@ WEAK ushort _REV16(ushort value) WEAK uint _REV(uint value) { - return (_REV16(value & 0xFFFF) << 16) | (_REV16(value >> 16) >> 16); + return (_REV16(value & 0xFFFF) << 16) | (_REV16(value >> 16)); } diff --git a/Core/Stream.h b/Core/Stream.h index 622496c2..3eae0617 100644 --- a/Core/Stream.h +++ b/Core/Stream.h @@ -7,34 +7,34 @@ class Stream { protected: byte* _Buffer; // 数据缓冲区。扩容后会重新分配缓冲区 - uint _Capacity; // 缓冲区容量 - uint _Position; // 游标位置 + int _Capacity; // 缓冲区容量 + int _Position; // 游标位置 - void Init(void* buf, uint len); - virtual bool CheckRemain(uint count); + void Init(void* buf, int len); + virtual bool CheckRemain(int count); public: - uint Length; // 数据长度 + int Length; // 数据长度 bool Little; // 默认小字节序。仅影响数据读写操作 bool CanWrite; // 是否可写 bool CanResize; // 是否可以自动扩容 // 使用缓冲区初始化数据流。注意,此时指针位于0,而内容长度为缓冲区长度 - Stream(void* buf, uint len); - Stream(const void* buf, uint len); + Stream(void* buf, int len); + Stream(const void* buf, int len); // 使用字节数组初始化数据流。注意,此时指针位于0,而内容长度为缓冲区长度 explicit Stream(Buffer& bs); explicit Stream(const Buffer& bs); virtual ~Stream(); // 数据流容量 - uint Capacity() const; - void SetCapacity(uint len); + int Capacity() const; + void SetCapacity(int len); // 当前位置 - uint Position() const; + int Position() const; // 设置位置 bool SetPosition(int p); // 余下的有效数据流长度。0表示已经到达终点 - uint Remain() const; + int Remain() const; // 尝试前后移动一段距离,返回成功或者失败。如果失败,不移动游标 bool Seek(int offset); @@ -44,17 +44,17 @@ public: byte* Current() const; // 读取7位压缩编码整数 - uint ReadEncodeInt(); + int ReadEncodeInt(); // 读取数据到字节数组,由字节数组指定大小。不包含长度前缀 - uint Read(Buffer& bs); + int Read(Buffer& bs); // 写入7位压缩编码整数 - uint WriteEncodeInt(uint value); + int WriteEncodeInt(int value); // 把字节数组的数据写入到数据流。不包含长度前缀 bool Write(const Buffer& bs); // 从数据流读取变长数据到字节数组。以压缩整数开头表示长度 - uint ReadArray(Buffer& bs); + int ReadArray(Buffer& bs); ByteArray ReadArray(int count); // 把字节数组作为变长数据写入到数据流。以压缩整数开头表示长度 bool WriteArray(const Buffer& bs); @@ -105,13 +105,13 @@ private: bool _needFree; // 是否自动释放 //bool _resize; // 是否可以自动扩容 - virtual bool CheckRemain(uint count); + virtual bool CheckRemain(int count); public: // 分配指定大小的数据流 - MemoryStream(uint len = 0); + MemoryStream(int len = 0); // 使用缓冲区初始化数据流,支持自动扩容 - MemoryStream(void* buf, uint len); + MemoryStream(void* buf, int len); // 销毁数据流 virtual ~MemoryStream(); }; diff --git a/Core/String.cpp b/Core/String.cpp index 4808eefb..f8f5048c 100644 --- a/Core/String.cpp +++ b/Core/String.cpp @@ -17,7 +17,7 @@ #include "SString.h" char* utohex(uint value, byte size, char* string, bool upper); -extern char* itoa(int value, char* string, int radix); +//extern char* itoa(int value, char* string, int radix); extern char* ltoa(Int64 value, char* string, int radix); extern char* utoa(uint value, char* string, int radix); extern char* ultoa(UInt64 value, char* string, int radix); @@ -173,7 +173,7 @@ void String::release() init(); } -bool String::CheckCapacity(uint size) +bool String::CheckCapacity(int size) { int old = _Capacity; CheckCapacity(size + 1, _Length); @@ -201,7 +201,7 @@ void* String::Alloc(int len) } } -String& String::copy(cstring cstr, uint length) +String& String::copy(cstring cstr, int length) { if (!cstr || !length) return *this; @@ -324,12 +324,12 @@ bool String::Concat(const String& s) return Concat(s._Arr, s._Length); } -bool String::Concat(cstring cstr, uint length) +bool String::Concat(cstring cstr, int length) { if (!cstr) return false; if (length == 0) return true; - uint newlen = _Length + length; + int newlen = _Length + length; if (!CheckCapacity(newlen)) return false; //strcpy(_Arr + _Length, cstr); diff --git a/Device/Port.cpp b/Device/Port.cpp index cd3c67fa..5adae9d1 100644 --- a/Device/Port.cpp +++ b/Device/Port.cpp @@ -16,10 +16,10 @@ static bool Port_Reserve(Pin pin, bool flag); #ifdef REGION_Port Port::Port() { - _Pin = P0; - Opened = false; - Index = 0; - State = nullptr; + _Pin = P0; + Opened = false; + Index = 0; + State = nullptr; } #ifndef TINY @@ -84,7 +84,7 @@ bool Port::Open() #if DEBUG // 保护引脚 auto name = typeid(*this).name(); - while(*name >= '0' && *name <= '9') name++; + while (*name >= '0' && *name <= '9') name++; debug_printf("%s", name); Port_Reserve(_Pin, true); #endif @@ -106,7 +106,7 @@ void Port::Close() #if DEBUG // 保护引脚 auto name = typeid(*this).name(); - while(*name >= '0' && *name <= '9') name++; + while (*name >= '0' && *name <= '9') name++; debug_printf("%s", name); Port_Reserve(_Pin, false); debug_printf("\r\n"); @@ -172,9 +172,9 @@ OutputPort::OutputPort() : Port() { } OutputPort::OutputPort(Pin pin) : OutputPort(pin, 2) { } OutputPort::OutputPort(Pin pin, byte invert, bool openDrain, byte speed) : Port() { - OpenDrain = openDrain; - Speed = speed; - Invert = invert; + OpenDrain = openDrain; + Speed = speed; + Invert = invert; if (pin != P0) { @@ -187,7 +187,7 @@ OutputPort& OutputPort::Init(Pin pin, bool invert) { Port::Set(pin); - Invert = invert; + Invert = invert; return *this; } @@ -238,7 +238,9 @@ WEAK bool OutputPort::ReadInput() const { if (Empty()) return false; - return Port::Read() ^ Invert; + bool v = Port::Read(); + if (Invert)return !v; + return v; } void OutputPort::Up(uint ms) const @@ -341,7 +343,9 @@ InputPort& InputPort::Init(Pin pin, bool invert) // 读取本组所有引脚,任意脚为true则返回true,主要为单一引脚服务 bool InputPort::Read() const { - return Port::Read() ^ Invert; + bool v = Port::Read(); + if (Invert)return !v; + return v; } void InputPort::OnPress(bool down) @@ -367,7 +371,7 @@ void InputPort::OnPress(bool down) if (down) _Start = now; else - PressTime = now - _Start; + PressTime = (ushort)(now - _Start); /*if(down) { diff --git a/Device/SerialPort.cpp b/Device/SerialPort.cpp index c613f533..a739fdd3 100644 --- a/Device/SerialPort.cpp +++ b/Device/SerialPort.cpp @@ -147,8 +147,8 @@ bool SerialPort::Flush(uint times) // 从某个端口读取数据 uint SerialPort::OnRead(Buffer& bs) { - uint count = 0; - uint len = Rx.Length(); + int count = 0; + int len = Rx.Length(); // 如果没有数据,立刻返回,不要等待浪费时间 if(!len) { diff --git a/Device/Timer.cpp b/Device/Timer.cpp index b1edccec..fbb92c3b 100644 --- a/Device/Timer.cpp +++ b/Device/Timer.cpp @@ -12,22 +12,22 @@ static Timer* Timers[16] = { Timer::Timer(TIMER index) { - Timers[index] = this; + Timers[index] = this; - _index = index; + _index = index; OnInit(); // 默认情况下,预分频到1MHz,然后1000个周期,即是1ms中断一次 SetFrequency(10); - Opened = false; + Opened = false; } Timer::~Timer() { Close(); - if(OnTick.Method) SetHandler(false); + if (OnTick.Method) SetHandler(false); Timers[_index] = nullptr; } @@ -37,16 +37,16 @@ Timer* Timer::Create(byte index) { TS("Timer::Create"); - byte tcount = ArrayLength(Timers); + byte tcount = ArrayLength(Timers); //byte tcount = 46; // 特殊处理随机分配 - if(index == 0xFF) + if (index == 0xFF) { // 找到第一个可用的位置,没有被使用,并且该位置定时器存在 byte i = 0; - for(; i= tcount) + if (i >= tcount) { debug_printf("Timer::Create 失败!没有空闲定时器!\r\n"); return nullptr; @@ -57,7 +57,7 @@ Timer* Timer::Create(byte index) assert(index < tcount, "index"); - if(Timers[index]) + if (Timers[index]) return Timers[index]; else return new Timer((TIMER)index); @@ -65,7 +65,7 @@ Timer* Timer::Create(byte index) void Timer::Open() { - if(Opened) return; + if (Opened) return; TS("Timer::Open"); @@ -76,7 +76,7 @@ void Timer::Open() void Timer::Close() { - if(!Opened) return; + if (!Opened) return; TS("Timer::Close"); @@ -89,9 +89,9 @@ void Timer::Close() void Timer::Register(const Delegate& dlg) { - OnTick = dlg; + OnTick = dlg; - SetHandler(dlg.Method); + SetHandler(dlg.Method != nullptr); } void Timer::OnInterrupt() diff --git a/Kernel/Heap.cpp b/Kernel/Heap.cpp index 37eeb881..ad7196ed 100644 --- a/Kernel/Heap.cpp +++ b/Kernel/Heap.cpp @@ -27,7 +27,7 @@ typedef struct MemoryBlock_ } MemoryBlock; /******************************** Heap ********************************/ -Heap::Heap(uint addr, uint size) +Heap::Heap(uint addr, int size) { Current = this; @@ -55,11 +55,11 @@ Heap::Heap(uint addr, uint size) debug_printf("Heap::Init(%p, %d) Free=%d \r\n", Address, Size, FreeSize()); } -uint Heap::Used() const { return _Used; } -uint Heap::Count() const { return _Count; } -uint Heap::FreeSize() const { return Size - _Used; } +int Heap::Used() const { return _Used; } +int Heap::Count() const { return _Count; } +int Heap::FreeSize() const { return Size - _Used; } -void* Heap::Alloc(uint size) +void* Heap::Alloc(int size) { // 要申请的内存大小需要对齐 size = (size+MEMORY_ALIGN-1) & (~(MEMORY_ALIGN-1)); @@ -73,7 +73,7 @@ void* Heap::Alloc(uint size) #if DEBUG // 检查头部完整性 auto head = (MemoryBlock*)Address; - assert(head->Used <= Size && (uint)head + head->Used <= (uint)head->Next, "堆头被破坏!"); + assert(head->Used <= (uint)Size && (uint)head + head->Used <= (uint)head->Next, "堆头被破坏!"); assert(_Used <= Size, "Heap::Used异常!"); #endif diff --git a/Kernel/Heap.h b/Kernel/Heap.h index daeb8614..4cde82bb 100644 --- a/Kernel/Heap.h +++ b/Kernel/Heap.h @@ -6,23 +6,23 @@ class Heap { public: uint Address;// 开始地址 - uint Size; // 大小 + int Size; // 大小 - Heap(uint addr, uint size); + Heap(uint addr, int size); - uint Used() const; // 已使用内存数 - uint Count() const; // 已使用内存块数 - uint FreeSize() const; // 可用内存数 + int Used() const; // 已使用内存数 + int Count() const; // 已使用内存块数 + int FreeSize() const; // 可用内存数 - void* Alloc(uint size); + void* Alloc(int size); void Free(void* ptr); // 当前堆 static Heap* Current; private: - uint _Used; - uint _Count; + int _Used; + int _Count; void* _First; // 第一个有空闲的内存块,加速搜索 }; diff --git a/Kernel/Task.cpp b/Kernel/Task.cpp index 90764826..a3eaccaa 100644 --- a/Kernel/Task.cpp +++ b/Kernel/Task.cpp @@ -179,7 +179,7 @@ TaskScheduler::TaskScheduler(cstring name) } // 使用外部缓冲区初始化任务列表,避免频繁的堆分配 -void TaskScheduler::Set(Task* tasks, uint count) +void TaskScheduler::Set(Task* tasks, int count) { for(int i=0; i= BASE_YEAR_US) sec -= BASE_YEAR_US; - BaseSeconds = (uint)(sec - Seconds); + BaseSeconds = (int)(sec - Seconds); #if DEBUG /*DateTime dt(sec); diff --git a/Message/BinaryPair.cpp b/Message/BinaryPair.cpp index b0fefeac..c5c7eb12 100644 --- a/Message/BinaryPair.cpp +++ b/Message/BinaryPair.cpp @@ -36,7 +36,7 @@ Buffer BinaryPair::Get(cstring name) const String sn = name; auto& ms = *_s; - uint p = ms.Position(); + int p = ms.Position(); for(int i=0; i<2; i++) { while(ms.Remain() && (i ==0 || ms.Position() < p)) diff --git a/Net/NetworkInterface.cpp b/Net/NetworkInterface.cpp index c53c9253..6946cb6d 100644 --- a/Net/NetworkInterface.cpp +++ b/Net/NetworkInterface.cpp @@ -4,9 +4,9 @@ #define NET_DEBUG DEBUG //#define NET_DEBUG 0 #if NET_DEBUG - #define net_printf debug_printf +#define net_printf debug_printf #else - #define net_printf(format, ...) +#define net_printf(format, ...) #endif List NetworkInterface::All; @@ -16,7 +16,7 @@ List NetworkInterface::All; struct NetConfig { uint IP; // 本地IP地址 - uint Mask; // 子网掩码 + uint Mask; // 子网掩码 byte Mac[6]; // 本地Mac地址 byte Mode; // 无线模式。0不是无线,1是STA,2是AP,3是混合 byte Reserved; @@ -32,16 +32,16 @@ struct NetConfig NetworkInterface::NetworkInterface() { - Name = "Network"; - Speed = 0; - Opened = false; - Linked = false; + Name = "Network"; + Speed = 0; + Opened = false; + Linked = false; - Mode = NetworkType::Wire; - Status = NetworkStatus::None; + Mode = NetworkType::Wire; + Status = NetworkStatus::None; - _taskLink = 0; - _retry = 0; + _taskLink = 0; + _retry = 0; debug_printf("Network::Add 0x%p\r\n", this); All.Add(this); @@ -52,32 +52,32 @@ NetworkInterface::~NetworkInterface() debug_printf("Network::Remove 0x%p\r\n", this); All.Remove(this); - if(!Opened) Close(); + if (!Opened) Close(); } bool NetworkInterface::Open() { - if(Opened) return true; + if (Opened) return true; // 打开接口 - Opened = OnOpen(); + Opened = OnOpen(); // 建立连接任务 - _taskLink = Sys.AddTask([](void* s){ ((NetworkInterface*)s)->OnLoop(); }, this, 0, 1000, "网络检测"); + _taskLink = Sys.AddTask([](void* s) { ((NetworkInterface*)s)->OnLoop(); }, this, 0, 1000, "网络检测"); return Opened; } void NetworkInterface::Close() { - if(!Opened) return; + if (!Opened) return; Sys.RemoveTask(_taskLink); OnClose(); - Linked = false; - Opened = false; + Linked = false; + Opened = false; } bool NetworkInterface::Active() const @@ -87,26 +87,26 @@ bool NetworkInterface::Active() const void NetworkInterface::OnLoop() { - bool old = Linked; + bool old = Linked; // 未连接时执行连接 - if(!old) + if (!old) { - bool link = OnLink(++_retry); - if(link ^ old) + bool link = OnLink(++_retry); + if (link ^ old) { debug_printf("%s::Change %s => %s \r\n", Name, old ? "连接" : "断开", link ? "连接" : "断开"); - Linked = link; - _retry = 0; + Linked = link; + _retry = 0; } } else { // 检测并通知状态改变 - bool link = CheckLink(); - if(link ^ old) + bool link = CheckLink(); + if (link ^ old) { debug_printf("%s::Change %s => %s \r\n", Name, old ? "连接" : "断开", link ? "连接" : "断开"); - Linked = link; + Linked = link; Changed(*this); } @@ -115,13 +115,13 @@ void NetworkInterface::OnLoop() Socket* NetworkInterface::CreateClient(const NetUri& uri) { - if(!Active()) return nullptr; + if (!Active()) return nullptr; - auto socket = CreateSocket(uri.Type); - if(socket) + auto socket = CreateSocket(uri.Type); + if (socket) { - socket->Local.Address = uri.Address; - socket->Local.Port = uri.Port; + socket->Local.Address = uri.Address; + socket->Local.Port = uri.Port; } return socket; @@ -129,15 +129,15 @@ Socket* NetworkInterface::CreateClient(const NetUri& uri) Socket* NetworkInterface::CreateRemote(const NetUri& uri) { - if(!Active()) return nullptr; + if (!Active()) return nullptr; - auto socket = CreateSocket(uri.Type); - if(socket) + auto socket = CreateSocket(uri.Type); + if (socket) { - socket->Local.Address = IP; - socket->Remote.Address = uri.Address; - socket->Remote.Port = uri.Port; - socket->Server = uri.Host; + socket->Local.Address = IP; + socket->Remote.Address = uri.Address; + socket->Remote.Port = uri.Port; + socket->Server = uri.Host; } return socket; @@ -150,53 +150,53 @@ void NetworkInterface::InitConfig() // 随机IP,取ID最后一个字节 IP = defip; byte first = Sys.ID[0]; - if(first <= 1 || first >= 254) first = 2; - IP[3] = first; + if (first <= 1 || first >= 254) first = 2; + IP[3] = first; - Mask = IPAddress(255, 255, 255, 0); - Gateway = defip; + Mask = IPAddress(255, 255, 255, 0); + Gateway = defip; // 修改为双DNS方案,避免单点故障。默认使用阿里和百度公共DNS。 - DNSServer = IPAddress(223, 5, 5, 5); - DNSServer2 = IPAddress(180, 76, 76, 76); + DNSServer = IPAddress(223, 5, 5, 5); + DNSServer2 = IPAddress(180, 76, 76, 76); - auto& mac = Mac; + auto& mac = Mac; // 随机Mac,前2个字节取自ASCII,最后4个字节取自后三个ID //mac[0] = 'W'; mac[1] = 'S'; // 第一个字节最低位为1表示组播地址,所以第一个字节必须是偶数 - mac[0] = 'N'; - mac[1] = 'X'; - for(int i=0; i< 4; i++) + mac[0] = 'N'; + mac[1] = 'X'; + for (int i = 0; i < 4; i++) mac[2 + i] = Sys.ID[3 - i]; // 多网卡时MAC不能相同 - int k=0; - for(;kGet(Name); - if(!nc) return false; + auto nc = (NetConfig*)cfg->Get(Name); + if (!nc) return false; debug_printf("%s::Load %d 字节\r\n", Name, sizeof(NetConfig)); - IP = nc->IP; - Mask = nc->Mask; - Mac = nc->Mac; - Mode = (NetworkType)nc->Mode; + IP = nc->IP; + Mask = nc->Mask; + Mac = nc->Mac; + Mode = (NetworkType)nc->Mode; - DNSServer = nc->DNSServer; - DNSServer2 = nc->DNSServer2; - Gateway = nc->Gateway; + DNSServer = nc->DNSServer; + DNSServer2 = nc->DNSServer2; + Gateway = nc->Gateway; OnLoadConfig(Buffer(nc->Data, sizeof(nc->Data))); @@ -205,18 +205,18 @@ bool NetworkInterface::LoadConfig() bool NetworkInterface::SaveConfig() const { - auto cfg = Config::Current; - if(!cfg) return false; + auto cfg = Config::Current; + if (!cfg) return false; NetConfig nc; - nc.IP = IP.Value; - nc.Mask = Mask.Value; + nc.IP = IP.Value; + nc.Mask = Mask.Value; Mac.CopyTo(nc.Mac); - nc.Mode = (byte)Mode; + nc.Mode = (byte)Mode; - nc.DNSServer = DNSServer.Value; - nc.DNSServer2 = DNSServer2.Value; - nc.Gateway = Gateway.Value; + nc.DNSServer = DNSServer.Value; + nc.DNSServer2 = DNSServer2.Value; + nc.Gateway = Gateway.Value; Buffer dat(nc.Data, sizeof(nc.Data)); dat.Clear(); @@ -225,7 +225,7 @@ bool NetworkInterface::SaveConfig() const debug_printf("%s::Save %d 字节\r\n", Name, sizeof(nc)); Buffer bs(&nc, sizeof(nc)); - return cfg->Set(Name, bs); + return cfg->Set(Name, bs) != nullptr; } void NetworkInterface::ShowConfig() const @@ -243,27 +243,27 @@ void NetworkInterface::ShowConfig() const //DHCPServer.Show(); net_printf("\r\n DNS:\t"); DNSServer.Show(); - if(!DNSServer2.IsAny()) + if (!DNSServer2.IsAny()) { net_printf("\r\n DNS2:\t"); DNSServer2.Show(); } net_printf("\r\n Speed:\t%dMps", Speed); net_printf("\r\n Mode:\t"); - switch(Mode) + switch (Mode) { - case NetworkType::Wire: - net_printf("有线"); - break; - case NetworkType::Station: - net_printf("无线Station"); - break; - case NetworkType::AP: - net_printf("无线AP热点"); - break; - case NetworkType::STA_AP: - net_printf("无线Station+AP热点"); - break; + case NetworkType::Wire: + net_printf("有线"); + break; + case NetworkType::Station: + net_printf("无线Station"); + break; + case NetworkType::AP: + net_printf("无线AP热点"); + break; + case NetworkType::STA_AP: + net_printf("无线Station+AP热点"); + break; } //net_printf("\r\n"); @@ -283,10 +283,10 @@ IPAddress NetworkInterface::QueryDNS(const String& domain) WiFiInterface::WiFiInterface() : NetworkInterface() { - Mode = NetworkType::Station; + Mode = NetworkType::Station; - SSID = nullptr; - Pass = nullptr; + SSID = nullptr; + Pass = nullptr; } bool WiFiInterface::IsStation() const @@ -302,13 +302,13 @@ bool WiFiInterface::IsAP() const Socket* WiFiInterface::CreateClient(const NetUri& uri) { // WiFi无需连接路由器就可以建立本地监听 - if(!Opened) return nullptr; + if (!Opened) return nullptr; - auto socket = CreateSocket(uri.Type); - if(socket) + auto socket = CreateSocket(uri.Type); + if (socket) { - socket->Local.Address = uri.Address; - socket->Local.Port = uri.Port; + socket->Local.Address = uri.Address; + socket->Local.Port = uri.Port; } return socket; @@ -316,13 +316,13 @@ Socket* WiFiInterface::CreateClient(const NetUri& uri) void WiFiInterface::OnInitConfig() { - if(!SSID) - SSID = new String(); + if (!SSID) + SSID = new String(); else SSID->Clear(); - if(!Pass) - Pass = new String(); + if (!Pass) + Pass = new String(); else Pass->Clear(); } @@ -330,19 +330,19 @@ void WiFiInterface::OnInitConfig() void WiFiInterface::OnLoadConfig(const Buffer& buf) { Stream ms(buf); - if(SSID) *SSID = ms.ReadString(); - if(Pass) *Pass = ms.ReadString(); + if (SSID) *SSID = ms.ReadString(); + if (Pass) *Pass = ms.ReadString(); } void WiFiInterface::OnSaveConfig(Buffer& buf) const { Stream ms(buf); - if(SSID) ms.WriteArray(*SSID); - if(Pass) ms.WriteArray(*Pass); + if (SSID) ms.WriteArray(*SSID); + if (Pass) ms.WriteArray(*Pass); } void WiFiInterface::OnShowConfig() const { - if(SSID) { net_printf("\r\n SSID:\t"); SSID->Show(false); } - if(Pass) { net_printf("\r\n Pass:\t"); Pass->Show(false); } + if (SSID) { net_printf("\r\n SSID:\t"); SSID->Show(false); } + if (Pass) { net_printf("\r\n Pass:\t"); Pass->Show(false); } } diff --git a/Test/ArrayTest.cpp b/Test/ArrayTest.cpp index 39fc3400..06b972a1 100644 --- a/Test/ArrayTest.cpp +++ b/Test/ArrayTest.cpp @@ -1,4 +1,4 @@ -#include "Kernel\Sys.h" +#include "Kernel\Sys.h" #if DEBUG static void TestAssign() diff --git a/Test/DictionaryTest.cpp b/Test/DictionaryTest.cpp index b4ceef5f..bec31b19 100644 --- a/Test/DictionaryTest.cpp +++ b/Test/DictionaryTest.cpp @@ -1,4 +1,4 @@ -#include "Kernel\Sys.h" +#include "Kernel\Sys.h" #include "Core\Dictionary.h" #if DEBUG diff --git a/Test/FlashTest.cpp b/Test/FlashTest.cpp index 18770d24..9bc121d7 100644 --- a/Test/FlashTest.cpp +++ b/Test/FlashTest.cpp @@ -7,7 +7,7 @@ void TestFlash() debug_printf("TestFlash Start......\r\n"); byte buf[] = "STM32F10x SPI Firmware Library Example: communication with an AT45DB SPI FLASH"; - uint size = ArrayLength(buf); + int size = ArrayLength(buf); Flash flash; // 凑一个横跨两页的地址 diff --git a/Test/JsonTest.cpp b/Test/JsonTest.cpp index 483172ad..c086bb81 100644 --- a/Test/JsonTest.cpp +++ b/Test/JsonTest.cpp @@ -1,4 +1,4 @@ -#include "Kernel\Sys.h" +#include "Kernel\Sys.h" #include "Message\Json.h" #if DEBUG diff --git a/Test/ListTest.cpp b/Test/ListTest.cpp index 095e9479..3361e631 100644 --- a/Test/ListTest.cpp +++ b/Test/ListTest.cpp @@ -1,4 +1,4 @@ -#include "Kernel\Sys.h" +#include "Kernel\Sys.h" #include "Core\List.h" #if DEBUG diff --git a/TinyNet/TinyController.cpp b/TinyNet/TinyController.cpp index b168808c..dadf6328 100644 --- a/TinyNet/TinyController.cpp +++ b/TinyNet/TinyController.cpp @@ -28,7 +28,7 @@ TinyController::TinyController() : Controller() while(Address < 2 || Address > 254) { Sys.Delay(30); - Address = Sys.Ms(); + Address = (byte)Sys.Ms(); } // 接收模式。0只收自己,1接收自己和广播,2接收所有。默认0 @@ -350,7 +350,7 @@ void TinyController::AckRequest(const TinyMessage& msg) auto& node = _Queue[i]; if(node.Using && node.Seq == msg.Seq) { - int cost = Sys.Ms() - node.StartTime; + int cost = (int)(Sys.Ms() - node.StartTime); if(cost < 0) cost = -cost; Total.Cost += cost; diff --git a/TinyNet/TinyMessage.cpp b/TinyNet/TinyMessage.cpp index b756e1e6..3b69a564 100644 --- a/TinyNet/TinyMessage.cpp +++ b/TinyNet/TinyMessage.cpp @@ -105,7 +105,7 @@ void TinyMessage::Write(Stream& ms) const // 实际数据拷贝到占位符 auto p = (TinyMessage*)this; p->_Code = Code; - p->_Length = len; + p->_Length = (byte)len; p->_Reply = Reply; p->_Error = Error;