keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.car; |
| 18 | |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 19 | import android.car.hardware.CarSensorEvent; |
Steve Paik | 289ab99 | 2017-07-11 22:40:57 -0700 | [diff] [blame] | 20 | import android.hardware.automotive.vehicle.V2_0.VehiclePropValue; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 21 | |
Steve Paik | 360b7a9 | 2018-05-01 20:13:08 -0700 | [diff] [blame] | 22 | import java.util.List; |
| 23 | |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 24 | //TODO add memory pool and recycling |
| 25 | public class CarSensorEventFactory { |
| 26 | |
Jason Tholstrup | d72b535 | 2016-09-22 16:32:14 -0700 | [diff] [blame] | 27 | public static CarSensorEvent createBooleanEvent(int sensorType, long timestamp, |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 28 | boolean value) { |
Steve Paik | 289ab99 | 2017-07-11 22:40:57 -0700 | [diff] [blame] | 29 | CarSensorEvent event = new CarSensorEvent(sensorType, timestamp, 0, 1, 0); |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 30 | event.intValues[0] = value ? 1 : 0; |
| 31 | return event; |
| 32 | } |
| 33 | |
Jason Tholstrup | d72b535 | 2016-09-22 16:32:14 -0700 | [diff] [blame] | 34 | public static CarSensorEvent createIntEvent(int sensorType, long timestamp, int value) { |
Steve Paik | 289ab99 | 2017-07-11 22:40:57 -0700 | [diff] [blame] | 35 | CarSensorEvent event = new CarSensorEvent(sensorType, timestamp, 0, 1, 0); |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 36 | event.intValues[0] = value; |
| 37 | return event; |
| 38 | } |
| 39 | |
Steve Paik | 360b7a9 | 2018-05-01 20:13:08 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Create int64 vector event |
| 42 | * @param sensorType |
| 43 | * @param timestamp |
| 44 | * @param value |
| 45 | * |
| 46 | * @return CarSensorEvent |
| 47 | */ |
| 48 | public static CarSensorEvent createInt64VecEvent(int sensorType, long timestamp, |
| 49 | List<Long> value) { |
| 50 | CarSensorEvent event = new CarSensorEvent(sensorType, timestamp, 0, 0, value.size()); |
| 51 | for (int i = 0; i < value.size(); i++) { |
| 52 | event.longValues[i] = value.get(i); |
| 53 | } |
| 54 | return event; |
| 55 | } |
| 56 | |
Jason Tholstrup | d72b535 | 2016-09-22 16:32:14 -0700 | [diff] [blame] | 57 | public static CarSensorEvent createFloatEvent(int sensorType, long timestamp, float value) { |
Steve Paik | 289ab99 | 2017-07-11 22:40:57 -0700 | [diff] [blame] | 58 | CarSensorEvent event = new CarSensorEvent(sensorType, timestamp, 1, 0, 0); |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 59 | event.floatValues[0] = value; |
| 60 | return event; |
| 61 | } |
| 62 | |
Steve Paik | 382ceee | 2018-01-22 19:21:44 -0800 | [diff] [blame] | 63 | public static CarSensorEvent createMixedEvent(int sensorType, long timestamp, |
Steve Paik | 289ab99 | 2017-07-11 22:40:57 -0700 | [diff] [blame] | 64 | VehiclePropValue v) { |
| 65 | int numFloats = v.value.floatValues.size(); |
| 66 | int numInts = v.value.int32Values.size(); |
| 67 | int numLongs = v.value.int64Values.size(); |
| 68 | CarSensorEvent event = new CarSensorEvent(sensorType, timestamp, numFloats, numInts, |
| 69 | numLongs); |
| 70 | // Copy arraylist elements into final arrays |
| 71 | for (int i=0; i<numFloats; i++) { |
| 72 | event.floatValues[i] = v.value.floatValues.get(i); |
| 73 | } |
| 74 | for (int i=0; i<numInts; i++) { |
| 75 | event.intValues[i] = v.value.int32Values.get(i); |
| 76 | } |
| 77 | for (int i=0; i<numLongs; i++) { |
| 78 | event.longValues[i] = v.value.int64Values.get(i); |
| 79 | } |
| 80 | return event; |
| 81 | } |
| 82 | |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 83 | public static void returnToPool(CarSensorEvent event) { |
| 84 | //TODO |
| 85 | } |
| 86 | } |