1 /* 2 * Copyright (C) 2024 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 #pragma once 17 18 #include <aidl/android/hardware/vibrator/BnVibrator.h> 19 20 namespace aidl { 21 namespace android { 22 namespace hardware { 23 namespace vibrator { 24 25 constexpr int32_t COMPOSE_PWLE_PRIMITIVE_DURATION_MAX_MS = 16383; 26 27 constexpr uint32_t WT_LEN_CALCD = 0x00800000; 28 constexpr uint8_t PWLE_CHIRP_BIT = 0x8; // Dynamic/frequency and voltage 29 constexpr uint8_t PWLE_BRAKE_BIT = 0x4; 30 constexpr uint8_t PWLE_AMP_REG_BIT = 0x2; 31 32 static constexpr uint8_t PWLE_WT_TYPE = 12; 33 static constexpr uint8_t PWLE_HEADER_WORD_COUNT = 3; 34 static constexpr uint8_t PWLE_HEADER_FTR_SHIFT = 8; 35 static constexpr uint8_t PWLE_SVC_METADATA_WORD_COUNT = 3; 36 static constexpr uint32_t PWLE_SVC_METADATA_TERMINATOR = 0xFFFFFF; 37 static constexpr uint8_t PWLE_SEGMENT_WORD_COUNT = 2; 38 static constexpr uint8_t PWLE_HEADER_WCOUNT_WORD_OFFSET = 2; 39 static constexpr uint8_t PWLE_WORD_SIZE = sizeof(uint32_t); 40 41 static constexpr uint8_t PWLE_SVC_NO_BRAKING = -1; 42 static constexpr uint8_t PWLE_SVC_CAT_BRAKING = 0; 43 static constexpr uint8_t PWLE_SVC_OPEN_BRAKING = 1; 44 static constexpr uint8_t PWLE_SVC_CLOSED_BRAKING = 2; 45 static constexpr uint8_t PWLE_SVC_MIXED_BRAKING = 3; 46 47 static constexpr uint32_t PWLE_SVC_MAX_BRAKING_TIME_MS = 1000; 48 49 static constexpr uint8_t PWLE_FTR_BUZZ_BIT = 0x80; 50 static constexpr uint8_t PWLE_FTR_CLICK_BIT = 0x00; 51 static constexpr uint8_t PWLE_FTR_DYNAMIC_F0_BIT = 0x10; 52 static constexpr uint8_t PWLE_FTR_SVC_METADATA_BIT = 0x04; 53 static constexpr uint8_t PWLE_FTR_DVL_BIT = 0x02; 54 static constexpr uint8_t PWLE_FTR_LF0T_BIT = 0x01; 55 56 constexpr float CS40L26_PWLE_LEVEL_MIN = -1.0; 57 constexpr float CS40L26_PWLE_LEVEL_MAX = 0.9995118; 58 59 constexpr float PWLE_FREQUENCY_MIN_HZ = 30.0f; 60 constexpr float PWLE_FREQUENCY_MAX_HZ = 300.0f; 61 62 /* nsections is 8 bits. Need to preserve 1 section for the first delay before the first effect. */ 63 static constexpr int32_t COMPOSE_SIZE_MAX = 254; 64 static constexpr int32_t COMPOSE_PWLE_SIZE_MAX_DEFAULT = 127; 65 66 class DspMemChunk { 67 public: 68 DspMemChunk(uint8_t type, size_t size); 69 front()70 uint8_t *front() const { return head.get(); } type()71 uint8_t type() const { return waveformType; } size()72 size_t size() const { return bytes; } 73 74 int flush(); 75 76 int constructComposeSegment(uint32_t effectVolLevel, uint32_t effectIndex, uint8_t repeat, 77 uint8_t flags, uint16_t nextEffectDelay); 78 int constructActiveSegment(int duration, float amplitude, float frequency, bool chirp); 79 int constructBrakingSegment(int duration, Braking brakingType); 80 81 int updateWLength(uint32_t totalDuration); 82 int updateNSection(int segmentIdx); 83 int updateWCount(int segmentCount); 84 85 private: 86 std::unique_ptr<uint8_t[]> head; 87 size_t bytes = 0; 88 uint8_t waveformType; 89 uint8_t *_current; 90 const uint8_t *_max; 91 uint32_t _cache = 0; 92 int _cachebits = 0; 93 isEnd()94 bool isEnd() const { return _current == _max; } min(int x,int y)95 int min(int x, int y) { return x < y ? x : y; } 96 97 int write(int nbits, uint32_t val); 98 99 int fToU16(float input, uint16_t *output, float scale, float min, float max); 100 101 void constructPwleSegment(uint16_t delay, uint16_t amplitude, uint16_t frequency, uint8_t flags, 102 uint32_t vbemfTarget = 0); 103 }; 104 105 } // namespace vibrator 106 } // namespace hardware 107 } // namespace android 108 } // namespace aidl 109