ldq.cpp
Go to the documentation of this file.
1 //
2 // The MIT License (MIT)
3 //
4 // Copyright (c) 2019 Livox. All rights reserved.
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 //
24 
25 #include "ldq.h"
26 
27 #include <stdio.h>
28 #include <string.h>
29 
30 namespace livox_ros {
31 
32 /* for pointcloud queue process */
33 int InitQueue(LidarDataQueue *queue, uint32_t queue_size) {
34  if (queue == nullptr) {
35  return 1;
36  }
37 
38  if (IsPowerOf2(queue_size) != true) {
39  queue_size = RoundupPowerOf2(queue_size);
40  }
41 
42  queue->storage_packet = new StoragePacket[queue_size];
43  if (queue->storage_packet == nullptr) {
44  return 1;
45  }
46 
47  queue->rd_idx = 0;
48  queue->wr_idx = 0;
49  queue->size = queue_size;
50  queue->mask = queue_size - 1;
51 
52  return 0;
53 }
54 
56  if (queue == nullptr) {
57  return 1;
58  }
59 
60  if (queue->storage_packet) {
61  delete[] queue->storage_packet;
62  }
63 
64  queue->rd_idx = 0;
65  queue->wr_idx = 0;
66  queue->size = 0;
67  queue->mask = 0;
68 
69  return 0;
70 }
71 
72 void ResetQueue(LidarDataQueue *queue) {
73  queue->rd_idx = 0;
74  queue->wr_idx = 0;
75 }
76 
77 void QueuePrePop(LidarDataQueue *queue, StoragePacket *storage_packet) {
78  uint32_t rd_idx = queue->rd_idx & queue->mask;
79 
80  memcpy(storage_packet, &(queue->storage_packet[rd_idx]),
81  sizeof(StoragePacket));
82 }
83 
84 void QueuePopUpdate(LidarDataQueue *queue) { queue->rd_idx++; }
85 
86 uint32_t QueuePop(LidarDataQueue *queue, StoragePacket *storage_packet) {
87  QueuePrePop(queue, storage_packet);
88  QueuePopUpdate(queue);
89 
90  return 1;
91 }
92 
94  return queue->wr_idx - queue->rd_idx;
95 }
96 
98  return (queue->size - (queue->wr_idx - queue->rd_idx));
99 }
100 
102  return ((queue->wr_idx - queue->rd_idx) > queue->mask);
103 }
104 
106  return (queue->rd_idx == queue->wr_idx);
107 }
108 
109 uint32_t QueuePush(LidarDataQueue *queue, StoragePacket *storage_packet) {
110  uint32_t wr_idx = queue->wr_idx & queue->mask;
111 
112  memcpy((void *)(&(queue->storage_packet[wr_idx])), (void *)(storage_packet),
113  sizeof(StoragePacket));
114 
115  queue->wr_idx++;
116 
117  return 1;
118 }
119 
121  uint64_t time_rcv, uint32_t point_num) {
122  uint32_t wr_idx = queue->wr_idx & queue->mask;
123 
124  queue->storage_packet[wr_idx].time_rcv = time_rcv;
125  queue->storage_packet[wr_idx].point_num = point_num;
126  memcpy(queue->storage_packet[wr_idx].raw_data, data, length);
127 
128  queue->wr_idx++;
129 
130  return 1;
131 }
132 
133 } // namespace livox_ros
static uint32_t RoundupPowerOf2(uint32_t size)
Definition: ldq.h:58
void QueuePrePop(LidarDataQueue *queue, StoragePacket *storage_packet)
Definition: ldq.cpp:77
uint8_t raw_data[KEthPacketMaxLength]
Definition: ldq.h:41
void ResetQueue(LidarDataQueue *queue)
Definition: ldq.cpp:72
uint32_t QueueIsFull(LidarDataQueue *queue)
Definition: ldq.cpp:101
unsigned char uint8_t
Definition: stdint.h:125
uint32_t QueueUnusedSize(LidarDataQueue *queue)
Definition: ldq.cpp:97
unsigned int uint32_t
Definition: stdint.h:127
uint32_t QueuePushAny(LidarDataQueue *queue, uint8_t *data, uint32_t length, uint64_t time_rcv, uint32_t point_num)
Definition: ldq.cpp:120
unsigned __int64 uint64_t
Definition: stdint.h:137
uint32_t point_num
Definition: ldq.h:40
uint32_t QueueUsedSize(LidarDataQueue *queue)
Definition: ldq.cpp:93
uint64_t time_rcv
Definition: ldq.h:39
int InitQueue(LidarDataQueue *queue, uint32_t queue_size)
Definition: ldq.cpp:33
static bool IsPowerOf2(uint32_t size)
Definition: ldq.h:54
StoragePacket * storage_packet
Definition: ldq.h:47
uint32_t QueueIsEmpty(LidarDataQueue *queue)
Definition: ldq.cpp:105
volatile uint32_t rd_idx
Definition: ldq.h:48
void QueuePopUpdate(LidarDataQueue *queue)
Definition: ldq.cpp:84
uint32_t QueuePush(LidarDataQueue *queue, StoragePacket *storage_packet)
Definition: ldq.cpp:109
int DeInitQueue(LidarDataQueue *queue)
Definition: ldq.cpp:55
volatile uint32_t wr_idx
Definition: ldq.h:49
uint32_t QueuePop(LidarDataQueue *queue, StoragePacket *storage_packet)
Definition: ldq.cpp:86


livox_ros_driver
Author(s): Livox Dev Team
autogenerated on Mon Mar 15 2021 02:40:46