优先使用int替换为uint,方便运算,能够准确识别产生的负数。

This commit is contained in:
大石头X2 2017-02-28 14:21:30 +08:00
parent 37e7b9ad72
commit 226f837205
25 changed files with 75 additions and 73 deletions

View File

@ -76,8 +76,8 @@ bool ConfigBlock::Init(const String& name, const Buffer& bs)
{
if(!name) return false;
assert(name.Length() < sizeof(Name), "配置区名称太长");
if(name.Length() >= sizeof(Name)) return false;
assert(name.Length() < (int)sizeof(Name), "配置区名称太长");
if(name.Length() >= (int)sizeof(Name)) return false;
//TS("ConfigBlock::Init");
@ -181,8 +181,8 @@ const ConfigBlock* FindBlock(const Storage& st, uint addr, const String& name)
if(!name) return nullptr;
assert(name.Length() < sizeof(ConfigBlock::Name), "配置区名称太长");
if(name.Length() >= sizeof(ConfigBlock::Name)) return nullptr;
assert(name.Length() < (int)sizeof(ConfigBlock::Name), "配置区名称太长");
if(name.Length() >= (int)sizeof(ConfigBlock::Name)) return nullptr;
if(!CheckSignature(st, addr, false)) return nullptr;
@ -307,8 +307,8 @@ const void* Config::Set(const String& name, const Buffer& bs) const
if(!name) return nullptr;
assert(name.Length() < sizeof(ConfigBlock::Name), "配置区名称太长");
if(name.Length() >= sizeof(ConfigBlock::Name)) return nullptr;
assert(name.Length() < (int)sizeof(ConfigBlock::Name), "配置区名称太长");
if(name.Length() >= (int)sizeof(ConfigBlock::Name)) return nullptr;
auto cfg = FindBlock(Device, Address, name);
if(!cfg) cfg = NewBlock(Device, Address, bs.Length());

View File

@ -50,7 +50,7 @@ public:
};
// 数组长度
#define ArrayLength(arr) (sizeof(arr)/sizeof(arr[0]))
#define ArrayLength(arr) (int)(sizeof(arr)/sizeof(arr[0]))
// 数组清零,固定长度
//#define ArrayZero(arr) memset(arr, 0, sizeof(arr))

View File

@ -3,7 +3,7 @@
//Pin _Pins[] = ADC1_PINS;
ADConverter::ADConverter(byte line, uint channel)
ADConverter::ADConverter(byte line, int channel)
{
assert(line >= 1 && line <= 3, "ADC Line");
@ -12,7 +12,7 @@ ADConverter::ADConverter(byte line, uint channel)
uint dat = 1;
Count = 0;
for(int i=0; i<ArrayLength(Data); i++, dat <<= 1)
for(int i=0; i<(int)ArrayLength(Data); i++, dat <<= 1)
{
if(Channel & dat) Count++;
}
@ -56,7 +56,7 @@ void ADConverter::Open()
debug_printf("ADC::Open %d 共%d个通道\r\n", Line, Count);
uint dat = 1;
for(int i=0; i<ArrayLength(Data); i++, dat <<= 1)
for(int i=0; i<(int)ArrayLength(Data); i++, dat <<= 1)
{
if(Channel & dat)
{

View File

@ -27,11 +27,11 @@ private:
public :
byte Line; // 中断线 1/2/3
byte Count; // 通道个数
uint Channel; // 使用哪些通道,每个通道一位
int Channel; // 使用哪些通道,每个通道一位
ushort Data[19]; // 存放数据
// 如果需要读取温度和电压通道需要设置0x30000
ADConverter(byte line = 1, uint channel = 0);
ADConverter(byte line = 1, int channel = 0);
void Add(Pin pin);
void Remove(Pin pin);

View File

@ -8,7 +8,7 @@ class Flash : public BlockStorage
{
public:
// 写块
virtual bool WriteBlock(uint address, const byte* buf, uint len, bool inc) const;
virtual bool WriteBlock(uint address, const byte* buf, int len, bool inc) const;
// 擦除块 (段地址)
virtual bool EraseBlock(uint address) const;

View File

@ -243,7 +243,7 @@ WEAK bool OutputPort::ReadInput() const
return v;
}
void OutputPort::Up(uint ms) const
void OutputPort::Up(int ms) const
{
if (Empty()) return;
@ -252,7 +252,7 @@ void OutputPort::Up(uint ms) const
Write(false);
}
void OutputPort::Down(uint ms) const
void OutputPort::Down(int ms) const
{
if (Empty()) return;
@ -261,7 +261,7 @@ void OutputPort::Down(uint ms) const
Write(true);
}
void OutputPort::Blink(uint times, uint ms) const
void OutputPort::Blink(int times, int ms) const
{
if (Empty()) return;

View File

@ -87,10 +87,10 @@ public:
void Write(bool value) const;
// 拉高一段时间后拉低
void Up(uint ms) const;
void Down(uint ms) const;
void Up(int ms) const;
void Down(int ms) const;
// 闪烁多次
void Blink(uint times, uint ms) const;
void Blink(int times, int ms) const;
// Read/ReadInput 的区别在于,前者读输出后者读输入,在开漏输出的时候有很大区别
virtual bool Read() const;

View File

@ -71,7 +71,7 @@ bool AT24CXX::Read(uint addr, Buffer& bs) const
IIC->Address = Address << 1;
uint len = IIC->Read((ushort)addr, bs);
int len = IIC->Read((ushort)addr, bs);
if(len == 0)return false;
if(len != bs.Length()) bs.SetLength(len);

View File

@ -44,7 +44,7 @@ bool DHT11::Read(ushort& temp, ushort& humi)
/*开始接收数据*/
byte buf[5];
for(int i=0; i<ArrayLength(buf); i++)
for(int i=0; i<(int)ArrayLength(buf); i++)
buf[i] = ReadByte();
/*读取结束,引脚改为输出模式*/

View File

@ -929,8 +929,8 @@ bool Enc28j60::OnWrite(const Buffer& bs)
// packet该包应该存储到的缓冲区maxlen可接受的最大数据长度
uint Enc28j60::OnRead(Buffer& bs)
{
uint rxstat;
uint len;
int rxstat;
int len;
// 检测并打开包接收
if(!(ReadReg(ECON1) & ECON1_RXEN)) WriteOp(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_RXEN);

View File

@ -67,7 +67,8 @@ bool JTW8953::Write(const Buffer& bs) const
bool JTW8953::Read(Buffer& bs) const
{
if (!IIC) return false;
uint len = IIC->Read(0, bs);
int len = IIC->Read(0, bs);
if (len == 0)return false;
if (len != bs.Length()) bs.SetLength(len);

View File

@ -149,7 +149,7 @@ INROOT UInt64 TSys::Ms() const { return Time.Current(); }
// 系统绝对当前时间,秒
INROOT uint TSys::Seconds() const { return Time.Seconds + Time.BaseSeconds; }
INROOT void TSys::Sleep(uint ms) const
INROOT void TSys::Sleep(int ms) const
{
// 优先使用线程级睡眠
if(OnSleep)
@ -174,7 +174,7 @@ INROOT void TSys::Sleep(uint ms) const
}
}
INROOT void TSys::Delay(uint us) const
INROOT void TSys::Delay(int us) const
{
// 如果延迟微秒数太大,则使用线程级睡眠
if(OnSleep && us >= 2000)

View File

@ -98,10 +98,10 @@ public:
UInt64 Ms() const; // 系统启动后的毫秒数
uint Seconds() const; // 系统绝对当前时间,秒
void Sleep(uint ms) const; // 毫秒级延迟
void Delay(uint us) const; // 微秒级延迟
typedef void (*FuncU32)(uint param);
FuncU32 OnSleep;
void Sleep(int ms) const; // 毫秒级延迟
void Delay(int us) const; // 微秒级延迟
typedef void (*FuncI32)(int param);
FuncI32 OnSleep;
bool CheckMemory() const;

View File

@ -387,9 +387,9 @@ byte Thread::BuildReady()
return count;
}
void OnSleep(uint ms)
static void OnSleep(int ms)
{
Thread* th = Thread::Current;
auto th = Thread::Current;
if(th) th->Sleep(ms);
}

View File

@ -15,7 +15,7 @@ Message::Message(byte code)
}
// 设置数据。
void Message::SetData(const Buffer& bs, uint offset)
void Message::SetData(const Buffer& bs, int offset)
{
Length = bs.Length() + offset;
if(Length > 0) bs.CopyTo(0, Data + offset, Length);

View File

@ -11,7 +11,7 @@ public:
byte Reply; // 是否响应
byte Error; // 是否错误
byte OneWay; // 是否单向
ushort Length; // 数据长度
short Length; // 数据长度
byte* Data; // 数据。指向子类内部声明的缓冲区
void* State; // 其它状态数据
@ -20,9 +20,9 @@ public:
Message(byte code = 0);
// 消息所占据的指令数据大小。包括头部、负载数据、校验和附加数据
virtual uint Size() const = 0;
virtual int Size() const = 0;
// 最大数据大小
virtual uint MaxDataSize() const = 0;
virtual int MaxDataSize() const = 0;
// 从数据流中读取消息
virtual bool Read(Stream& ms) = 0;
@ -35,7 +35,7 @@ public:
virtual bool Clone(const Message& msg);
// 设置数据
void SetData(const Buffer& bs, uint offset = 0);
void SetData(const Buffer& bs, int offset = 0);
void SetError(byte errorCode, cstring msg = nullptr);
// 负载数据转数据流

View File

@ -153,7 +153,7 @@ bool Blu40::SetBP(int BP)
Sys.Delay(500);
Port->Read(ds);
for(int j=0;j<sizeof(BPSOK);j++)
for(int j=0;j<(int)sizeof(BPSOK);j++)
{
if(ds[j] != BPSOK[j])
{
@ -188,7 +188,7 @@ bool Blu40::SetBP(int BP)
Port->Read(ds);
//"AT:BPS SET AFTER 2S \r\n\0"
//"AT:ERR\r\n\0"
for(int j=0;j<sizeof(BPSOK);j++)
for(int j=0;j<(int)sizeof(BPSOK);j++)
{
if(ds[j] != BPSOK[j])
{
@ -215,7 +215,7 @@ bool Blu40::CheckSet()
//byte buf[40];
ByteArray bs;
Port->Read(bs);
for(int i=0;i<sizeof(ATOK);i++)
for(int i=0;i<(int)sizeof(ATOK);i++)
{
if(bs[i] != ATOK[i])
{

View File

@ -10,7 +10,7 @@
bool BlockStorage::Read(uint address, Buffer& bs) const
{
uint len = bs.Length();
int len = bs.Length();
if (!len) return true;
if(address < Start || address + len > Start + Size) return false;
@ -29,7 +29,7 @@ bool BlockStorage::Write(uint address, const Buffer& bs) const
assert((address & 0x01) == 0x00, "Write起始地址必须是2字节对齐");
auto buf = bs.GetBuffer();
uint len = bs.Length();
int len = bs.Length();
if (!len) return true;
if(address < Start || address + len > Start + Size) return false;
@ -93,8 +93,8 @@ bool BlockStorage::Write(uint address, const Buffer& bs) const
if(offset)
{
// 计算块起始地址和剩余大小
uint blk = addr - offset;
uint size = Block - offset;
int blk = addr - offset;
int size = Block - offset;
// 前段原始数据,中段来源数据,末段原始数据
ms.Copy(0, (byte*)blk, offset);
if(size > remain) size = remain;
@ -141,7 +141,7 @@ bool BlockStorage::Write(uint address, const Buffer& bs) const
return true;
}
bool BlockStorage::Memset(uint address, byte data, uint len) const
bool BlockStorage::Memset(uint address, byte data, int len) const
{
assert((address & 0x01) == 0x00, "Memset起始地址必须是2字节对齐");
@ -156,7 +156,7 @@ bool BlockStorage::Memset(uint address, byte data, uint len) const
}
// 擦除块。起始地址字节数量默认0表示擦除全部
bool BlockStorage::Erase(uint address, uint len) const
bool BlockStorage::Erase(uint address, int len) const
{
assert((address & 0x01) == 0x00, "Erase起始地址必须是2字节对齐");
@ -173,7 +173,7 @@ bool BlockStorage::Erase(uint address, uint len) const
uint end = address + len;
// 需要检查是否擦除的范围,从第一段开始
uint addr = address;
uint len2 = blk + Block - address;
int len2 = blk + Block - address;
// 如果还不够一段,则直接长度
if(len2 > len) len2 = len;
while(blk < end)
@ -191,7 +191,7 @@ bool BlockStorage::Erase(uint address, uint len) const
}
/* 指定块是否被擦除 */
bool BlockStorage::IsErased(uint address, uint len) const
bool BlockStorage::IsErased(uint address, int len) const
{
assert((address & 0x01) == 0x00, "IsErased起始地址必须是2字节对齐");

View File

@ -17,9 +17,9 @@ public:
class BlockStorage : public Storage
{
public:
uint Size; // 容量大小,字节
int Size; // 容量大小,字节
uint Start; // 起始地址
uint Block; // 每块字节数
int Block; // 每块字节数
bool ReadModifyWrite; // 是否读改写
// 读取
@ -27,17 +27,17 @@ public:
// 写入
virtual bool Write(uint address, const Buffer& bs) const;
// 清空
virtual bool Memset(uint address, byte data, uint len) const;
virtual bool Memset(uint address, byte data, int len) const;
// 擦除
virtual bool Erase(uint address, uint len) const;
virtual bool Erase(uint address, int len) const;
protected:
// 写入块
virtual bool WriteBlock(uint address, const byte* buf, uint len, bool inc) const = 0;
virtual bool WriteBlock(uint address, const byte* buf, int len, bool inc) const = 0;
// 擦除块
virtual bool EraseBlock(uint address) const = 0;
// 指定块是否被擦除
virtual bool IsErased(uint address, uint len) const;
virtual bool IsErased(uint address, int len) const;
};
// 字符存储接口

View File

@ -60,7 +60,7 @@ bool TinyMessage::Read(Stream& ms)
if(Dest == Src) return false;
// 校验剩余长度
ushort len = Length;
short len = Length;
if(ms.Remain() < len + 2) return false;
// 避免错误指令超长,导致溢出
@ -145,12 +145,12 @@ bool TinyMessage::Valid() const
}
// 消息所占据的指令数据大小。包括头部、负载数据和附加数据
uint TinyMessage::Size() const
int TinyMessage::Size() const
{
return MinSize + Length;
}
uint TinyMessage::MaxDataSize() const
int TinyMessage::MaxDataSize() const
{
return Data == _Data ? ArrayLength(_Data) : Length;
}

View File

@ -29,17 +29,17 @@ public:
// 负载数据及校验部分,并非内存布局。
ushort Crc; // 整个消息的Crc16校验计算前Checksum清零
static const uint HeaderSize = 1 + 1 + 1 + 1 + 1 + 1; // 消息头部大小
static const uint MinSize = HeaderSize + 0 + 2; // 最小消息大小
static const int HeaderSize = 1 + 1 + 1 + 1 + 1 + 1; // 消息头部大小
static const int MinSize = HeaderSize + 0 + 2; // 最小消息大小
public:
// 初始化消息各字段为0
TinyMessage(byte code = 0);
// 消息所占据的指令数据大小。包括头部、负载数据和附加数据
virtual uint Size() const;
virtual int Size() const;
// 数据缓冲区大小
virtual uint MaxDataSize() const;
virtual int MaxDataSize() const;
// 分析数据,转为消息。负载数据部分将指向数据区,外部不要提前释放内存
virtual bool Read(Stream& ms);

View File

@ -216,7 +216,7 @@ bool TokenController::OnReceive(Message& msg)
auto& tmsg = (TokenMessage&)msg;
// 过滤重复请求。1秒内不接收重复指令
UInt64 start = Sys.Ms() - 5000;
for (int i = 0; i < (int)ArrayLength(_RecvQueue); i++)
for (int i = 0; i < ArrayLength(_RecvQueue); i++)
{
auto& qi = _RecvQueue[i];
if (qi.Code == msg.Code && qi.Seq == tmsg.Seq && qi.Time > start)
@ -225,7 +225,7 @@ bool TokenController::OnReceive(Message& msg)
return true;
}
}
for (int i = 0; i < (int)ArrayLength(_RecvQueue); i++)
for (int i = 0; i < ArrayLength(_RecvQueue); i++)
{
auto& qi = _RecvQueue[i];
if (qi.Code == 0 || qi.Time <= start)
@ -314,7 +314,7 @@ void TokenController::ShowMessage(cstring action, const Message& msg)
#if MSG_DEBUG
TS("TokenController::ShowMessage");
for (int i = 0; i < (int)ArrayLength(NoLogCodes); i++)
for (int i = 0; i < ArrayLength(NoLogCodes); i++)
{
if (msg.Code == NoLogCodes[i]) return;
if (NoLogCodes[i] == 0) break;
@ -377,7 +377,7 @@ bool TokenController::StatSend(const Message& msg)
// 超时指令也干掉
UInt64 end = Sys.Ms() - 5000;
for (int i = 0; i < (int)ArrayLength(_StatQueue); i++)
for (int i = 0; i < ArrayLength(_StatQueue); i++)
{
auto& qi = _StatQueue[i];
if (qi.Code == 0 || qi.Time <= end)
@ -408,7 +408,7 @@ bool TokenController::StatReceive(const Message& msg)
if (msg.Reply)
{
bool rs = false;
for (int i = 0; i < (int)ArrayLength(_StatQueue); i++)
for (int i = 0; i < ArrayLength(_StatQueue); i++)
{
auto& qi = _StatQueue[i];
if (qi.Code == code)

View File

@ -73,12 +73,12 @@ bool TokenMessage::Valid() const
}
// 消息总长度,包括头部、负载数据和校验
uint TokenMessage::Size() const
int TokenMessage::Size() const
{
return HeaderSize + Length;
}
uint TokenMessage::MaxDataSize() const
int TokenMessage::MaxDataSize() const
{
return Data == _Data ? ArrayLength(_Data) : Length;
}

View File

@ -20,8 +20,8 @@ public:
byte _Data[512]; // 数据
static const uint HeaderSize = 1 + 1 + 1; // 消息头部大小
static const uint MinSize = HeaderSize + 0; // 最小消息大小
static const int HeaderSize = 1 + 1 + 1; // 消息头部大小
static const int MinSize = HeaderSize + 0; // 最小消息大小
// 使用指定功能码初始化令牌消息
TokenMessage(byte code = 0);
@ -32,9 +32,9 @@ public:
virtual void Write(Stream& ms) const;
// 消息总长度,包括头部、负载数据和校验
virtual uint Size() const;
virtual int Size() const;
// 数据缓冲区大小
virtual uint MaxDataSize() const;
virtual int MaxDataSize() const;
// 验证消息校验码是否有效
virtual bool Valid() const;

View File

@ -25,6 +25,7 @@ build.BuildLib("..\\libSmartOS_M3.a");
build.Debug = true;
// 未使用参数和无符号整数比较太多了,过滤掉
build.ExtCompiles = "-Wno-unused-parameter -Wno-sign-compare";
//build.ExtCompiles = "-Wno-unused-parameter -Wno-sign-compare";
build.ExtCompiles = "-Wno-unused-parameter";
build.CompileAll();
build.BuildLib("..\\libSmartOS_M3.a");