PortaMob
PortaMob.cpp
1 /*
2  * This file is part of the PortaMob Shield Arduino library.
3  * Copyright (c) 2023 Nathanne Isip
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  * THE SOFTWARE.
22  */
23 
24 #include <Arduino.h>
25 #include <Adafruit_SSD1306.h>
26 #include <SPI.h>
27 #include <Wire.h>
28 
29 #include "PortaMob.h"
30 
31 Adafruit_SSD1306 PortaMob::portamob_display(void (*on_error)()) {
32  Adafruit_SSD1306 display(128, 32, &Wire, -1);
33 
34  if(!display.begin(
35  SSD1306_SWITCHCAPVCC,
37  )) on_error();
38 
39  display.clearDisplay();
40  return display;
41 }
42 
44  pinMode(PORTAMOB_KEY_1, INPUT_PULLUP);
45  pinMode(PORTAMOB_KEY_2, INPUT_PULLUP);
46  pinMode(PORTAMOB_KEY_3, INPUT_PULLUP);
47  pinMode(PORTAMOB_KEY_4, INPUT_PULLUP);
48  pinMode(PORTAMOB_KEY_5, INPUT_PULLUP);
49  pinMode(PORTAMOB_KEY_6, INPUT_PULLUP);
50  pinMode(PORTAMOB_KEY_7, INPUT_PULLUP);
51  pinMode(PORTAMOB_KEY_8, INPUT_PULLUP);
52  pinMode(PORTAMOB_KEY_9, INPUT_PULLUP);
53 }
54 
55 bool PortaMob::is_button_pressed(uint8_t button) {
56  return digitalRead(button) == LOW;
57 }
58 
60  Wire.begin();
61 }
62 
64  uint8_t addr,
65  uint8_t value
66 ) {
67  Wire.beginTransmission(PORTAMOB_EEPROM_ADDR);
68 
69  Wire.write((int)(addr >> 8));
70  Wire.write((int)(addr & 0xFF));
71  Wire.write(value);
72 
73  Wire.endTransmission();
74 }
75 
76 uint8_t PortaMob::eeprom_read(uint8_t addr) {
77  byte data = 0xFF;
78  Wire.beginTransmission(PORTAMOB_EEPROM_ADDR);
79 
80  Wire.write((int)(addr >> 8));
81  Wire.write((int)(addr & 0xFF));
82 
83  Wire.endTransmission();
84  Wire.requestFrom(PORTAMOB_EEPROM_ADDR, 1);
85 
86  if(Wire.available())
87  data = Wire.read();
88  return data;
89 }
90 
92  Wire.beginTransmission(PORTAMOB_EEPROM_ADDR);
93 
94  return Wire.endTransmission(
96  ) == 0;
97 }
The PortaMob Arduino Shield Library.
#define PORTAMOB_KEY_5
Digital pin number for button 5.
Definition: PortaMob.h:52
#define PORTAMOB_KEY_6
Digital pin number for button 6.
Definition: PortaMob.h:53
#define PORTAMOB_KEY_2
Digital pin number for button 2.
Definition: PortaMob.h:49
#define PORTAMOB_KEY_1
Digital pin number for button 1.
Definition: PortaMob.h:48
#define PORTAMOB_KEY_9
Digital pin number for button 9.
Definition: PortaMob.h:56
#define PORTAMOB_EEPROM_ADDR
EEPROM I2C address.
Definition: PortaMob.h:58
#define PORTAMOB_KEY_8
Digital pin number for button 8.
Definition: PortaMob.h:55
#define PORTAMOB_KEY_3
Digital pin number for button 3.
Definition: PortaMob.h:50
#define PORTAMOB_KEY_7
Digital pin number for button 7.
Definition: PortaMob.h:54
#define PORTAMOB_SCREEN_ADDRESS
OLED screen I2C address.
Definition: PortaMob.h:59
#define PORTAMOB_KEY_4
Digital pin number for button 4.
Definition: PortaMob.h:51
static void init_i2c()
Initializes the I2C communication.
Definition: PortaMob.cpp:59
static Adafruit_SSD1306 portamob_display(void(*on_error)())
Initializes the PortaMob display.
Definition: PortaMob.cpp:31
static void init_buttons(void)
Initializes the push buttons on the PortaMob Shield.
Definition: PortaMob.cpp:43
static bool is_button_pressed(uint8_t button)
Checks if a specific button is pressed.
Definition: PortaMob.cpp:55
static void eeprom_write(uint8_t addr, uint8_t value)
Writes a value to the EEPROM memory.
Definition: PortaMob.cpp:63
static bool has_eeprom()
Checks if the EEPROM memory is present.
Definition: PortaMob.cpp:91
static uint8_t eeprom_read(uint8_t addr)
Reads a value from the EEPROM memory.
Definition: PortaMob.cpp:76