@dotpi/tools 2.6.1 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. package/.history/dotpi_root/dist/@dotpi/javascript/index_20250224110150.js +0 -0
  2. package/.history/dotpi_root/dist/@dotpi/javascript/index_20250429123336.js +6 -0
  3. package/.history/dotpi_root/dist/@dotpi/javascript/package_20250224111859.json +15 -0
  4. package/.history/dotpi_root/dist/@dotpi/javascript/package_20250429101520.json +16 -0
  5. package/.history/dotpi_root/share/dotpi_functions/dotpi_functions_audio_device_20250117162542.bash +556 -0
  6. package/.history/dotpi_root/share/dotpi_functions/dotpi_functions_audio_device_20250428182930.bash +556 -0
  7. package/.history/dotpi_root/share/dotpi_functions/dotpi_functions_audio_device_20250428183019.bash +561 -0
  8. package/.history/dotpi_root/share/dotpi_functions/dotpi_functions_audio_device_20250428183434.bash +563 -0
  9. package/.history/dotpi_root/share/dotpi_functions/dotpi_functions_audio_device_20250428183737.bash +563 -0
  10. package/.history/dotpi_root/share/dotpi_functions/dotpi_functions_audio_device_20250428183826.bash +563 -0
  11. package/.history/test/utils.spec_20241114125309.js +48 -0
  12. package/.history/test/utils.spec_20250428183107.js +50 -0
  13. package/.history/test/utils.spec_20250428183349.js +50 -0
  14. package/.history/test/utils.spec_20250428183620.js +49 -0
  15. package/.history/test/utils.spec_20250428183631.js +50 -0
  16. package/.history/test/utils.spec_20250428183753.js +50 -0
  17. package/.history/test/utils.spec_20250428183813.js +50 -0
  18. package/dotpi_root/dist/@dotpi/javascript/LICENSE +28 -0
  19. package/dotpi_root/dist/@dotpi/javascript/index.js +6 -0
  20. package/dotpi_root/dist/@dotpi/javascript/package.json +7 -2
  21. package/dotpi_root/dist/@dotpi/module/LICENSE +28 -0
  22. package/dotpi_root/dist/@dotpi/module/package.json +4 -3
  23. package/dotpi_root/dist/@dotpi/module-module/package.json +3 -3
  24. package/dotpi_root/etc/dotpi_modules.json +2 -1
  25. package/dotpi_root/share/dotpi_functions/dotpi_functions_audio_device.bash +25 -18
  26. package/package.json +3 -3
  27. package/test/utils.spec.js +15 -13
@@ -0,0 +1,6 @@
1
+ export * as bash from './bash.js';
2
+ export * as echo from './echo.js';
3
+ export * as service from './service.js';
4
+ export * as system from './system.js';
5
+ export * as template from './template.js';
6
+ export * as user from './user.js';
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@dotpi/javascript",
3
+ "version": "1.5.0",
4
+ "description": "dotpi javascript utilities",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": "./index.js",
8
+ "./*.js": "./*.js"
9
+ },
10
+ "dependencies": {
11
+ "chalk": "^5.4.0",
12
+ "execa": "^9.5.2",
13
+ "template-literal": "^1.0.4"
14
+ }
15
+ }
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "@dotpi/javascript",
3
+ "version": "1.5.0",
4
+ "description": "dotpi javascript utilities",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "exports": {
8
+ ".": "./index.js",
9
+ "./*.js": "./*.js"
10
+ },
11
+ "dependencies": {
12
+ "chalk": "^5.4.0",
13
+ "execa": "^9.5.2",
14
+ "template-literal": "^1.0.4"
15
+ }
16
+ }
@@ -0,0 +1,556 @@
1
+ #!/bin/bash
2
+
3
+ dotpi_audio_device_supported=(
4
+ 'default'
5
+ 'none'
6
+ 'headphones'
7
+ 'hifiberry dac+ adc pro'
8
+ 'hifiberry dac+ adc'
9
+ 'hifiberry dac+ standard'
10
+ 'hifiberry dac+ light'
11
+ 'hifiberry dac zero'
12
+ 'hifiberry miniamp'
13
+ 'hifiberry beocreate'
14
+ 'hifiberry dac+ dsp'
15
+ 'hifiberry dac+ rtc'
16
+ 'hifiberry pro'
17
+ 'hifiberry amp2'
18
+ 'hifiberry dac2 hd'
19
+ 'hifiberry digi+'
20
+ 'hifiberry digi+ pro'
21
+ 'hifiberry amp+'
22
+ 'hifiberry amp3'
23
+ 'hifiberry dac for raspberry pi 1'
24
+ 'raspberry pi dac pro'
25
+ 'raspberry pi dac+'
26
+ 'raspberry pi digiamp+'
27
+ 'raspberry pi codec zero'
28
+ 'iqaudio dac pro'
29
+ 'iqaudio dac+'
30
+ 'iqaudio digiamp+'
31
+ 'iqaudio codec zero'
32
+ 'bluetooth'
33
+ 'ue boom 2'
34
+ 'ue boom 3'
35
+ 'ue megaboom 2'
36
+ 'ue megaboom 3'
37
+ )
38
+
39
+ dotpi_audio_device_is_analog() {
40
+ audio_device="${1:-$dotpi_audio_device}"
41
+ case "$audio_device" in
42
+ 'headphones')
43
+ return 0
44
+ ;;
45
+ *)
46
+ return 1
47
+ ;;
48
+ esac
49
+ }
50
+
51
+ _dotpi_audio_device_get_options() {
52
+
53
+ _echo_empty_option_error() {
54
+ dotpi_echo_error "$1 requires a non-empty option argument."
55
+ }
56
+
57
+ # From http://mywiki.wooledge.org/BashFAQ/035
58
+
59
+ while true ; do
60
+ case $1 in
61
+
62
+ -h|-\?|--help)
63
+ _dotpi_usage # Display a usage synopsis.
64
+ return 0
65
+ ;;
66
+
67
+ -m|--model) # Takes an option argument; ensure it has been specified.
68
+ if [ -z "$2" ] ; then
69
+ _dotpi_usage
70
+ _echo_empty_option_error "$1"
71
+ return 1
72
+ else
73
+ model="$2"
74
+ shift
75
+ fi
76
+ ;;
77
+
78
+ -c|--config) # Takes an option argument; ensure it has been specified.
79
+ if [ -z "$2" ] ; then
80
+ _dotpi_usage
81
+ _echo_empty_option_error "$1"
82
+ return 1
83
+ else
84
+ config_file="$2"
85
+ shift
86
+ fi
87
+ ;;
88
+
89
+ --) # End of all options.
90
+ shift
91
+ break
92
+ ;;
93
+
94
+ -?*) # Unknown
95
+ _dotpi_usage
96
+ dotpi_echo_error "Unknown option: $1"
97
+ return 1
98
+ ;;
99
+
100
+ *) # Default case: No more options, so break out of the loop.
101
+ break
102
+
103
+ esac
104
+
105
+ shift
106
+ done
107
+
108
+ }
109
+
110
+ dotpi_audio_device_model_normalise() {
111
+ echo "$1" | tr '[:upper:]' '[:lower:]'
112
+ }
113
+
114
+ # Usage: sudo dotpi audio_device_select --model <model> [--config <config_file_path>]
115
+ # if --config is not provided, it will use the system default config file
116
+ dotpi_audio_device_select() (
117
+ # sub-shell to keep everything local (functions and variables)
118
+
119
+ _dotpi_command="$(basename -- "${FUNCNAME[0]:-"$0"}")"
120
+ _dotpi_usage() {
121
+ echo "Usage: ${_dotpi_command} --model <model> [--config <config_file_path>]"
122
+ }
123
+
124
+ # Initialize all the option variables.
125
+ # This ensures we are not contaminated by variables from the environment.
126
+ model=
127
+ config_file=
128
+
129
+ if ! _dotpi_audio_device_get_options "${@}" ; then
130
+ return $?
131
+ fi
132
+
133
+ if [ -z "$model" ] ; then
134
+ _dotpi_usage
135
+ dotpi_echo_error "missing model option"
136
+ return 1
137
+ fi
138
+
139
+ if [ -z "$config_file" ] ; then
140
+ config_path="$(dotpi_system_get_bootfs_path)"
141
+ if [ -z "$config_path" ] ; then
142
+ dotpi_echo_error "Unable to get default system path for config file"
143
+ _dotpi_usage
144
+ return 1
145
+ fi
146
+ config_filename='config.txt'
147
+ config_file="${config_path}/${config_filename}"
148
+ dotpi_echo_warning "Using default config file ${config_file}"
149
+ fi
150
+
151
+
152
+ if ! [ -w "$config_file" ] ; then
153
+ dotpi_echo_error "Unable to access config file ${config_file} for writing"
154
+ return 1
155
+ fi
156
+
157
+ # lowercase everything
158
+ model_normalised="$(dotpi_audio_device_model_normalise "$model")"
159
+
160
+ case "$model_normalised" in
161
+
162
+ ######## Generic devices
163
+
164
+ 'default')
165
+ dotpi_echo_warning "Not installing a specific audio device, keeping defaults"
166
+ ;;
167
+
168
+ 'none')
169
+ _dotpi_audio_device_disable_all
170
+ ;;
171
+
172
+ 'headphones')
173
+ _dotpi_audio_device_select_headphones
174
+ ;;
175
+
176
+ 'bluetooth')
177
+ dotpi_echo_info "Installing generic bluetooth device"
178
+ _dotpi_audio_device_select_bluetooth
179
+ ;;
180
+
181
+ ######## Specific devices
182
+
183
+ # Raspberry
184
+ # Cf. https://www.raspberrypi.com/documentation/accessories/audio.html#configuration
185
+
186
+ 'raspberry pi dac pro'|'iqaudio dac pro')
187
+ dtoverlay=rpi-dacpro
188
+ _dotpi_audio_device_select_raspberry_pi
189
+ ;;
190
+
191
+ 'raspberry pi dac+'|'iqaudio dac+')
192
+ dtoverlay='rpi-dacplus,auto_mute_amp'
193
+ _dotpi_audio_device_select_raspberry_pi
194
+ ;;
195
+
196
+ 'raspberry pi digiamp+'|'iqaudio digiamp+')
197
+ dtoverlay='rpi-digiampplus,auto_mute_amp'
198
+ _dotpi_audio_device_select_raspberry_pi
199
+ ;;
200
+
201
+ 'raspberry pi codec zero'|'iqaudio codec zero')
202
+ dtoverlay=rpi-codeczero
203
+ _dotpi_audio_device_select_raspberry_pi
204
+ ;;
205
+
206
+ # HiFiBerry
207
+ # Cf. https://www.hifiberry.com/docs/software/configuring-linux-3-18-x/
208
+
209
+ 'hifiberry dac for raspberry pi 1'|'hifiberry dac+ light'|'hifiberry dac zero'|'hifiberry miniamp'|'hifiberry beocreate'|'hifiberry dac+ dsp'|'hifiberry dac+ rtc')
210
+ dtoverlay=hifiberry-dac
211
+ _dotpi_audio_device_select_hifiberry
212
+ ;;
213
+
214
+ 'hifiberry dac+ standard'|'hifiberry pro'|'hifiberry amp2')
215
+ dtoverlay=hifiberry-dacplus
216
+ _dotpi_audio_device_select_hifiberry
217
+ ;;
218
+
219
+ 'hifiberry dac2 hd')
220
+ dtoverlay=hifiberry-dacplushd
221
+ _dotpi_audio_device_select_hifiberry
222
+ ;;
223
+
224
+ 'hifiberry dac+ adc')
225
+ dtoverlay=hifiberry-dacplusadc
226
+ _dotpi_audio_device_select_hifiberry
227
+ ;;
228
+
229
+ 'hifiberry dac+ adc pro')
230
+ dtoverlay=hifiberry-dacplusadcpro
231
+ _dotpi_audio_device_select_hifiberry
232
+ ;;
233
+
234
+ 'hifiberry digi+')
235
+ dtoverlay=hifiberry-digi
236
+ _dotpi_audio_device_select_hifiberry
237
+ ;;
238
+
239
+ 'hifiberry digi+ pro')
240
+ dtoverlay=hifiberry-digi-pro
241
+ _dotpi_audio_device_select_hifiberry
242
+ ;;
243
+
244
+ 'hifiberry amp+')
245
+ dtoverlay=hifiberry-amp
246
+ _dotpi_audio_device_select_hifiberry
247
+ ;;
248
+
249
+ 'hifiberry amp3')
250
+ dtoverlay=hifiberry-amp3
251
+ _dotpi_audio_device_select_hifiberry
252
+ ;;
253
+
254
+ ####### Ultimate Ears (UE)
255
+
256
+ 'ue boom 2'|'ue boom 3'|'ue megaboom 2'|'ue megaboom 3')
257
+ _dotpi_audio_device_select_bluetooth
258
+ ;;
259
+
260
+
261
+ ########### Unknown device
262
+
263
+ *) # other
264
+ _dotpi_usage
265
+ dotpi_echo_error "Unknown audio device model: ${model}"
266
+ return 1
267
+ ;;
268
+
269
+ esac
270
+
271
+ # use xargs to unquote value
272
+ model_project="$(dotpi_configuration_read \
273
+ --file "${DOTPI_ROOT}/etc/dotpi_environment_project.bash" \
274
+ --key dotpi_audio_device \
275
+ | xargs)"
276
+
277
+
278
+ if [[ "$model_normalised" == "$(dotpi_audio_device_model_normalise "$model_project")" ]] ; then
279
+ # no need to update environment
280
+ return 0;
281
+ fi
282
+
283
+ dotpi_echo_warning 'Audio device model is specific to this instance'
284
+
285
+ # comment setting in project
286
+ dotpi_configuration_comment --file "${DOTPI_ROOT}/etc/dotpi_environment_project.bash" \
287
+ --key dotpi_audio_device
288
+
289
+ # escape double-quote for now, to quote when writing later
290
+ model_value="$(echo "$model" | dotpi_sed 's/"/\\"/g')"
291
+
292
+ # write changed setting in instance
293
+ # be sure to quote value
294
+ dotpi_configuration_write --file "${DOTPI_ROOT}/etc/dotpi_environment_instance.bash" \
295
+ --key dotpi_audio_device \
296
+ --value \""${model_value}"\"
297
+ )
298
+
299
+ _dotpi_audio_device_disable_all() (
300
+ exception_family="$1"
301
+
302
+ if [[ "$exception_family" = "" ]] ; then
303
+ dotpi_echo_info "Disabling all audio devices"
304
+ else
305
+ dotpi_echo_info "Disabling all audio devices except ${exception_family}"
306
+ fi
307
+
308
+ if [[ ! "$exception_family" = "headphones" ]] ; then
309
+ _dotpi_audio_device_disable_headphones
310
+ fi
311
+
312
+ if [[ ! "$exception_family" = "hdmi" ]] ; then
313
+ _dotpi_audio_device_disable_hdmi
314
+ fi
315
+
316
+ if [[ ! "$exception_family" = "raspberry_pi" ]] ; then
317
+ _dotpi_audio_device_disable_raspberry_pi
318
+ fi
319
+
320
+ if [[ ! "$exception_family" = "hifiberry" ]] ; then
321
+ _dotpi_audio_device_disable_hifiberry
322
+ fi
323
+
324
+ )
325
+
326
+ _dotpi_audio_device_select_headphones() (
327
+ dotpi_echo_info "Installing Headphones (default) audio device"
328
+
329
+ _dotpi_audio_device_disable_all headphones
330
+
331
+ # un-comment dtparam=audio as it must be set first
332
+ # (setting at the end the the file fails)
333
+ pattern="$(dotpi_configuration_get_pattern --prefix "# dtparam=audio")"
334
+ perl -pe "s/${pattern}/"'dtparam=audio=on/' -i -- "$config_file"
335
+ )
336
+
337
+ _dotpi_audio_device_select_bluetooth() (
338
+ _dotpi_audio_device_disable_all bluetooth
339
+ )
340
+
341
+ _dotpi_audio_device_select_raspberry_pi() (
342
+ dotpi_echo_info "Configuring Raspberry Pi audio device '${model}'"
343
+
344
+ _dotpi_audio_device_disable_all raspberry_pi
345
+
346
+ mkdir -p "$(dirname -- "$config_file")"
347
+ cat >> "$config_file" << EOF
348
+
349
+ # dotpi: overlay for audio device '${model}'
350
+ dtoverlay=${dtoverlay}
351
+
352
+ EOF
353
+ )
354
+
355
+
356
+ _dotpi_audio_device_select_hifiberry() (
357
+ dotpi_echo_info "Configuring HiFiBerry audio device '${model}'"
358
+
359
+ _dotpi_audio_device_disable_all hifiberry
360
+
361
+ mkdir -p "$(dirname -- "$config_file")"
362
+ cat >> "$config_file" << EOF
363
+
364
+ # dotpi: overlay for audio device '${model}'
365
+ dtoverlay=${dtoverlay}
366
+
367
+ EOF
368
+ )
369
+
370
+ _dotpi_audio_device_disable_headphones() (
371
+ dotpi_echo_info "Disabling headphones"
372
+ # disable internal audio device
373
+ dotpi_configuration_comment --file "$config_file" --key 'dtparam=audio'
374
+ )
375
+
376
+ _dotpi_audio_device_disable_hdmi() (
377
+ dotpi_echo_info "Disabling HDMI audio"
378
+ # disable HDMI audio
379
+
380
+ # new systems
381
+ pattern="$(dotpi_configuration_get_pattern --prefix "dtoverlay=vc4-kms-v3d")"
382
+ perl -pe "s/${pattern}/"'${1}${2},noaudio/' -i -- "$config_file"
383
+
384
+ # old systems
385
+ pattern="$(dotpi_configuration_get_pattern --prefix "dtoverlay=vc4-fkms-v3d")"
386
+ perl -pe "s/${pattern}/"'${1}${2},audio=off/' -i -- "$config_file"
387
+ )
388
+
389
+ _dotpi_audio_device_disable_raspberry_pi() (
390
+ dotpi_echo_info "Disabling Raspberry Pi audio"
391
+
392
+ # disable hifiberry
393
+ pattern="$(dotpi_configuration_get_pattern --prefix "dtoverlay=rpi-")"
394
+ perl -pe "s/${pattern}/"'${1}# ${2}${3}/' -i -- "$config_file"
395
+ )
396
+
397
+ _dotpi_audio_device_disable_hifiberry() (
398
+ dotpi_echo_info "Disabling HiFiBerry audio"
399
+
400
+ # disable hifiberry
401
+ pattern="$(dotpi_configuration_get_pattern --prefix "dtoverlay=hifiberry-")"
402
+ perl -pe "s/${pattern}/"'${1}# ${2}${3}/' -i -- "$config_file"
403
+ )
404
+
405
+ _dotpi_audio_device_volume_set_alsa() (
406
+ amixer -- sset "$control" "$volume"
407
+ alsactl store
408
+
409
+ if [[ "$volume" == "$dotpi_audio_volume" ]] ; then
410
+ # no change to write
411
+ return 0
412
+ fi
413
+
414
+ # comment setting in project
415
+ dotpi_configuration_comment --file "${DOTPI_ROOT}/etc/dotpi_environment_project.bash" \
416
+ --key dotpi_audio_volume
417
+
418
+ # escape double-quote for now, to quote when writing later
419
+ model_value="$(echo "$model" | dotpi_sed 's/"/\\"/g')"
420
+
421
+ # write changed setting in instance
422
+ dotpi_configuration_write --file "${DOTPI_ROOT}/etc/dotpi_environment_instance.bash" \
423
+ --key dotpi_audio_volume \
424
+ --value "$volume"
425
+ )
426
+
427
+ dotpi_audio_device_init() (
428
+
429
+ if [[ -z "$dotpi_audio_device" ]] ; then
430
+ dotpi_echo_warning "dotpi_audio_device not set, keeping default audio device"
431
+ return 0
432
+ fi
433
+
434
+ model_normalised="$(dotpi_audio_device_model_normalise "$dotpi_audio_device")"
435
+
436
+ # hardware
437
+ case "$model_normalised" in
438
+ 'raspberry pi codec zero'|'iqaudio codec zero')
439
+
440
+ usr_repository="raspberrypi/Pi-Codec"
441
+ usr_directory="${DOTPI_ROOT}/usr/${usr_repository}"
442
+
443
+ # clone once to be able to init offline
444
+ if ! [[ -d "${usr_directory}" ]] ; then
445
+ mkdir -p "${usr_directory}"
446
+ git clone "https://github.com/${usr_repository}.git" "${usr_directory}" || {
447
+ dotpi_echo_error "Unable to clone repository ${usr_repository}"
448
+ return 1
449
+ }
450
+ fi
451
+
452
+ alsactl restore -f "${usr_directory}/Codec_Zero_StereoMIC_record_and_HP_playback.state"
453
+ ;;
454
+
455
+ esac
456
+
457
+ # alsa
458
+ case "$model_normalised" in
459
+
460
+ 'headphones')
461
+
462
+ amixer -D sysdefault sset -- 'PCM' 'on'
463
+ amixer -D sysdefault sset -- 'PCM' '0dB'
464
+ alsactl store
465
+ ;;
466
+
467
+ 'raspberry pi dac pro'|'iqaudio dac pro' \
468
+ | 'raspberry pi dac+'|'iqaudio dac+' \
469
+ | 'raspberry pi digiamp+'|'iqaudio digiamp+' \
470
+ | 'raspberry pi codec zero'|'iqaudio codec zero')
471
+
472
+ amixer -D sysdefault sset -- 'Headphone' 'on'
473
+ amixer -D sysdefault sset -- 'Headphone' '0dB'
474
+
475
+ amixer -D sysdefault sset -- 'Lineout' 'on'
476
+ amixer -D sysdefault sset -- 'Lineout' '0dB'
477
+
478
+ alsactl store
479
+ ;;
480
+
481
+ esac
482
+
483
+ )
484
+
485
+ # set and store volume for the current audio device
486
+ dotpi_audio_device_volume_set() (
487
+ volume="$1"
488
+
489
+ model="$dotpi_audio_device"
490
+ # lowercase everything
491
+ model_normalised="$(dotpi_audio_device_model_normalise "$model")"
492
+
493
+ case "$model_normalised" in
494
+
495
+ ####### Generic devices
496
+
497
+ 'headphones')
498
+ control='PCM'
499
+ _dotpi_audio_device_volume_set_alsa
500
+ ;;
501
+
502
+ 'bluetooth')
503
+ dotpi_audio_bluetooth_destination_volume_set "$volume"
504
+ ;;
505
+
506
+
507
+ ####### Specific devices
508
+
509
+ # HiFiBerry
510
+ # Cf. https://www.hifiberry.com/docs/archive/mixer-controls-on-the-hifiberry-boards/
511
+
512
+
513
+ 'hifiberry dac+ dsp'|'hifiberry dac+ rtc'|'hifiberry dac+ standard'|'hifiberry amp2'|'hifiberry dac+ adc'|'hifiberry dac+ adc pro'|'hifiberry amp3')
514
+ control='Digital'
515
+ _dotpi_audio_device_volume_set_alsa
516
+ ;;
517
+
518
+ 'hifiberry dac2 hd')
519
+ control='DAC'
520
+ _dotpi_audio_device_volume_set_alsa
521
+ ;;
522
+
523
+ 'hifiberry amp+')
524
+ control='Master'
525
+ _dotpi_audio_device_volume_set_alsa
526
+ ;;
527
+
528
+ ####### Ultimate Ears (UE)
529
+
530
+ 'ue boom 2'|'ue boom 3'|'ue megaboom 2'|'ue megaboom 3')
531
+ dotpi_audio_bluetooth_destination_volume_set "$volume"
532
+ ;;
533
+
534
+ ########### Unknown device
535
+
536
+ *) # other
537
+ dotpi_echo_error "Unable to set volume for unknown audio device model: ${model}"
538
+ return 1
539
+ ;;
540
+
541
+
542
+ esac
543
+ )
544
+
545
+ # restore volume for the current audio device
546
+ dotpi_audio_device_volume_init() (
547
+ if [[ -z "$dotpi_audio_volume" ]] ; then
548
+ dotpi_echo_warning "dotpi_audio_volume not set, keeping default volume"
549
+ return 0
550
+ fi
551
+
552
+ destination_volume="$dotpi_audio_volume"
553
+ dotpi_audio_device_volume_set "$destination_volume"
554
+ )
555
+
556
+