|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2015, Niklas Hauser |
| 3 | + * Copyright (c) 2023, Raphael Lehmann |
| 4 | + * |
| 5 | + * This file is part of the modm project. |
| 6 | + * |
| 7 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 8 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 9 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 10 | + */ |
| 11 | +// ---------------------------------------------------------------------------- |
| 12 | + |
| 13 | +#ifndef MODM_SSD1306_SPI_HPP |
| 14 | +#error "Don't include this file directly, use 'ssd1306_spi.hpp' instead!" |
| 15 | +#endif |
| 16 | + |
| 17 | +namespace modm |
| 18 | +{ |
| 19 | + |
| 20 | +template<class SpiMaster, class Cs, class Dc, uint8_t Height> |
| 21 | +ResumableResult<bool> |
| 22 | +Ssd1306Spi<SpiMaster, Cs, Dc, Height>::initialize(InitSequences initSeq) |
| 23 | +{ |
| 24 | + RF_BEGIN(); |
| 25 | + |
| 26 | + this->attachConfigurationHandler([]() { |
| 27 | + SpiMaster::setDataMode(SpiMaster::DataMode::Mode0); |
| 28 | + SpiMaster::setDataOrder(SpiMaster::DataOrder::MsbFirst); |
| 29 | + }); |
| 30 | + Cs::setOutput(modm::Gpio::High); |
| 31 | + Dc::setOutput(); |
| 32 | + |
| 33 | + if (initSeq == InitSequences::Hp12832_02) |
| 34 | + { |
| 35 | + // Initialize sequence from HP12832-02-TSBG12P091-A-VER1.0.pdf page 14 |
| 36 | + |
| 37 | + commandBuffer[0] = uint8_t(FundamentalCommands::DisplayOff); |
| 38 | + commandBuffer[1] = uint8_t(TimingAndDrivingCommands::DisplayClockDivideRatio); |
| 39 | + commandBuffer[2] = 0x80; // 0x90 or 0x80 (page 14)? |
| 40 | + commandBuffer[3] = uint8_t(HardwareConfigCommands::MultiplexRatio); |
| 41 | + commandBuffer[4] = 0x1F; // 0x3F or 0x1F (page 14)? |
| 42 | + commandBuffer[5] = uint8_t(HardwareConfigCommands::DisplayOffset); |
| 43 | + commandBuffer[6] = 0; |
| 44 | + commandBuffer[7] = uint8_t(AdressingCommands::MemoryMode); |
| 45 | + commandBuffer[8] = uint8_t(MemoryMode::HORIZONTAL); |
| 46 | + commandBuffer[9] = uint8_t(AdressingCommands::ColumnAddress); |
| 47 | + commandBuffer[10] = 0; |
| 48 | + commandBuffer[11] = 127; |
| 49 | + commandBuffer[12] = uint8_t(AdressingCommands::PageAddress); |
| 50 | + commandBuffer[13] = 0; |
| 51 | + commandBuffer[14] = (Height == 64) ? 7 : 3; |
| 52 | + commandBuffer[15] = uint8_t(HardwareConfigCommands::DisplayStartLine) | 0; |
| 53 | + commandBuffer[16] = uint8_t(HardwareConfigCommands::SegmentRemap0); |
| 54 | + commandBuffer[17] = uint8_t(HardwareConfigCommands::ComOutputScanDirectionIncrement); |
| 55 | + commandBuffer[18] = uint8_t(HardwareConfigCommands::ComPinsOrder); |
| 56 | + commandBuffer[19] = 0x02; |
| 57 | + commandBuffer[20] = uint8_t(FundamentalCommands::ContrastControl); |
| 58 | + commandBuffer[21] = 0x8F; |
| 59 | + commandBuffer[22] = uint8_t(TimingAndDrivingCommands::PreChargePeriod); |
| 60 | + commandBuffer[23] = 0x1F; |
| 61 | + commandBuffer[24] = uint8_t(TimingAndDrivingCommands::V_DeselectLevel); |
| 62 | + commandBuffer[25] = 0x30; |
| 63 | + commandBuffer[26] = uint8_t(FundamentalCommands::EntireDisplayResumeToRam); |
| 64 | + commandBuffer[27] = uint8_t(FundamentalCommands::NormalDisplay); |
| 65 | + commandBuffer[28] = uint8_t(TimingAndDrivingCommands::ChargePump); |
| 66 | + commandBuffer[29] = uint8_t(ChargePump::DISABLE); |
| 67 | + commandBuffer[30] = uint8_t(FundamentalCommands::DisplayOn); |
| 68 | + |
| 69 | + RF_RETURN_CALL(writeCommands(31)); |
| 70 | + } |
| 71 | + else { |
| 72 | + RF_RETURN(false); |
| 73 | + } |
| 74 | + |
| 75 | + RF_END(); |
| 76 | +} |
| 77 | + |
| 78 | +template<class SpiMaster, class Cs, class Dc, uint8_t Height> |
| 79 | +ResumableResult<bool> |
| 80 | +Ssd1306Spi<SpiMaster, Cs, Dc, Height>::setOrientation(glcd::Orientation orientation) |
| 81 | +{ |
| 82 | + RF_BEGIN(); |
| 83 | + |
| 84 | + if (orientation == glcd::Orientation::Landscape0) |
| 85 | + { |
| 86 | + commandBuffer[0] = uint8_t(HardwareConfigCommands::SegmentRemap127); |
| 87 | + commandBuffer[1] = uint8_t(HardwareConfigCommands::ComOutputScanDirectionDecrement); |
| 88 | + } |
| 89 | + else if (orientation == glcd::Orientation::Landscape180) |
| 90 | + { |
| 91 | + commandBuffer[0] = uint8_t(HardwareConfigCommands::SegmentRemap0); |
| 92 | + commandBuffer[1] = uint8_t(HardwareConfigCommands::ComOutputScanDirectionIncrement); |
| 93 | + } |
| 94 | + else { |
| 95 | + RF_RETURN(false); |
| 96 | + } |
| 97 | + |
| 98 | + RF_END_RETURN_CALL(writeCommands(2)); |
| 99 | +} |
| 100 | + |
| 101 | +template<class SpiMaster, class Cs, class Dc, uint8_t Height> |
| 102 | +ResumableResult<bool> |
| 103 | +Ssd1306Spi<SpiMaster, Cs, Dc, Height>::configureScroll(uint8_t origin, uint8_t size, |
| 104 | + ScrollDirection direction, ScrollStep steps) |
| 105 | +{ |
| 106 | + RF_BEGIN(); |
| 107 | + |
| 108 | + if (!RF_CALL(disableScroll())) |
| 109 | + RF_RETURN(false); |
| 110 | + |
| 111 | + { |
| 112 | + uint8_t beginY = (origin > 7) ? 7 : origin; |
| 113 | + |
| 114 | + uint8_t endY = ((origin + size) > 7) ? 7 : (origin + size); |
| 115 | + if (endY < beginY) endY = beginY; |
| 116 | + |
| 117 | + commandBuffer[0] = uint8_t(direction); |
| 118 | + commandBuffer[1] = 0x00; |
| 119 | + commandBuffer[2] = beginY; |
| 120 | + commandBuffer[3] = uint8_t(steps); |
| 121 | + commandBuffer[4] = endY; |
| 122 | + commandBuffer[5] = 0x00; |
| 123 | + commandBuffer[6] = 0xFF; |
| 124 | + } |
| 125 | + |
| 126 | + RF_END_RETURN_CALL(writeCommands(7)); |
| 127 | +} |
| 128 | + |
| 129 | +template<class SpiMaster, class Cs, class Dc, uint8_t Height> |
| 130 | +ResumableResult<bool> |
| 131 | +Ssd1306Spi<SpiMaster, Cs, Dc, Height>::writeDisplay() |
| 132 | +{ |
| 133 | + RF_BEGIN(); |
| 134 | + |
| 135 | + RF_WAIT_UNTIL(this->acquireMaster()); |
| 136 | + Cs::reset(); |
| 137 | + |
| 138 | + Dc::set(); |
| 139 | + |
| 140 | + RF_CALL(SpiMaster::transfer((uint8_t*)(&this->buffer), nullptr, sizeof(this->buffer))); |
| 141 | + |
| 142 | + if (this->releaseMaster()) { |
| 143 | + Cs::set(); |
| 144 | + } |
| 145 | + |
| 146 | + RF_END_RETURN(true); |
| 147 | +} |
| 148 | + |
| 149 | +template<class SpiMaster, class Cs, class Dc, uint8_t Height> |
| 150 | +ResumableResult<bool> |
| 151 | +Ssd1306Spi<SpiMaster, Cs, Dc, Height>::writeCommands(std::size_t length) |
| 152 | +{ |
| 153 | + RF_BEGIN(); |
| 154 | + |
| 155 | + RF_WAIT_UNTIL(this->acquireMaster()); |
| 156 | + Cs::reset(); |
| 157 | + |
| 158 | + Dc::reset(); |
| 159 | + |
| 160 | + RF_CALL(SpiMaster::transfer(commandBuffer, nullptr, length)); |
| 161 | + |
| 162 | + if (this->releaseMaster()) { |
| 163 | + Cs::set(); |
| 164 | + } |
| 165 | + |
| 166 | + RF_END_RETURN(true); |
| 167 | +} |
| 168 | + |
| 169 | +} // namespace modm |
0 commit comments