Skip to main content

esp_generate/
lib.rs

1use esp_metadata_generated::{MemoryRegion, PinInfo};
2use serde::{Deserialize, Serialize};
3
4use crate::modules::Module;
5
6pub mod cargo;
7pub mod config;
8pub mod modules;
9pub mod template;
10
11#[derive(
12    Debug,
13    Clone,
14    Copy,
15    PartialEq,
16    Eq,
17    Serialize,
18    Deserialize,
19    clap::ValueEnum,
20    strum::EnumIter,
21    strum::Display,
22)]
23#[serde(rename_all = "kebab-case")]
24#[strum(serialize_all = "kebab-case")]
25pub enum Chip {
26    Esp32,
27    Esp32c2,
28    Esp32c3,
29    Esp32c5,
30    Esp32c6,
31    Esp32c61,
32    Esp32h2,
33    Esp32s2,
34    Esp32s3,
35}
36impl Chip {
37    pub fn metadata(self) -> esp_metadata_generated::Chip {
38        match self {
39            Chip::Esp32 => esp_metadata_generated::Chip::Esp32,
40            Chip::Esp32c2 => esp_metadata_generated::Chip::Esp32c2,
41            Chip::Esp32c3 => esp_metadata_generated::Chip::Esp32c3,
42            Chip::Esp32c5 => esp_metadata_generated::Chip::Esp32c5,
43            Chip::Esp32c6 => esp_metadata_generated::Chip::Esp32c6,
44            Chip::Esp32c61 => esp_metadata_generated::Chip::Esp32c61,
45            Chip::Esp32h2 => esp_metadata_generated::Chip::Esp32h2,
46            Chip::Esp32s2 => esp_metadata_generated::Chip::Esp32s2,
47            Chip::Esp32s3 => esp_metadata_generated::Chip::Esp32s3,
48        }
49    }
50
51    pub fn wokwi(self) -> &'static str {
52        match self {
53            Chip::Esp32 => "board-esp32-devkit-c-v4",
54            Chip::Esp32c2 => "",
55            Chip::Esp32c3 => "board-esp32-c3-devkitm-1",
56            Chip::Esp32c5 => "",
57            Chip::Esp32c6 => "board-esp32-c6-devkitc-1",
58            Chip::Esp32c61 => "",
59            Chip::Esp32h2 => "board-esp32-h2-devkitm-1",
60            Chip::Esp32s2 => "board-esp32-s2-devkitm-1",
61            Chip::Esp32s3 => "board-esp32-s3-devkitc-1",
62        }
63    }
64
65    pub fn dram2_region(self) -> &'static MemoryRegion {
66        self.metadata()
67            .memory_layout()
68            .region("dram2_uninit")
69            .expect("All chips should have a dram2_uninit region")
70    }
71
72    pub fn pins(self) -> &'static [PinInfo] {
73        self.metadata().pins()
74    }
75
76    pub fn modules(self) -> &'static [Module] {
77        match self {
78            Chip::Esp32 => crate::modules::ESP32_MODULES,
79            Chip::Esp32c2 => crate::modules::ESP32C2_MODULES,
80            Chip::Esp32c3 => crate::modules::ESP32C3_MODULES,
81            Chip::Esp32c5 => crate::modules::ESP32C5_MODULES,
82            Chip::Esp32c6 => crate::modules::ESP32C6_MODULES,
83            Chip::Esp32c61 => crate::modules::ESP32C61_MODULES,
84            Chip::Esp32h2 => crate::modules::ESP32H2_MODULES,
85            Chip::Esp32s2 => crate::modules::ESP32S2_MODULES,
86            Chip::Esp32s3 => crate::modules::ESP32S3_MODULES,
87        }
88    }
89
90    pub fn module_by_name(&self, module_name: &str) -> Option<&'static Module> {
91        self.modules()
92            .iter()
93            .find(|module| module.name == module_name)
94    }
95}
96
97/// This turns a list of strings into a sentence, and appends it to the base string.
98///
99/// # Example
100///
101/// ```rust
102/// # use esp_generate::append_list_as_sentence;
103/// let list = &["foo", "bar", "baz"];
104/// let sentence = append_list_as_sentence("Here is a sentence.", "My elements are", list);
105/// assert_eq!(sentence, "Here is a sentence. My elements are `foo`, `bar` and `baz`.");
106///
107/// let list = &["foo", "bar", "baz"];
108/// let sentence = append_list_as_sentence("The following list is problematic:", "", list);
109/// assert_eq!(sentence, "The following list is problematic: `foo`, `bar` and `baz`.");
110/// ```
111pub fn append_list_as_sentence<S: AsRef<str>>(base: &str, word: &str, els: &[S]) -> String {
112    if !els.is_empty() {
113        let mut requires = String::new();
114
115        if !base.is_empty() {
116            requires.push_str(base);
117            requires.push(' ');
118        }
119
120        for (i, r) in els.iter().enumerate() {
121            if i == 0 {
122                if !word.is_empty() {
123                    requires.push_str(word);
124                    requires.push(' ');
125                }
126            } else if i == els.len() - 1 {
127                requires.push_str(" and ");
128            } else {
129                requires.push_str(", ");
130            };
131
132            requires.push('`');
133            requires.push_str(r.as_ref());
134            requires.push('`');
135        }
136        requires.push('.');
137
138        requires
139    } else {
140        base.to_string()
141    }
142}