Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Raspberry Pi Pico をSwiftで操作する

Raspberry Pi Pico をSwiftで操作する

iOSDC 2024 day1 trackD 17:25~

yochidros

August 23, 2024
Tweet

More Decks by yochidros

Other Decks in Programming

Transcript

  1. &NCFEEFE4XJGU w αϒηοτݴޠ w ίʔυαΠζΛۃݶ·Ͱ࡟ݮ w ҉໧తͳ࣮ߦ࣌ධՁΛېࢭ w ΞϩέʔλʔΛ࣋ͨͳ͍Α͏Ϗϧυ͢Δ ͜ͱ΋Ͱ͖Δ

    ŠOPBMMPDBUJPO  w swiftc — - enable-experimental- feature Embedded -wmoͰ &NCFEEFE4XJGUϞʔυͰίϯύΠϧ w ίʔυαΠζ͸ϥϯλΠϜɾඪ४ϥΠϒ ϥϦؚΊͯd,#
  2. 3F fl FDUJPO &YJTUFOUJBM %ZOBNJDMPBEJOH 2 | class Animal {}

    3 | let animal = Animal() 4 | let mirror = Mirror(reflecting: animal) | `- error: 'Mirror' is unavailable w ϥϯλΠϜ࣌ʹར༻͍ͯ͠Δػೳ͸ར༻Ͱ͖ͳ͍ w ϝλσʔλΛ͍ΕΔͱόΠφϦαΠζ͕େ͖͘ͳΔ w 04ͱҧ͍EMPQFO΍EMTZNͳͲ͕ແ͍ protocol Animal { var name: String { get } } struct Dog: Animal { var name: String { "🐕" } } func sayName(animal: any Animal) { print(animal.name) } let dog = Dog() sayName(animal: dog) /* 28 | let dog = Dog() 29 | say(animal: dog) | `- error: cannot use a value of protocol type 'any Animal' in embedded Swift */
  3. w 4UBUJD4USJOH 4USJOHར༻Մೳ w 4USJOHͷ৔߹ w 4USJOH*OUFSQPMBUJPOར༻Մೳ w 4USJOHͷ৔߹͸จࣈྻૢ࡞͸6OJDPEF5BCMF͕ඞཁ w

    ඞཁͳ৔߹͸खಈͰ6OJDPEFϥΠϒϥϦΛϦϯΫΛ͢Δඞཁ͕͋Δ w MJCTXJGUFNCFEEFEBSNWNOPOFOPOFFBCJMJCTXJGU6OJDPEF%BUB5BCMFTB 4USJOH 6OEF fi OFETZNCPMTGPSBSDIJUFDUVSFBSN @@TXJGU@TUEMJC@HFU$PNQPTJUJPO SFGFSFODFEGSPN
  4. w 4UBUJD4USJOH *OU #PPM  4USJOH  w PCKFDUͳͲͷϝλσʔλ৘ใͷग़ྗ͸ෆՄ w

    4USJOH͸JOUFSQPMBUJPOʹΑΔ4USJOHੜ੒͸Մೳ͕ͩͦΕҎ֎͸ෆՄ QSJOU struct Dog { var name: String { "🐕" } } let dog = Dog() print(“Hello!”) print(dog.name) print("\(dog)") print("\(type(of: dog))") print(“\("iOSDC 2024”)") NBJO )FMMP 🐕 DBOOPUQSJOUWBMVFJOFNCFEEFE4XJGU  DBOOPUQSJOUWBMVFJOFNCFEEFE4XJGU  DBOOPUQSJOUWBMVFJOFNCFEEFE4XJGU  J04%$
  5. ରԠද "SSBZ "SSBZ4MJDFT ✅ QSJOU EFCVH1SJOU ⚠ BTTFSU QSFDPOEJUJPO GBUBM&SSPS

    ⚠ 3BOHF $MPTFE3BOHF 4USJEF ✅ $PEBCMF &ODPEBCMF %FDPEBCMF ❌ 3FTVMU ✅ $PMMFDUJPO ✅ 4FU ✅ $VTUPN4USJOH$POWFSUJCMF ✅ 4*.%UZQFT ✅ #PPM *OUFSHFS 'MPBU ✅ 4UBUJD4USJOH ✅ %JDUJPOBSZ ✅ 4USJOH 4USJOH*OUFSQPMBUJPOT ✅ )BTIBCMF &RVUBCMF $PNQBSBCMF ✅ 6OJDPEF ✅ *OQVU0VUQVU4USFBN ✅ 6OTBGF .VUBCMF 3BX #V ff FS 1PJOUFS ✅ ,FZ1BUIT ✅ 7BS"SHT 💭 -B[Z$PMMFDUJPO 💭 $PODVSSFODZ ⚠ .JSSPS ❌ 4ZODSPOJ[BUJPO ✅ 0CKDJOUFSPQ ❌ $ $ JOUFSPQ ✅ IUUQTHJUIVCDPNTXJGUMBOHTXJGUCMPCNBJOEPDT&NCFEEFE4XJGU&NCFEEFE4XJGU4UBUVTNE
  6. IUUQTXXXSBTQCFSSZQJDPNEPDVNFOUBUJPONJDSPDPOUSPMMFSTJNBHFT QJDPQJOPVUTWH ͜ͷ-&%Λ νΧνΧͤ͞Δ (1*0 -&% @main struct Main {

    static func main() { // GPIO25 <= PICO_DEFAULT_LED_PIN let led = UInt32(PICO_DEFAULT_LED_PIN) gpio_init(led) gpio_set_dir(led, true) while true { gpio_put(led, true) sleep_ms(1000) gpio_put(led, false) sleep_ms(1000) } } } .BJOTXJGU
  7. IUUQTXXXSBTQCFSSZQJDPNEPDVNFOUBUJPONJDSPDPOUSPMMFSTJNBHFTQJDPQJOPVUTWH @main struct Main { static func main() { //

    GPIO25 <= PICO_DEFAULT_LED_PIN let led = UInt32(PICO_DEFAULT_LED_PIN) gpio_init(led) gpio_set_dir(led, true) while true { gpio_put(led, true) sleep_ms(1000) gpio_put(led, false) sleep_ms(1000) } } } .BJOTXJGU ϞσϦϯά͢Δ
  8. struct PicoDevice { var defaultLED: LED var redLed: LED? var

    blueLed: LED? var greenLed: LED? var allOn: Bool = false { didSet { defaultLED.isOn = allOn redLed?.isOn = allOn blueLed?.isOn = allOn greenLed?.isOn = allOn } } } struct LED { private let pin: UInt32 init(pin: UInt32) { self.pin = pin gpio_init(pin) gpio_set_dir(pin, true) } var isOn: Bool = false { didSet { gpio_put(pin, isOn) } } }
  9. IUUQTXXXSBTQCFSSZQJDPNEPDVNFOUBUJPONJDSPDPOUSPMMFSTJNBHFTQJDPQJOPVUTWH static func main() { var device = PicoDevice( defaultLED:

    LED( pin: UInt32(PICO_DEFAULT_LED_PIN) ), redLed: LED(pin: UInt32(28)), blueLed: LED(pin: UInt32(16)), greenLed: LED(pin: UInt32(18)) ) while true { device.allOn.toggle() sleep_ms(1000) } }