Array Usage collection

本文总结了JavaScript中数组的各种原型方法,包括遍历、查找、校验等操作,并介绍了如何创建新数组或将现有数组转换为其他类型。此外还讨论了修改源数组的方法及如何创建新数组而不影响原数组。

I'm so used to using array prototype methods because it makes my code simpler and more efficient. I could focus more on logic design or business service. What so many kinds of methods! Sometimes, we may be confused about when and how to use, or when the source array are secretly forced to be changed...

So I made a short summary here just for reference:

go through, find, check, create to be as another type one, create to be an array,

change source array and create a new array.

#Gthrough: in order to do other actions when going through an array

Methods
Comments
Updates in
forEach ES5
reduce ES5
reduceRightthe same as "reserve().reduce()"ES5

#Find: find in an array

Methods
Comments
Updates in
array.find(callback)return an item from an arrayES6
array.findIndex(callback)return index numberES6
array.indexOf(element[, fromIndex])return index numberES5
array.lastIndexOf(element[, fromIndex])return index number

ES5







#Check: check with a callback in an array

Methods
Comments
Updates in
array.every(callback)return true only when all elements in the array pass the callbackES5
array.includes(element[, fromIndex])return true only when an array includes a certain elementES6
array.some(callback)return true when at least one element in the array passes the callbackES5




#Create to be as another type one

Methods
Comments
Updates in
array.entries()

returns a new Array Iterator object that contains the key/value pairs for each index in the array.

ES6
array.keys()returns a new Array Iterator object that contains the keys for each index in the array.ES6
array.values()returns a new Array Iterator object that contains the values for each index in the array.ES6
array.join()joins all elements of an array (or an array-like object) into a string and returns this string.ES5
array.toString()same as array.join(',')before ES5
array.toLocalString()use toLocalString method of object, number, Date and then join all elements with ','before ES5

#Create to be an array from other array-like object or iterable object

Methods
Comments
Updates in
Array.from(array-like)

creates a new, shallow-copied Array instance from an array-like or iterable object.

ES6

Array.of(element0[...,[elementN]])

creates a new Array instance with a variable number of arguments, regardless of number or type of the arguments.

ES6

#Change source array

Methods
Comments
Updates in
array.splice(start[, deleteCount[, item1[, ...[,itemN]]]])remove existing elements and/or add new elementsbefore ES5

array.copyWithin(target[,start[,end]])

e.g. var a=[1,2,3]; a.copyWithin(1,2,3); //expected a: [1,3, 3]

ES6

array.fill(value[,start[,end]])

e.g. var a=[1,2,3]; a.fill(1,2,3); //expected a: [1,2,1]
ES6
array.pop()
before ES5
array.push(element1[, ...[,elementN]])Array.prototype.push.apply(array1, array2) equals array1 = array1.concat(array2);before ES5
array.sort(callback)
before ES5

array.reverse()

e.g. var a=["a", "c", "d"]; a.reverse(); //expected a: ["d", "c", "a"]

before ES5

array.shift()e.g. var a=["a", "c", "d"]; a.shift(); //expected a: ["c", "d"]before ES5
array.unshift()

e.g. var a=["a", "c", "d"]; a.unshift("e","f"); //expected a: ["e", "f", "a", "c", "d"]

before ES5














#Create a new array and don't affect source array

Methods
Comments
Updates in
array1.concat(array2)
before ES5
array.filter(callback)
ES5
array.map(callback)
ES5
array.flatMap(callback)
experimental API
array.flat(depth)
experimental API
array.slice(begin[, end])e.g. var a=[1,2,3]; var b = a.slice(1); //expected b: [2,3]before ES5









//************mouse buttons+sensor+wheel**********************// 0x05, 0x01, // Usage Page (Generic Desktop) 0x09, 0x02, // Usage (Mouse) 0xA1, 0x01, // Collection (Application) 0x85, 0x01, 0x09, 0x01, 0xA1, 0x00, // Usage (Pointer) 0x05, 0x09, // Collection (Physical) 0x19, 0x01, 0x29, 0x05, // Usage Page (Buttons) 0x15, 0x00, // Usage Minimum (1 buttons) 0x25, 0x01, // Usage Maximum (6 buttons) 0x75, 0x01, // Logic Minimum (0) 0x95, 0x05, // Logic Maximum (1) 0x81, 0x02, 0x75, 0x01, // Report Size (1bit) 0x95, 0x03, // Report Count (6) 0x81, 0x03, // Input (Data, Variable, Absolute) - Button Byte 0x05, 0x01, 0x09, 0x38, // Usage Page (Reserved) 0x15, 0x81, // Report Size (1bit) 0x25, 0x7F, // Report Count (2) 0x75, 0x08, // Input (Constant, Variable, Absolute) - Button Padding 0x95, 0x01, 0x81, 0x06, // Usage (X, Y) 0x09, 0x30, // Report Size (8bits) 0x09, 0x31, // Report Count (2) 0x16, 0x00, 0x80, // Logical Minimum (-127) 0x26, 0xFF, 0x7F, // Logical Maximum (+127) 0x75, 0x10, // Input (Data, Variable, Absolute) - X, Y Bytes 0x95, 0x02, 0x81, 0x06, // Usage Page (Generic Desktop) 0x05, 0x0C, // Usage (Scroll Wheel) 0x0A, 0x38, 0x02, // Logical Minimum (-127) 0x15, 0x81, // Logical Maximum (+127) 0x25, 0x7F, // Report Size (8) 0x75, 0x08, // Report Count (1) 0x95, 0x01, // Input (Data, Variable, Relative) - Z Bytes 0x81, 0x06, // Usage (X) 0x09, 0x00, // Usage (Y) 0x15, 0x00, // Logical Minimum (-32767) 0x25, 0x01, // Logical Maximum (+32767) 0x75, 0x08, // Report Size (16) 0x95, 0x01, // Report Count (2) 0x81, 0x06, // Input (Data, Variable, Relative) - X, Y Words 0xC0, // End Collection (Physical) 0x09, 0x05, // Usage (Vendor Defined) 0x15, 0x00, // Logical Minimum (0) 0x26, 0xFF, 0x00, // Logical Maximum (255) 0x75, 0x08, // Report Size (8 bit) 0x95, 0x40, // Report Count (2) 0xB1, 0x02, // Feature (Data, Variable, Absolute) 0xC0, // End Collection (Application) 0x05, 0x01, // Usage Page (Generic Desktop) 0x09, 0x06, // Usage (Keyboard) 0xA1, 0x01, // Collection (Application) 0x85, 0x02, 0x05, 0x07, // Usage Page (Key Codes) 0x19, 0xe0, // Usage Minimum (224) 0x29, 0xe7, // Usage Maximum (231) 0x15, 0x00, // Logical Minimum (0) 0x25, 0x01, // Logical Maximum (1) 0x75, 0x01, // Report Size (1) 0x95, 0x08, // Report Count (8) 0x81, 0x02, // Input (Data, Variable, Absolute) 0x05,0x07, //; Usage Page(Key Codes) 6 bytes for keycode 0x19,0x00, //; Usage Min(00) 0x2A,0xFF,0x00, //; Usage Max(0x00FF) 0x15,0x00, //; Logical Min(0) 0x26,0xFF,0x00, //; Logical Max(0x00FF) 0x75,0x08, //; Report size(8) 0x95,0x06, //; Report count(6) 0x81,0x00, //; Output(Data, Array, Absolute) 0xC0, //;End Collection //************Consumer**********************// 0x05, 0x0C, // Usage Page (Consumer) 0x09, 0x01, // Usage (Consumer) 0xA1, 0x01, // Collection (Application) 0x85, 0x03, // Report ID of Multimedia keys - Record ID, 1-byte (0x02) 0x19, 0x00, // Usage Min (0x0000) 0x2A, 0x3C, 0x02, // Usage Max (0x023C) 0x15, 0x00, // Logical Min (0x0000) 0x26, 0x3C, 0x02, // Logical Max (0x023C) 0x95, 0x01, // Report Count (1) 0x75, 0x10, // Report Size (16) 0x81, 0x00, // Input (Data, Array, Absolute) - Media Key, 1-word 0x95, 0x05, // Report Count (13) 0x75, 0x08, // Report Size (8) 0x81, 0x01, // Input (Constant, Array, Absolute) - Packet Padding, 5-byte 0xC0, // End Collection 0x05, 0x01, // Usage Page (Generic desktop) 0x09, 0x80, // Usage (System Control) 0xA1, 0x01, // Collection (Application) 0x85, 0x04, // Report ID of Power keys - Record ID, 1-byte (0x03) 0x19, 0x81, // Usage Min (0x81) 0x29, 0x83, // Usage Max (0x83) 0x15, 0x00, // Logical Min (0) 0x25, 0x01, // Logical Max (1) 0x75, 0x01, // Report Size (1) 0x95, 0x03, // Report Count (3) 0x81, 0x02, // Input (Data, Variable, Absolute) - Power Keys, 3-bits 0x95, 0x05, // Report Count (5) 0x81, 0x01, // Input (Constant, Array, Absolute) - Byte Padding, 5-bits 0x75, 0x07, // Report Size (8) 0x95, 0x08, // Report Count (14) 0x81, 0x01, // Input (Constant, Array, Absolute) - Packet Padding, 6-bytes 0xC0, // End_Collection 0x06, 0xA0, 0xFF, // 0x09, 0x01, // 0xA1, 0x01, // 0x85, 0x05, // 0x09, 0x01, // 0x15, 0x00, // 0x26, 0xFF, 0x00, // 0x75, 0x08, // 0x95, 0x08, // 0x81, 0x02, // 0xC0, // 0x06, 0xFF, 0xFF, // 0x09, 0x01, // 0xA1, 0x01, // 0x85, 0x06, // 0x09, 0x01, // 0x15, 0x00, // 0x26, 0xFF, 0x00, // 0x75, 0x08, // 0x95, 0x08, // 0x81, 0x02, // 0xc0,
07-12
uint8_t hid_report_desc[]={ 0x05, 0x01, // Usage Page (Generic Desktop) 0x09, 0x05, // Usage (Game Pad) 0xa1, 0x01, // Collection (Application) 0x15, 0x00, // Logical minimum (0) 0x25, 0x01, // Logical maximum (1) 0x35, 0x00, // Physical minimum (0) 0x45, 0x01, // Physical maximum (1) 0x75, 0x01, // Report Size (1) 0x95, 0x0e, // Report Count (14) 0x05, 0x09, // Usage Page (Button) 0x19, 0x01, // Usage Minimum (Button 1) 0x29, 0x0e, // Usage Maximum (Button 14) 0x81, 0x02, // Input (Data,Value,Absolute,Bit Field) 0x95, 0x02, // Report Count (2) 0x81, 0x01, // Input (Constant,Array,Absolute,Bit Field) 0x05, 0x01, // Usage Page (Generic Desktop) 0x25, 0x07, // Logical maximum (7) 0x46, 0x3b, 0x01, // Physical maximum (315) 0x75, 0x04, // Report Size (4) 0x95, 0x01, // Report Count (1) 0x65, 0x14, // Unit (English Rotation: Angular Position [degrees]) 0x09, 0x39, // Usage (Hat switch) 0x81, 0x42, // Input (Data,Value,Absolute,Bit Field) 0x65, 0x00, // Unit (None) 0x95, 0x01, // Report Count (1) 0x81, 0x01, // Input (Constant,Array,Absolute,Bit Field) 0x26, 0xff, 0x00, // Logical maximum (255) 0x46, 0xff, 0x00, // Physical maximum (-1) 0x09, 0x30, // Usage (X) 0x09, 0x31, // Usage (Y) 0x09, 0x32, // Usage (Z) 0x09, 0x35, // Usage (Rz) 0x75, 0x08, // Report Size (8) 0x95, 0x04, // Report Count (4) 0x81, 0x02, // Input (Data,Value,Absolute,Bit Field) 0x75, 0x08, // Report Size (8) 0x95, 0x01, // Report Count (1) 0x81, 0x01, // Input (Constant,Array,Absolute,Bit Field) // New Axes Definitions 0x09, 0x33, // USAGE (Rx Axis) 0x09, 0x34, // USAGE (Ry Axis) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x26, 0xFF, 0x00, // LOGICAL_MAXIMUM (255) 0x75, 0x08, // REPORT_SIZE (8 bits) 0x95, 0x02, // REPORT_COUNT (2 new axes) 0x81, 0x02, // INPUT (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) // Example of extending the report descriptor with a custom 38-byte field. // 0x05, 0xFF, // Usage Page (Vendor Defined) 0x09, 0x20, // Usage (Custom Data Field ID 1) // 0xA1, 0x01, // Collection (Application) 0x75, 0x08, // Report Size (8 bits per logical value) 0x95, 0x0e, // Report Count (14 values total) 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) 0xc0, // End Collection }; 解析这段报告描述符
05-21
<think>嗯,用户问的是USB HID Collection的概念和工作原理。首先,我需要回忆一下USB HID的相关知识。根据之前提供的引用内容,USB HID协议不需要安装驱动,这点在引用1中提到过。不过用户的问题更具体,关于Collection这个概念。 首先,HID设备使用报告描述符来描述数据格式,而Collection应该是报告描述符中的一个结构。记得在USB规范中,报告描述符通过不同的项(item)来组织数据,比如用法页(Usage Page)、逻辑最小值(Logical Minimum)等。而Collection可能是用来将相关的数据项组合在一起的,比如一个鼠标可能有多个按钮和移动轴,这些会被组织在一个Collection里。 然后,用户想知道Collection如何工作。可能需要解释Collection的类型,比如应用集合(Application)、物理集合(Physical)等。不同的类型有不同的用途,比如应用集合通常表示一个功能整体,物理集合可能对应物理上的部件组合。同时,在报告描述符中,Collection的开始和结束会有对应的标记,比如0xA1和0xC0。 另外,用户可能是在开发HID设备时遇到了配置问题,比如在STM32或者Qualcomm平台上,如何正确设置报告描述符中的Collection结构。引用3提到HID的速度问题可能与端点大小有关,但这里的问题更偏向数据结构。引用4提到了Android作为HID设备的配置,可能需要使用Collection来组织不同的数据用途。 需要确保回答的结构清晰,先定义Collection,再说明其作用,然后举例说明,比如鼠标的例子。最后,结合引用的资料,可能提到配置时的注意事项,比如在STM32例程中的端点大小设置,或者Android HID配置中的相关编译选项,但需要明确Collection是报告描述符的一部分,和这些配置的关系可能不大,但可以适当关联。 还要生成相关问题,可能需要围绕HID的结构、报告描述符的编写、不同集合类型的区别,以及实际应用中的配置问题来提问。</think>### USB HID Collection 详解 #### 1. 基本概念 **USB HID Collection** 是描述设备功能分组的逻辑结构,通过报告描述符(Report Descriptor)定义。它将具有相关功能的数据项(如按钮、传感器、控制轴)组织为统一的操作单元。例如: - 鼠标的按键和滚轮可组成一个集合 - 游戏手柄的摇杆和按钮可组成另一个集合 HID Collection 的层级关系可表示为: $$ \text{Report Descriptor} \rightarrow \text{Collection} \rightarrow \text{Data Fields} $$ #### 2. 工作流程 1. **设备枚举**:主机通过HID描述符识别设备类型[^1] 2. **报告解析**:根据Collection定义的数据结构解析输入/输出报告 3. **数据分组**:同一Collection内的数据字段共享传输协议和用途定义 #### 3. 核心类型 | 类型值 | 名称 | 应用场景 | |--------|---------------|------------------------------| | 0x00 | Physical | 物理关联的部件(如键盘按键) | | 0x01 | Application | 功能逻辑分组(如鼠标设备) | | 0x02 | Logical | 数据逻辑关联 | | 0x03 | Report | 报告格式容器 | | 0x04 | Named Array | 预定义值集合 | | 0x05 | Usage Switch | 动态功能切换 | | 0x06 | Usage Modifier| 功能修饰符 | #### 4. 实例解析(鼠标描述符片段) ```c 0x05, 0x01, // Usage Page (Generic Desktop) 0x09, 0x02, // Usage (Mouse) 0xA1, 0x01, // Collection (Application) 0x09, 0x01, // Usage (Pointer) 0xA1, 0x00, // Collection (Physical) 0x05, 0x09, // Usage Page (Buttons) 0x19, 0x01, // Usage Minimum (1) 0x29, 0x03, // Usage Maximum (3) 0x15, 0x00, // Logical Minimum (0) 0x25, 0x01, // Logical Maximum (1) 0x95, 0x03, // Report Count (3) 0x75, 0x01, // Report Size (1) 0x81, 0x02, // Input (Data,Var,Abs) 0xC0, // End Collection 0xC0 // End Collection ``` 此描述符构建了双层嵌套结构,实现了按钮与移动控制的逻辑分组[^2]。 #### 5. 开发注意事项 1. 端点缓冲区大小需匹配最大报告长度(参考CUSTOM_HID_EPIN_SIZE配置[^3]) 2. Android系统实现需启用HID配置选项: ```makefile CONFIG_USB_CONFIGFS_F_HID=y CONFIG_USB_G_HID=y ``` 如引用4所示[^4]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值