28. 钛极OS之常用工具

钛极OS之常用工具

 

 

  为了方便用户使用, TiJOS 提供了一些常用的工具类方便用户在应用中使用, 如日志,Delay,Formatter等等。

 

  Java包

  tijos.framework.util

 

包/类 说明
BigBitConverter Byte与Int,Long相互转换,大尾模式
LittleBitConverter Byte与Int, Long相互转换,小尾模式
Delay 延时,支持毫秒,微妙延时
Formatter 支持Double/float/byte[] 转字符串, 方便显示

  主要类说明:  
  BigBitConverter/LittleBitConverter

  由于Java不支持指针,无法象C语言一样方便地进行不同类型之间的内存转换, BigBitConverter/LittleBitConverter提供了方便用户使用的不同类型的数据转换为Byte数组的方法, 常用于一些通讯协议的数据传输过程中。

  BigBitConverter - 大端数据转换

  LittleBitConverter - 小端数据转换

  主要方法如下:

 

方法 说明
short ToInt16(byte[] bytes, int offset) 将数组中指定位置的2字节数据转换为short
int ToUInt16(byte[] bytes, int offset) 将数组中指定位置的2字节数据转换为int
int ToInt32(byte[] bytes, int offset) 将数组中指定位置的4字节数据转换为int
long ToUInt32(byte[] bytes, int offset) 将数组中指定位置的4字节数据转换为long
byte[] GetBytes(int value) 将int数据转换为4字节byte数组
byte[] GetBytes(short value) 将short数据转换为2字节byte数组


  用法例程:

 

  1. .....
  2. byte[] data = new byte[22];
  3. // read calibration data
  4. data[0] = (byte) 0xAA;
  5.  
  6. // set the eeprom pointer position to 0xAA
  7. // read 11 x 16 bits at this position
  8. bmp180i2c.read(BMP180_I2C_ADDRESS, 0XAA, data, 0, 22);
  9.  
  10. // store calibration data for further calculus
  11. //将数组中指定位置大端数据换为int
  12. ac1 = BigBitConverter.ToInt16(data, 0);
  13. ac2 = BigBitConverter.ToInt16(data, 2);
  14. ac3 = BigBitConverter.ToInt16(data, 4);
  15. ac4 = BigBitConverter.ToUInt16(data, 6);
  16. ac5 = BigBitConverter.ToUInt16(data, 8);
  17. ac6 = BigBitConverter.ToUInt16(data, 10);
  18.  
  19. b1 = BigBitConverter.ToInt16(data, 12);
  20. b2 = BigBitConverter.ToInt16(data, 14);
  21.  
  22. mc = BigBitConverter.ToInt16(data, 18);
  23. md = BigBitConverter.ToInt16(data, 20);
复制代码  Delay

  在硬件应用中经常用到延时功能,Delay类提供了毫秒和微妙级的延时功能,不能被中断。 比Java自带的Thread.Sleep更方便易用,建议用户在使用延时功能时使用该类提供的API。

 

方法 说明
void msDelay(long period) 按指定毫秒延时
void usDelay(long period) 按指定微秒延时
void nsDelay(long period) 按指定纳秒延时


  用法例程:

 

  1. Delay.msDelay(1000); //延时1秒
  2. Delay.usDelay(1); //延时1微妙
  3. Delay.nsDelay(1); //延时1纳秒
复制代码  
  Formatter 字符串转换

  生成数据格式化字符串,Formatter提供了常用的字符串转换函数,包括double/float 指定小数位长度, byte数组生成16进制字符串等等。

  Java自带的DecimalFormat需要占用更大的代码和内存空间,性能不是太好, 建议能满足需求的情况下使用Formatter提供的函数。

 

方法 说明
String format(double value, String pattern) 将double数据转成指定位数小数点的字符串,pattern支持"#.##"
String format(float value, String pattern) 将float数据转成指定位数小数点的字符串,pattern支持"#.##"
String toHexString(byte [] byteArray) 将byte数组转为16进制字符串
byte[] hexStringToByte(String str) 将16进制字符串转为byte数组


  用法例程:

 

  1. double val = 1.234;
  2. byte [] buff = new byte[] {1,2,3,4,5,6};
  3.  
  4. System.out.println(Formatter.format(val, ".##"));
  5. System.out.println(Formatter.toHexString(buff));
复制代码

 


  更多详细介绍请见钛云物联官网:www.tijos.net

你可能感兴趣的:([钛极OS专栏])