[{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003eThis Note is to document some tips and tricks on using the \u003ca href=\"https://www.adafruit.com/product/6200\" target=\"_blank\"\u003eAdafruit Fruit Jam\u003c/a\u003e and the \u003ccode\u003eadafruit_fruitjam\u003c/code\u003e library.\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"text","content":"\n        \u003ch2\u003eDetermining what board you're running on\u003c/h2\u003e\n\u003cp\u003eSome code will run both on Fruit Jam and another RP2350B board. The need is to determine which platform one is using. This is handy, for example, in determining if the TLV320 on the Fruit Jam is available. Another method could be to do an I2C scan and look for a TLV320. But the method below is a bit more straightforward in finding out if the board is a Fruit Jam or not.\u003c/p\u003e\n      ","metadata":{}},{"element_type":"code","content":"# SPDX-FileCopyrightText: 2025 Anne Barela for Adafruit Industries\n#\n# SPDX-License-Identifier: MIT\n#\n# What Machine is this?\n\nimport os\n\nmachine = os.uname().machine\n\nif machine[9:18] == \"Fruit Jam\":\n    print(\"\\nFruit Jam\")\nelif machine[9:21] == \"Metro RP2350\":\n    print(\"\\nMetro RP2350\")\nelse:\n    print(\"\\nOther\")","metadata":{"language":"python","linenums":false,"filename":""}},{"element_type":"text","content":"\n  \n        \u003ch2\u003eA \u003ccode\u003etone\u003c/code\u003e function for \u003ccode\u003eadafruit_fruitjam\u003c/code\u003e\n\u003c/h2\u003e\n\u003cp\u003eUnlike using \u003ccode\u003esimpleio\u003c/code\u003e for tone generation, which uses pwm on a pin, there is no tone function for I2S devices like the TLV320 DAC on the Fruit Jam (that I am aware of). The function below works, and takes arguments\u0026nbsp;\u003ccode\u003efrequency\u003c/code\u003e and \u003ccode\u003eduration\u003c/code\u003e and an optional \u003ccode\u003evolume\u003c/code\u003e parameter.\u003c/p\u003e\n\u003cp\u003eNote the DAC settings I chose. The TLV320 has so many settings it is head spinning. These may need to be optimized.\u003c/p\u003e\n      \n","metadata":{}},{"element_type":"code","content":"# SPDX-FileCopyrightText: 2025 Anne Barela for Adafruit Industries\n#\n# SPDX-License-Identifier: MIT\n#\n# Implement a tone function for CircuitPython I2C DACs\n\nimport sys\nimport array\nimport math\nimport time\nimport audiocore\nfrom adafruit_fruitjam.peripherals import Peripherals\n\n# Initialize Fruit Jam peripherals\nfruit_jam = Peripherals(audio_output=\"speaker\")  # or \"headphone\"\nif fruit_jam.dac is None:\n    print(\"\\nAre you running this on a Fruit Jam?\")\n    sys.exit(1)\n\ndef tone(frequency, duration, volume=0.5):\n    print(frequency, duration)\n    \"\"\"\n    Play a tone at a specific frequency for a given duration.\n    \n    Args:\n        frequency (int/float): Frequency in Hz (e.g., 440 for A4)\n        duration (float): Duration in seconds\n        volume (float): Volume level from 0.0 to 1.0 (default 0.5)\n    \"\"\"\n    # Get the sample rate from the DAC\n    sample_rate = fruit_jam.dac.sample_rate\n    \n    # Calculate length of one period of the waveform\n    length = sample_rate // int(frequency)\n    \n    # Generate a sine wave for one period\n    sine_wave = array.array(\"h\", [0] * length)\n    for i in range(length):\n        sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * volume * (2**15 - 1))\n    \n    # Create a RawSample from the sine wave\n    sine_wave_sample = audiocore.RawSample(sine_wave, sample_rate=sample_rate)\n    \n    # Play the tone\n    fruit_jam.audio.play(sine_wave_sample, loop=True)\n    time.sleep(duration)\n    fruit_jam.audio.stop()\n\n\n# Example usage:\nif __name__ == \"__main__\":\n    # Configure speaker settings\n    fruit_jam.dac.speaker_output = True\n    fruit_jam.dac.dac_volume = 0         # Digital volume (-63.5 to 24.0 dB)\n    fruit_jam.dac.speaker_volume = -5    # Analog volume (-78.3 to 0 dB)\n    fruit_jam.dac.speaker_gain = 18      # Amplifier gain (6, 12, 18, or 24 dB)\n    \n    # Play some tones\n    tone(440, 1.0)      # A4 for 1 second\n    time.sleep(0.2)     # Pause between tones\n    tone(523, 0.5)      # C5 for 0.5 seconds\n    time.sleep(0.2)\n    tone(659, 0.75)     # E5 for 0.75 seconds\n    \n    # Example: play a melody\n    notes = [\n        (262, 0.5),  # C4\n        (294, 0.5),  # D4\n        (330, 0.5),  # E4\n        (349, 0.5),  # F4\n        (392, 1.0),  # G4\n    ]\n    \n    for freq, dur in notes:\n        tone(freq, dur, volume=0.5)\n        time.sleep(0.1)","metadata":{"language":"python","linenums":false,"filename":""}}]