aboutsummaryrefslogtreecommitdiffstats
path: root/HAL-afb/HAL-interface/hal-volramp.c
blob: 6343a06f5b0711d05bb9b032a8cbb49a66bd902f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (C) 2016 "IoT.bzh"
 * Author Fulup Ar Foll <fulup@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

#define _GNU_SOURCE  // needed for vasprintf
#include "hal-interface.h"

STATIC int RampTimerCB (sd_event_source* source,uint64_t timer, void* handle) {
   halVolRampT *volRamp= (halVolRampT*)handle;
   int err;
   uint64_t usec;

    // RampDown
    if (volRamp->current > volRamp->target) {
       volRamp->current =  volRamp->current - volRamp->stepDown;
       if (volRamp->current < volRamp->target) volRamp->current = volRamp->target;
    }
    
    // RampUp
    if (volRamp->current < volRamp->target) {
       volRamp->current =  volRamp->current + volRamp->stepUp;
       if (volRamp->current > volRamp->target) volRamp->current = volRamp->target;
    }
    
    // request current Volume Level 
    err = halSetCtlByTag(volRamp->slave, volRamp->current);
    if (err)  goto OnErrorExit;

    // we reach target stop volram event
    if (volRamp->current == volRamp->target) sd_event_source_unref(source);
    else {
        // otherwise validate timer for a new run    
        sd_event_now(afb_daemon_get_event_loop(), CLOCK_MONOTONIC, &usec);
        sd_event_source_set_enabled(source, SD_EVENT_ONESHOT);
        err=sd_event_source_set_time(source, usec+volRamp->delay);
    }

    return 0;
    
  OnErrorExit:
    AFB_WARNING ("RampTimerCB Fail to set HAL ctl tag=%d vol=%d", Master_Playback_Volume, volRamp->current);
    sd_event_source_unref(source); // abandon VolRamp
    return -1;
}

/* UCS2 Interface Timer Callback */
STATIC void SetRampTimer(void *handle) {
   halVolRampT *volRamp= (halVolRampT*)handle;
   
   uint64_t usec;
   /* set a timer with  250ms accuracy */
   sd_event_now(afb_daemon_get_event_loop(), CLOCK_MONOTONIC, &usec);
   sd_event_add_time(afb_daemon_get_event_loop(), &volRamp->evtsrc, CLOCK_MONOTONIC, usec, 250, RampTimerCB, volRamp);
}

STATIC int volumeDoRamp (halVolRampT *volRamp,  int numid, json_object *volumeJ) {
    json_object *responseJ;
    
    // request current Volume Level 
    responseJ = halGetCtlByTag(volRamp->slave);
    if (!responseJ) {
        AFB_WARNING ("volumeDoRamp Fail to get HAL ctl tag=%d", Master_Playback_Volume);
        goto OnErrorExit;
    }    
    
    // use 1st volume value as target for ramping
    switch (json_object_get_type(volumeJ)) {
        case json_type_array:
            volRamp->target=json_object_get_int(json_object_array_get_idx(volumeJ,0));
            break;
           
        case json_type_int:
            volRamp->target=json_object_get_int(volumeJ);
            break;
        
        default:
            AFB_WARNING ("volumeDoRamp Invalid volumeJ=%s", json_object_get_string(volumeJ));
            goto OnErrorExit;
    }
    
    // use 1st volume value as current for ramping
    switch (json_object_get_type(responseJ)) {
        case json_type_array:
            volRamp->current=json_object_get_int(json_object_array_get_idx(responseJ,0));
            break;
           
        case json_type_int:
            volRamp->current=json_object_get_int(responseJ);
            break;
        
        default:
            AFB_WARNING ("volumeDoRamp Invalid reponseJ=%s", json_object_get_string(responseJ));
            goto OnErrorExit;
    }
    
    SetRampTimer (volRamp);
    
    return 0;

  OnErrorExit:
    return -1;
}


PUBLIC void volumeRamp (halCtlsEnumT halTag, alsaHalCtlMapT *ctl, void* handle, json_object *valJ) {
    halVolRampT *volRamp = (halVolRampT*) handle;
    json_object *tmpJ;

    if (json_object_get_type(valJ) != json_type_array || volRamp==NULL) goto OnErrorExit;
    
    switch (halTag) {
        case Vol_Ramp:
            tmpJ = json_object_array_get_idx(valJ, 0);            
            volumeDoRamp (volRamp, ctl->numid, tmpJ);
            break;
            
        case Vol_Ramp_Set_Mode:
            tmpJ = json_object_array_get_idx(valJ, 0);            
            volRamp->mode = json_object_get_int (tmpJ);
            break;
            
        case Vol_Ramp_Set_Slave:
            tmpJ = json_object_array_get_idx(valJ, 0);            
            volRamp->slave = json_object_get_int (tmpJ);
            break;
        
        case Vol_Ramp_Set_Delay:
            tmpJ = json_object_array_get_idx(valJ, 0);            
            volRamp->delay = 1000*json_object_get_int (tmpJ);
            break;

        case Vol_Ramp_Set_Down:
            tmpJ = json_object_array_get_idx(valJ, 0);            
            volRamp->stepDown = json_object_get_int (tmpJ);
            break;

        case Vol_Ramp_Set_Up:
            tmpJ = json_object_array_get_idx(valJ, 0);            
            volRamp->stepUp = json_object_get_int (tmpJ);
            break;

        default:
            goto OnErrorExit;
    }
    
    return;
    
    OnErrorExit:
        AFB_WARNING ("Invalid Ctrl Event halCtlsEnumT=%d numid=%d name=%s value=%s", halTag, ctl->numid, ctl->name, json_object_get_string(valJ));
        return;
}