├── .formatter.exs ├── .gitignore ├── LICENSE ├── README.md ├── config └── config.exs ├── lib ├── power │ ├── application.ex │ ├── cpu.ex │ ├── hdmi.ex │ └── led.ex └── power_control.ex ├── mix.exs ├── mix.lock └── test ├── power_test.exs └── test_helper.exs /.formatter.exs: -------------------------------------------------------------------------------- 1 | # Used by "mix format" 2 | [ 3 | inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] 4 | ] 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # The directory Mix will write compiled artifacts to. 2 | /_build/ 3 | 4 | # If you run "mix test --cover", coverage assets end up here. 5 | /cover/ 6 | 7 | # The directory Mix downloads your dependencies sources to. 8 | /deps/ 9 | 10 | # Where 3rd-party dependencies like ExDoc output generated docs. 11 | /doc/ 12 | 13 | # Ignore .fetch files in case you like to edit your project deps locally. 14 | /.fetch 15 | 16 | # If the VM crashes, it generates a dump, let's ignore it too. 17 | erl_crash.dump 18 | 19 | # Also ignore archive artifacts (built via "mix archive.build"). 20 | *.ez 21 | 22 | # Ignore package tarball (built via "mix hex.build"). 23 | power_control-*.tar 24 | 25 | .elixir_ls -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power_control, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PowerControl 2 | 3 | PowerControl is a library for start-up and runtime configuration of embedded device power consumption settings. This can be used to decrease power usage of embedded devices or increase their performance. For more information, see the benchmarks section. 4 | 5 | [![Hex](https://img.shields.io/hexpm/v/power_control.svg?style=flat)](https://hexdocs.pm/power_control/PowerControl.html) 6 | 7 | ## Installation 8 | 9 | Add `power_control` to your list of dependencies in `mix.exs`: 10 | 11 | ```elixir 12 | def deps do 13 | [ 14 | {:power_control, "~> 0.1.0"} 15 | ] 16 | end 17 | ``` 18 | 19 | If using `shoehorn`, add `:power_control` to your shoehorn apps in your `config.exs`: 20 | 21 | ```elixir 22 | config :shoehorn, 23 | init: [:nerves_runtime, ..., :power_control], 24 | ... 25 | ``` 26 | It must come after `:nerves_runtime` but has no requirements other than that. 27 | 28 | Once installed, startup configuration can be set in your `config.exs` like so: 29 | 30 | ```elixir 31 | config :power_control, 32 | cpu_governor: :powersave, 33 | disable_hdmi: true, 34 | disable_leds: true 35 | ``` 36 | 37 | ## What governors can I use? 38 | 39 | Governors are dependant on hardware, and while it's possible to guess what governors you have access to based on your device, the best way to be sure is to run `PowerControl.list_cpu_governors()` in your nerves iex shell (on the device you want to identify) to see a list of atoms representing your device's supported governors. For a more detailed walkthough, check out the [docs](https://hexdocs.pm/power_control/PowerControl.html). 40 | 41 | ## Benchmarks 42 | 43 | Lets take a look at the following fibonacci benchmark run on a Raspberry Pi Zero W using different [`CPUFreq`](https://www.kernel.org/doc/html/v4.15/admin-guide/pm/cpufreq.html) settings: 44 | 45 | ``` 46 | iex(1)> PowerControl.cpu_info(:cpu0).speed 47 | 700000 48 | iex(2)> PowerTest.bench_fib(145862) 49 | "11611628 μs" 50 | iex(3)> PowerControl.set_cpu_governor(:cpu0, :performance) 51 | {:ok, :performance} 52 | iex(4)> PowerControl.cpu_info(:cpu0).speed 53 | 1000000 54 | iex(5)> PowerTest.bench_fib(145862) 55 | "8332247 μs" 56 | ``` 57 | 58 | Just by changing the CPU governor at runtime to `:performance`, we see a significant increase in benchmark speed. If you compare the ratios of the two benchmarks to the ratios of the clock speeds, you can see they are almost the same (Benchmark: 0.71, CPU clock: 0.7). 59 | -------------------------------------------------------------------------------- /config/config.exs: -------------------------------------------------------------------------------- 1 | use Mix.Config 2 | -------------------------------------------------------------------------------- /lib/power/application.ex: -------------------------------------------------------------------------------- 1 | defmodule PowerControl.Application do 2 | @moduledoc false 3 | use Application 4 | 5 | def start(_type, _args) do 6 | PowerControl.init() 7 | 8 | opts = [strategy: :one_for_one, name: PowerControl.Supervisor] 9 | Supervisor.start_link([], opts) 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /lib/power/cpu.ex: -------------------------------------------------------------------------------- 1 | defmodule PowerControl.CPU do 2 | @moduledoc false 3 | @default_cpu_dir "/sys/devices/system/cpu/" 4 | @governors_file_name "scaling_available_governors" 5 | @governor_file_name "scaling_governor" 6 | @info_files %{ 7 | speed: :cpuinfo_cur_freq, 8 | max_speed: :cpuinfo_max_freq, 9 | min_speed: :cpuinfo_min_freq 10 | } 11 | 12 | @doc false 13 | def init do 14 | with {:ok, cpus} <- list_cpus(), 15 | startup_governor when not is_nil(startup_governor) <- 16 | Application.get_env(:power_control, :cpu_governor) do 17 | Enum.each(cpus, fn cpu -> 18 | {:ok, ^startup_governor} = set_governor(cpu, startup_governor) 19 | end) 20 | else 21 | nil -> {:error, :no_startup_governor_configured} 22 | error -> error 23 | end 24 | end 25 | 26 | @doc false 27 | def cpu_dir do 28 | Application.get_env(:power_control, :cpu_dir, @default_cpu_dir) 29 | end 30 | 31 | @doc false 32 | def list_cpus do 33 | cpu_dir() 34 | |> File.ls() 35 | |> case do 36 | {:ok, list} -> 37 | cpus = 38 | list 39 | |> Enum.filter(&filter_cpus/1) 40 | |> Enum.map(&String.to_atom/1) 41 | 42 | {:ok, cpus} 43 | 44 | {:error, reason} -> 45 | {:error, reason} 46 | end 47 | end 48 | 49 | defp filter_cpus("cpu" <> rest) do 50 | Integer.parse(rest) != :error 51 | end 52 | 53 | defp filter_cpus(_), do: false 54 | 55 | @doc false 56 | def list_governors(cpu) do 57 | "#{cpu_dir()}#{cpu}/cpufreq/#{@governors_file_name}" 58 | |> File.read() 59 | |> case do 60 | {:ok, contents} -> 61 | governors = 62 | contents 63 | |> String.split() 64 | |> Enum.map(&:"#{&1}") 65 | 66 | {:ok, governors} 67 | 68 | {:error, reason} -> 69 | {:error, reason} 70 | end 71 | end 72 | 73 | @doc false 74 | def set_governor(cpu, governor) do 75 | file_path = "#{cpu_dir()}#{cpu}/cpufreq/#{@governor_file_name}" 76 | 77 | with {:file, true} <- {:file, File.exists?(file_path)}, 78 | {:ok, valid_governors} <- list_governors(cpu), 79 | {:governor, true} <- {:governor, governor in valid_governors}, 80 | :ok <- File.write(file_path, "#{governor}") do 81 | {:ok, governor} 82 | else 83 | {:file, false} -> {:error, :governor_file_not_found} 84 | {:governor, false} -> {:error, :invalid_governor} 85 | {:error, reason} -> {:error, reason} 86 | end 87 | end 88 | 89 | @doc false 90 | def cpu_info(cpu) do 91 | case list_cpus() do 92 | {:ok, cpus} -> 93 | Enum.find(cpus, &(&1 == cpu)) 94 | |> parse_cpu_info() 95 | 96 | error -> 97 | error 98 | end 99 | end 100 | 101 | defp parse_cpu_info(nil), do: {:error, :cpu_not_found} 102 | 103 | defp parse_cpu_info(cpu) do 104 | for {key, file} <- @info_files do 105 | info_path = "#{cpu_dir()}/#{cpu}/cpufreq/#{file}" 106 | 107 | with {:ok, body} <- File.read(info_path), 108 | {value, _} <- Integer.parse(body) do 109 | {key, value} 110 | else 111 | _ -> {key, nil} 112 | end 113 | end 114 | |> Enum.filter(fn {_, value} -> value end) 115 | |> Enum.into(%{}) 116 | end 117 | end 118 | -------------------------------------------------------------------------------- /lib/power/hdmi.ex: -------------------------------------------------------------------------------- 1 | defmodule PowerControl.HDMI do 2 | @moduledoc false 3 | require Logger 4 | 5 | @doc false 6 | def init do 7 | if Application.get_env(:power_control, :disable_hdmi) do 8 | disable_hdmi() 9 | end 10 | 11 | :ok 12 | end 13 | 14 | @doc false 15 | def disable_hdmi do 16 | :os.cmd('tvservice -o') 17 | end 18 | 19 | @doc false 20 | def enable_hdmi do 21 | :os.cmd('tvservice -p') 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /lib/power/led.ex: -------------------------------------------------------------------------------- 1 | defmodule PowerControl.LED do 2 | @moduledoc false 3 | @default_led_dir "/sys/class/leds/" 4 | @brightness_file_name "brightness" 5 | 6 | @doc false 7 | def init do 8 | with {:ok, leds} <- list_leds() do 9 | if(Application.get_env(:power_control, :disable_leds, true)) do 10 | Enum.each(leds, fn led -> 11 | disable_led(led) 12 | end) 13 | end 14 | end 15 | end 16 | 17 | @doc false 18 | def led_dir do 19 | Application.get_env(:power_control, :led_dir, @default_led_dir) 20 | end 21 | 22 | @doc false 23 | def list_leds do 24 | led_dir() 25 | |> File.ls() 26 | |> case do 27 | {:ok, list} -> 28 | {:ok, Enum.filter(list, &filter_leds/1)} 29 | 30 | {:error, reason} -> 31 | {:error, reason} 32 | end 33 | end 34 | 35 | defp filter_leds("led" <> rest) do 36 | Integer.parse(rest) != :error 37 | end 38 | 39 | defp filter_leds(_), do: false 40 | 41 | @doc false 42 | def disable_led(led, tries \\ 0) do 43 | "#{led_dir()}#{led}/#{@brightness_file_name}" 44 | |> File.write("0") 45 | |> case do 46 | {:error, _} when tries < 3 -> 47 | Process.sleep(tries * 100) 48 | disable_led(led, tries + 1) 49 | 50 | _ -> 51 | :ok 52 | end 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /lib/power_control.ex: -------------------------------------------------------------------------------- 1 | defmodule PowerControl do 2 | @moduledoc """ 3 | `PowerControl` is a library that enables runtime configuration of embedded linux for power conservation or performance via native elixir. 4 | 5 | ## Getting Started 6 | 7 | If using `shoehorn`, add `:power_control` to your shoehorn apps in your `config.exs`: 8 | 9 | ```elixir 10 | config :shoehorn, 11 | init: [:nerves_runtime, ..., :power_control], 12 | ... 13 | ``` 14 | It must come after `:nerves_runtime` but has no requirements other than that. 15 | 16 | Once installed, startup configuration can be set in your `config.exs` like so: 17 | 18 | ```elixir 19 | config :power_control, 20 | cpu_governor: :powersave, 21 | disable_leds: true, 22 | disable_hdmi: true 23 | ``` 24 | The CPU Governor determines CPU clock speed behavior. Different devices can have different available governors. For more information about determining available governors, see the "Runtime Functionality" section below. 25 | 26 | `disable_leds` disables all system leds. To selectively disable them, you may want to use the functions `list_leds/0` and `disable_led/1` manually. More complex LED management can be done with [`Nerves.Leds`](https://github.com/nerves-project/nerves_leds). 27 | 28 | `disable_hdmi` disables the onboard hdmi of the pi. This may also disable other forms of video on hats or other peripherals, but this is not tested. 29 | 30 | ## Runtime Functionality 31 | 32 | `PowerControl` also allows you fetch information about your system and configure some settings during runtime, for example: 33 | 34 | ```elixir 35 | iex> list_cpus() 36 | {:ok, [:cpu0]} 37 | iex> cpu_info(:cpu0).speed 38 | 700000 39 | iex> list_cpu_governors(:cpu0) 40 | {:ok, [:ondemand, :userspace, :powersave, :conservative, :performance]} 41 | iex> set_cpu_governor(:cpu0, :performance) 42 | {:ok, :performance} 43 | iex> cpu_info(:cpu0).speed 44 | 1000000 45 | ``` 46 | 47 | The functions `list_cpus/0` and `list_cpu_governors/1` can be used to determine what governors you have available on your device for configuration. 48 | 49 | ## Other Errors and Advanced Configuration 50 | 51 | All Led and CPU functions can return Elixir `File` error messages such as `:enoent`, which generally indicates you have configured the directories improperly for your specific device (unlikely) or that `PowerControl` does not support your device (most likely). In the unlikely case that your device can be supported by `PowerControl` but you must manually configure the directories, the config keys are `cpu_dir` and `led_dir`. I do not currently support configuring filenames. 52 | """ 53 | require Logger 54 | alias PowerControl.{CPU, LED, HDMI} 55 | 56 | @startup_governor_warning "[PowerControl] No startup CPU Governor configured, device will use default." 57 | @directory_warning_cpu "[PowerControl] Could not find CPU directory, are you sure this is a Nerves device?" 58 | @directory_warning_led "[PowerControl] Could not find LED directory, are you sure this is a Nerves device?" 59 | @doc false 60 | def init do 61 | case CPU.init() do 62 | {:error, :no_startup_governor_configured} -> 63 | Logger.warn(@startup_governor_warning) 64 | 65 | {:error, :enoent} -> 66 | Logger.warn(@directory_warning_cpu) 67 | 68 | _ -> 69 | :ok 70 | end 71 | 72 | case LED.init() do 73 | {:error, :enoent} -> 74 | Logger.warn(@directory_warning_led) 75 | 76 | _ -> 77 | :ok 78 | end 79 | 80 | HDMI.init() 81 | :ok 82 | end 83 | 84 | @doc """ 85 | Lists system CPUS. 86 | 87 | ``` 88 | iex> list_cpus() 89 | {:ok, [:cpu0]} 90 | ``` 91 | """ 92 | def list_cpus do 93 | CPU.list_cpus() 94 | end 95 | 96 | @doc """ 97 | Returns an info map for a CPU. If the CPU does not exist, a `File` error is returned. 98 | 99 | ``` 100 | iex> cpu_info(:cpu0) 101 | %{max_speed: 1000000, min_speed: 700000, speed: 1000000} 102 | ``` 103 | 104 | ``` 105 | iex> cpu_info(:bad_cpu) 106 | {:error, :enoent} 107 | ``` 108 | """ 109 | def cpu_info(cpu) do 110 | CPU.cpu_info(cpu) 111 | end 112 | 113 | @doc """ 114 | Returns available governors for a CPU. 115 | 116 | ``` 117 | iex> list_cpu_governors(:cpu0) 118 | {:ok, [:ondemand, :userspace, :powersave, :conservative, :performance]} 119 | ``` 120 | """ 121 | def list_cpu_governors(cpu) do 122 | CPU.list_governors(cpu) 123 | end 124 | 125 | @doc """ 126 | Sets the governor for a CPU. 127 | 128 | ``` 129 | iex> set_cpu_governor(:cpu0, :powersave) 130 | {:ok, :powersave} 131 | 132 | iex> set_cpu_governor(:cpu0, :invalid) 133 | {:error, :invalid_governor} 134 | 135 | # Running on non-nerves device or with bad governor file settings 136 | iex> set_cpu_governor(:cpu0, :powersave) 137 | {:error, :governor_file_not_found} 138 | ``` 139 | """ 140 | def set_cpu_governor(cpu, governor) do 141 | CPU.set_governor(cpu, governor) 142 | end 143 | 144 | @doc """ 145 | Lists system LEDS. 146 | 147 | ``` 148 | iex> list_leds() 149 | {:ok, [:led0]} 150 | ``` 151 | """ 152 | def list_leds do 153 | LED.list_leds() 154 | end 155 | 156 | @doc """ 157 | Disables an LED. 158 | 159 | Uses a simple retry system if it fails, retries 2 times then gives up. 160 | To re-enable the LED or further configure LED settings, I reccomend [`Nerves.Leds`](https://github.com/nerves-project/nerves_leds). 161 | 162 | ``` 163 | iex> disable_led(:led0) 164 | :ok 165 | 166 | iex> disable_led(:invalid_led) 167 | {:error, ...} 168 | ``` 169 | """ 170 | def disable_led(led) do 171 | LED.disable_led(led) 172 | end 173 | 174 | @doc """ 175 | Disables the HDMI port. 176 | 177 | Returns `:ok` regardless of failure or success, but almost always succeeds. 178 | 179 | ``` 180 | iex> disable_hdmi() 181 | :ok 182 | ``` 183 | """ 184 | def disable_hdmi do 185 | HDMI.disable_hdmi() 186 | :ok 187 | end 188 | end 189 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule PowerControl.MixProject do 2 | use Mix.Project 3 | 4 | def project do 5 | [ 6 | app: :power_control, 7 | version: "0.2.0", 8 | elixir: "~> 1.7", 9 | start_permanent: Mix.env() == :prod, 10 | deps: deps(), 11 | description: "A library for managing power consumption of embedded devices.", 12 | package: package() 13 | ] 14 | end 15 | 16 | def application do 17 | [ 18 | extra_applications: [:logger], 19 | mod: {PowerControl.Application, []} 20 | ] 21 | end 22 | 23 | defp deps do 24 | [{:ex_doc, ">= 0.0.0", only: :dev}] 25 | end 26 | 27 | defp package do 28 | [ 29 | maintainers: ["Chris Freeze"], 30 | licenses: ["Apache 2.0"], 31 | links: %{"Github" => "https://github.com/cjfreeze/power_control"} 32 | ] 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /mix.lock: -------------------------------------------------------------------------------- 1 | %{ 2 | "earmark": {:hex, :earmark, "1.2.6", "b6da42b3831458d3ecc57314dff3051b080b9b2be88c2e5aa41cd642a5b044ed", [:mix], [], "hexpm"}, 3 | "ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"}, 4 | "makeup": {:hex, :makeup, "0.5.5", "9e08dfc45280c5684d771ad58159f718a7b5788596099bdfb0284597d368a882", [:mix], [{:nimble_parsec, "~> 0.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"}, 5 | "makeup_elixir": {:hex, :makeup_elixir, "0.10.0", "0f09c2ddf352887a956d84f8f7e702111122ca32fbbc84c2f0569b8b65cbf7fa", [:mix], [{:makeup, "~> 0.5.5", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"}, 6 | "nimble_parsec": {:hex, :nimble_parsec, "0.4.0", "ee261bb53214943679422be70f1658fff573c5d0b0a1ecd0f18738944f818efe", [:mix], [], "hexpm"}, 7 | } 8 | -------------------------------------------------------------------------------- /test/power_test.exs: -------------------------------------------------------------------------------- 1 | defmodule PowerTest do 2 | use ExUnit.Case 3 | end 4 | -------------------------------------------------------------------------------- /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | --------------------------------------------------------------------------------